环境:
ubuntu16.04.1 LTS
Python 3.5.2
Apache/2.4.18 (Ubuntu)
OpenSSL/1.0.2g
mod_wsgi/4.3.0
Python/3.5.2
Django 2.1.2
问题:在部署完成后apache的error.log报错ImportError: No module named 'django',同时服务器500
查看详细信息发现python环境与安装django时一样排除python环境版本不正确的问题
[Tue Oct 30 09:54:13.482587 2018] [wsgi:warn] [pid 13678:tid 140365118388096] mod_wsgi: Compiled for Python/3.5.1+.
[Tue Oct 30 09:54:13.482593 2018] [wsgi:warn] [pid 13678:tid 140365118388096] mod_wsgi: Runtime using Python/3.5.2.
[Tue Oct 30 09:54:13.483150 2018] [mpm_event:notice] [pid 13678:tid 140365118388096] AH00489: Apache/2.4.18 (Ubuntu) OpenSSL/1.0.2g mod_wsgi/4.3.0 Python/3.5.2 configured -- resuming normal operations
[Tue Oct 30 09:54:13.483170 2018] [core:notice] [pid 13678:tid 140365118388096] AH00094: Command line: '/usr/sbin/apache2'
[Tue Oct 30 09:54:16.845781 2018] [wsgi:error] [pid 13681:tid 140364824405760] [client 101.90.255.187:26560] mod_wsgi (pid=13681): Target WSGI script '/home/ubuntu/django_project/mysite/mysite/wsgi.py' cannot be loaded as Python module.
[Tue Oct 30 09:54:16.845840 2018] [wsgi:error] [pid 13681:tid 140364824405760] [client 101.90.255.187:26560] mod_wsgi (pid=13681): Exception occurred processing WSGI script '/home/ubuntu/django_project/mysite/mysite/wsgi.py'.
[Tue Oct 30 09:54:16.846037 2018] [wsgi:error] [pid 13681:tid 140364824405760] [client 101.90.255.187:26560] Traceback (most recent call last):
[Tue Oct 30 09:54:16.846059 2018] [wsgi:error] [pid 13681:tid 140364824405760] [client 101.90.255.187:26560] File "/home/ubuntu/django_project/mysite/mysite/wsgi.py", line 11, in <module>
[Tue Oct 30 09:54:16.846064 2018] [wsgi:error] [pid 13681:tid 140364824405760] [client 101.90.255.187:26560] from django.core.wsgi import get_wsgi_application
[Tue Oct 30 09:54:16.846078 2018] [wsgi:error] [pid 13681:tid 140364824405760] [client 101.90.255.187:26560] ImportError: No module named 'django'
查看django安装的位置
$ pip3 install django
Requirement already satisfied: django in ./.local/lib/python3.5/site-packages (2.1.2)
Requirement already satisfied: pytz in ./.local/lib/python3.5/site-packages (from django) (2018.5)
查看wsgi.py运行时的包检索路径
['/usr/lib/python35.zip', '/usr/lib/python3.5', '/usr/lib/python3.5/plat-x86_64-linux-gnu', '/usr/lib/python3.5/lib-dynload', '/usr/local/lib/python3.5/dist-packages', '/usr/lib/python3/dist-packages']
发现没有django的安装路径
将该路径加入sys.path
import sys
sys.path.append(r"/home/ubuntu/.local/lib/python3.5/site-packages")
依然报错500
考虑权限问题,测试发现Permission denied
PermissionError: [Errno 13] Permission denied: '/home/ubuntu/.local/lib/python3.5/site-packages'
执行以下命令
cd /home/ubuntu/.local/lib/python3.5
sudo chmod -R 777 site-packages/
问题解决