几天前,我决定将python从2.7版本升级到3.7。这是我目前的设置。
Ubuntu 16.04
Python 3.7.7
Django 3.0.6
Apache/2.4.18
使用命令 python -m venv --system-site-packages /var/www/path/to/myenv
我已经创建了病毒环境,激活这个环境后,我创建了一个新的项目,环境的路径是这样的 /var/www/path/to/myenv
项目的路径是这样的 /var/www/path/to/myenv/myproject
.的配置 myproject.conf
看起来是这样的。
<VirtualHost *:80>
ServerName myproject.com
ServerAlias www.myproject.com
WSGIDaemonProcess myproject processes=2 threads=15 display-name=%{GROUP} python-home=/var/www/path/to/myenv python-path=/var/www/path/to/myenv/myproject
WSGIProcessGroup candyhand
WSGIScriptAlias / /var/www/path/to/myenv/myproject/myproject/wsgi.py
<Directory /var/www/path/to/myenv/myproject/myproject/>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
<Directory /var/www/path/to/myenv/myproject/>
Require all granted
</Directory>
CustomLog /var/www/path/to/myenv/myproject/logs/apache_access.log combined
ErrorLog /var/www/path/to/myenv/myproject/logs/apache_error.log
Alias /static/ /var/www/path/to/myenv/myproject/static/
<Directory /var/www/path/to/myenv/myproject/>
Options Indexes MultiViews FollowSymLinks
AllowOverride None
Require all granted
</Directory>
Alias /media/ /var/www/path/to/myenv/myproject/media/
<Directory /var/www/path/to/myenv/myproject/>
Options Indexes MultiViews FollowSymLinks
AllowOverride None
Require all granted
</Directory>
</VirtualHost>
但我在apache服务器上得到了500个错误。下面是apache服务器的日志。
mod_wsgi (pid=9495): Target WSGI script '/var/www/path/to/myenv/myproject/myproject/wsgi.py' cannot be loaded as Python module.
[Wed May 20 16:25:08.145621 2020] [wsgi:error] [pid 9495] mod_wsgi (pid=9495): Exception occurred processing WSGI script '/var/www/path/to/myenv/myproject/myproject/wsgi.py'.
[Wed May 20 16:25:08.145788 2020] [wsgi:error] [pid 9495] Traceback (most recent call last):
[Wed May 20 16:25:08.145864 2020] [wsgi:error] [pid 9495] File "/var/www/path/to/myenv/myproject/myproject/wsgi.py", line 12, in <module>
[Wed May 20 16:25:08.145885 2020] [wsgi:error] [pid 9495] from django.core.wsgi import get_wsgi_application
[Wed May 20 16:25:08.145945 2020] [wsgi:error] [pid 9495] ImportError: No module named 'django'
我配置了 VirtualHost
据此 文件但也许我犯了一个错误的地方,谢谢你的建议。
P.S.python manage.py runserver
命令运行良好
问题很可能是 python -m venv
在您的virtualenv中没有生成activate_this.py,请查看以下文档。https:/modwsgi.readthedocs.ioendevelopuser-guidesvirtual-environments.html#daemon-mode-multiple-applications。
"当需要从WSGI脚本文件中激活Python虚拟环境时,最好使用virtualenv或virtualenvwrapper来创建Python虚拟环境。这是因为它们都提供了 activate_this.py 脚本文件,它完成了设置 sys.path 的所有工作。当你用Python 3使用pyvenv或python -m venv时,没有提供这样的激活脚本。"
EDIT
刚刚发现mod_wsgi v4.6.1似乎可以处理由 python -m venv
但mod_wsgi必须使用与virtualenv完全相同的python版本 (mod_wsgi不从virtualenv中获取python解释器,只需检查wsgi.py中的python版本以确保mod_wsgi使用的是正确的版本)。如果是错误的解释器版本,你必须在更新你的全局python包到正确的版本号后重新安装mod_wsgi。
请检查您是否在 apache2.conf 中插入了这些语言。
WSGIPythonPath /usr/local/lib/python3.7/dist-packages
WSGILazyInitialization On
WSGIApplicationGroup %{GLOBAL}
你能不能分享一下wsgi.py和apache日志文件的开头?
下面添加到你的VirtualHost之外。 WSGIPythonHome varwwwpathtomyenv