使用 mod_wsgi 部署 Django 项目

Deploying django3 project with mod_wsgi

我正在尝试使用 Apache mod_wsgi 发布我的 django 项目。我在 virtualenv 中创建了一个名为 sampleapp 的简单 django3 项目。然后,我配置我的 /etc/apache2/sites-enabled/000-default.conf 文件如下

<VirtualHost *:80>
ServerAdmin webmaster@localhost
DocumentRoot /var/www/html

ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined

WSGIDaemonProcess sampleapp python-home=/var/www/html/sampleapp/env python-path=/var/www/html/sampleapp:/var/www/html/sampleapp/env/lib/python3.7/site-packages
WSGIProcessGroup sampleapp
WSGIScriptAlias /sampleapp /var/www/html/sampleapp/sampleapp/wsgi.py process-group=sampleapp

</VirtualHost>

虽然我的 virtualenv 的 python 版本是 3.7,但是 mod_wsgi 使用的是系统的 python 版本(3.5),所以它看不到我的 virtualenv 并且不使用它的 python版本。我的apache错误日志如下:

[Sat Feb 20 15:36:02.390188 2021] [mpm_prefork:notice] [pid 20090] AH00169: caught SIGTERM, shutting down
[Sat Feb 20 15:36:03.226065 2021] [mpm_prefork:notice] [pid 20204] AH00163: Apache/2.4.18 (Ubuntu) mod_wsgi/4.3.0 Python/3.5.2 configured -- resuming normal operations
[Sat Feb 20 15:36:03.226278 2021] [core:notice] [pid 20204] AH00094: Command line: '/usr/sbin/apache2'
[Sat Feb 20 15:36:03.294321 2021] [wsgi:error] [pid 20207] mod_wsgi (pid=20207): Call to 'site.addsitedir()' failed for '(null)', stopping.
[Sat Feb 20 15:36:03.295568 2021] [wsgi:error] [pid 20207] mod_wsgi (pid=20207): Call to 'site.addsitedir()' failed for '/var/www/html/sampleapp/env/lib/python3.7/site-packages'.
[Sat Feb 20 15:36:07.084368 2021] [wsgi:error] [pid 20207] mod_wsgi (pid=20207): Call to 'site.addsitedir()' failed for '(null)', stopping.
[Sat Feb 20 15:36:07.084455 2021] [wsgi:error] [pid 20207] mod_wsgi (pid=20207): Call to 'site.addsitedir()' failed for '/var/www/html/sampleapp/env/lib/python3.7/site-packages'.
[Sat Feb 20 15:36:07.085660 2021] [wsgi:error] [pid 20207] [remote 127.0.0.1:47028] mod_wsgi (pid=20207): Target WSGI script '/var/www/html/sampleapp/sampleapp/wsgi.py' cannot be loaded as Python module.
[Sat Feb 20 15:36:07.085769 2021] [wsgi:error] [pid 20207] [remote 127.0.0.1:47028] mod_wsgi (pid=20207): Exception occurred processing WSGI script '/var/www/html/sampleapp/sampleapp/wsgi.py'.
[Sat Feb 20 15:36:07.086188 2021] [wsgi:error] [pid 20207] [remote 127.0.0.1:47028] Traceback (most recent call last):
[Sat Feb 20 15:36:07.086266 2021] [wsgi:error] [pid 20207] [remote 127.0.0.1:47028]   File "/var/www/html/sampleapp/sampleapp/wsgi.py", line 12, in <module>
[Sat Feb 20 15:36:07.086280 2021] [wsgi:error] [pid 20207] [remote 127.0.0.1:47028]     from django.core.wsgi import get_wsgi_application
[Sat Feb 20 15:36:07.086321 2021] [wsgi:error] [pid 20207] [remote 127.0.0.1:47028] ImportError: No module named 'django'

如何配置 mod_wsgi 以使用我的 venv 的 python 版本?或者我的错在哪里?感谢您的回答。

因此 mod_wsgi 被编译为 Apache 的特定版本 Python。你不能使它 运行 成为 python 的不同版本。您可以将 mod_wsgi 重建为正确的版本,或者更改虚拟环境。这将删除 virtualenv 包装器但保留项目文件。

我认为“更改 virtualenv”会更容易。

rmvirtualenv env-name
mkvirtualenv -p python3.5 env-name
setvirtualenvproject env-name

当您使用 'mkvirtualenv' 时,使用 -r 标签作为 requirements.txt

或重建 mod_wsgi:

Documentation