Hostgator VPS 中的 Django 部署 - PHP 和 Python 在同一台服务器上 - easyapache24 和 mod-wsgi

Django deployment in Hostgator VPS - PHP and Python on same server - easyapache24 and mod-wsgi

PHP 和 Django 在同一服务器上

到目前为止我尝试了什么:在 /home/user/public_html/demo/django/ 中创建了 venv 和项目,并通过加载外部配置文件在 www.ourdomain.com 的现有虚拟主机配置的末尾添加了额外的虚拟主机配置。配置文件中没有错误。 Apache 重新启动成功。 PHP 项目 运行 不错。

这是我们主域的主要 apache conf 文件的一部分 www.ourdomain.com

<VirtualHost ipaddress:80>
    ServerName ourdomain.com
    ServerAlias www.ourdomain.com
    DocumentRoot /home/user/public_html
    ServerAdmin webmaster@ourdomain.com
    UseCanonicalName Off

    # configuration to load modules 

    # To customize this VirtualHost use an include file at the following location
    Include "/etc/apache2/conf.d/userdata/std/2_4/user/ourdomain.com/*.conf"
</VirtualHost>

我在 /etc/apache2/conf.d/userdata/std/2_4/user/ourdomain.com/ 添加了一个 conf 文件,其中包括

    Alias /demo/django/static /home/user/public_html/demo/django/static
    <Directory /home/user/public_html/demo/django/static>
        Require all granted
    </Directory>

    Alias /demo/django/media /home/user/public_html/demo/django/media
    <Directory /home/user/public_html/demo/django/media>
        Require all granted
    </Directory>

    <Directory /home/user/public_html/demo/django/testproject>
        <Files wsgi.py>
            Require all granted
        </Files>
    </Directory>

    WSGIDaemonProcess testdjango python-path=/home/user/public_html/demo/django python-home=/home/user/public_html/demo/django/venv/lib/python3.6/site-packages
    WSGIProcessGroup testdjango
    WSGIScriptAlias /demo/django /home/user/public_html/demo/django/testproject/wsgi.py

检查 mod_wsgi 是否已加载

# sudo /usr/sbin/httpd -M
[sudo] password for user:
Loaded Modules:
 core_module (static)
 so_module (static)
 http_module (static)
 ...
 suphp_module (shared)
 wsgi_module (shared)
 passenger_module (shared)
#

当我尝试使用此配置访问 http://ourdomain.com/demo/django 时,显示 网关超时 错误。

# error_log
[Fri Jul 10 14:19:21.342208 2020] [wsgi:error] [pid 3083:tid 47072565069568] [client *.*.*.*:*] Timeout when reading response headers from daemon process 'django': /home/user/public_html/demo/django/testproject/wsgi.py

我发布此内容以供遇到此问题的任何人将来参考 *

后来查了一下发现是静态文件服务,只有页面加载不出来。 旧配置在 WSGIDaemonProcess 中存在问题。 python-homepython-path 值不正确。进一步阅读 WSGIDaemonProcess 部分中的 docs 我更改了相应的值。经过一些尝试和错误后,我能够 运行 该应用程序。这还不完整。需要重新加载应用程序更新。

在调整配置时,当我将 WSGIDaemonProcess testdjango python-path=/home/user/public_html/demo/django python-home=/home/user/public_html/demo/django/venv/lib/python3.6/site-packages 更改为 WSGIDaemonProcess testdjango python-path=/home/user/public_html/demo/django python-home=/home/user/public_html/demo/django/venv 时发生 内部服务器错误

来自 WSGIDaemonProcess 部分的 docs

python-home=directory Set the location of the Python virtual environment to be used by the daemon processes. The directory to use is that which sys.prefix is set to for the Python virtual environment. The virtual environment can have been created by virtualenv, pyvenv or python -m venv. Note that the Python virtual environment must have been created using the same base Python version as was used to compile the mod_wsgi module. You can’t use this to force mod_wsgi to somehow use a different Python version than it was compiled for. If you want to use a different version of Python, you will need to reinstall mod_wsgi, compiling it for the version you want. It is not possible for the one mod_wsgi instance to run applications for both Python 2 and 3 at the same time.

python-path=directory | python-path=directory:directory List of colon separated directories to add to the Python module search path, ie., sys.path. Note that this is not strictly the same as having set the PYTHONPATH environment variable when running normal command line Python. When this option is used, the directories are added by calling site.addsitedir(). As well as adding the directory to sys.path this function has the effect of opening and interpreting any .pth files located in the specified directories. If using a Python virtual environment, rather than use this option to refer to the site-packages directory of the Python virtual environment, you should use the python-home option to specify the root of the Python virtual environment instead. In all cases, if the directory contains Python packages which have C extension components, those packages must have been installed using the same base Python version as was used to compile the mod_wsgi module. You should not mix packages from different Python versions or installations.

环境:

  • Apache/2.4.43
  • mod_wsgi/4.7.1
  • Python/3.6
  • Django/3.0.8

Django 应用 运行s

的当前配置
    WSGIDaemonProcess ourdomain.com python-home=/home/user/public_html/demo/django/venv python-path=/home/user/public_html/demo/django/
    WSGIProcessGroup ourdomain.com

    Alias /demo/django/media /home/user/public_html/demo/django/media
    <Directory /home/user/public_html/demo/django/media>
        <IfVersion < 2.4>
            Order allow,deny
            Allow from all
        </IfVersion>
        <IfVersion >= 2.4>
            Require all granted
        </IfVersion>
    </Directory>

    Alias /demo/django/static /home/user/public_html/demo/django/static
    <Directory /home/user/public_html/demo/django/static>
        <IfVersion < 2.4>
            Order allow,deny
            Allow from all
        </IfVersion>
        <IfVersion >= 2.4>
            Require all granted
        </IfVersion>
    </Directory>

    <Directory /home/user/public_html/demo/django/testproject>
        <Files wsgi.py>
            <IfVersion < 2.4>
                Order allow,deny
                Allow from all
            </IfVersion>
            <IfVersion >= 2.4>
                Require all granted
            </IfVersion>
        </Files>
    </Directory>


    WSGIScriptAlias /demo/django /home/user/public_html/demo/django/testproject/wsgi.py

    <Directory /home/user/public_html/demo/django>
        <IfVersion < 2.4>
            Order allow,deny
            Allow from all
        </IfVersion>
        <IfVersion >= 2.4>
            Require all granted
        </IfVersion>
    </Directory>