使用 mod_wsgi 在 Apache 上部署 Django

Deploying Django on Apache with mod_wsgi

我目前正在尝试使用 mod_wsgi 在 Centos 服务器上的 Apache 上设置 Django 应用程序。这在 https 上设置为 运行。它在 virtualenv 上。

但是,目前它给出了 504 网关超时错误。

错误日志:

(2)No such file or directory: mod_wsgi (pid=15007): Unable to stat Python home /bin/virtualenv:/var/www/django/mysite/mysite/lib/python3.6/site-packages. Python interpreter may not be able to be initialized correctly. Verify the supplied path and access permissions for whole of the path.
Fatal Python error: Py_Initialize: Unable to get the locale encoding
ModuleNotFoundError: No module named 'encodings'

httpd.conf

<VirtualHost *:443>
ServerName mysite.example.com
ServerAlias www.mysite.example.com
# DocumentRoot /var/www/django/mysite

LogLevel info

ErrorLog /etc/httpd/logs/mysite_error.log


Alias /static /var/www/django/mysite/static

<Directory /var/www/django/mysite/static>
  Require all granted
</Directory>

<Directory /var/www/django/mysite/mysite>
    <Files wsgi.py>
        Require all granted
    </Files>
</Directory>

WSGIDaemonProcess mysite python-home=/bin/virtualenv:/var/www/django/mysite/mysite/lib/python3.6/site-packages
WSGIProcessGroup mysite
WSGIScriptAlias / /var/www/django/mysite/mysite/wsgi.py
WSGIApplicationGroup %{GLOBAL}


SSLEngine on
SSLProtocol all -SSLv2

SSLCompression off

SSLCertificateFile /etc/pki/tls/certs/mysite.cert.pem
SSLCertificateKeyFile /etc/pki/tls/private/mysite.key.pem
SSLCertificateChainFile /etc/pki/tls/certs/mysite.chain.pem

</VirtualHost>

这是不正确的:

WSGIDaemonProcess mysite python-home=/bin/virtualenv:/var/www/django/mysite/mysite/lib/python3.6/site-packages

评论:

python-home值应该是单个目录,这与使用虚拟环境时Python的sys.prefix相同。不要添加 site-packages 目录。

最重要的是,mod_wsgi 必须针对用于创建虚拟环境的相同 Python 版本进行编译。您不能将为 Python 2.7 编译的 mod_wsgi 与使用 Python 3.6.

创建的虚拟环境一起使用

检查编译的 Python mod_wsgi 的版本: