尝试通过 Apache 连接到数据库时 Django 应用挂起

Django app hangs when attempting to connect to database via Apache

我很难解决这个问题。我在 Ubuntu 14.04 服务器上有一个 Django 应用程序 运行(Apache 2.4 和 mod_wsgi 用于 Python 3.4)。它通过 pymssql 连接到 SQL 服务器。

在开发中,该应用运行良好。我查询了数据库,和数据库returns预期的结果。

但是,在生产环境中(在 Apache 用户下),脚本会在进行数据库查询的那一刻挂起。我的浏览器(Chrome 或 Firefox)显示一个旋转轮,只要浏览器 window 打开,它就会继续旋转。

我的 apache2.conf 文件中有以下内容:

ServerName localhost
# WSGIDaemonProcess application
WSGIPythonPath /home/production_code/python3env/lib/python3.4/site-packages:/home/production_code/school
# WSGIProcessGroup application
WSGIScriptAlias / /home/production_code/school/school/wsgi.py
# Python virtualenv home
WSGIPythonHome /home/production_code/python3env
# Include the virtual host configurations:
IncludeOptional sites-enabled/*.conf

以及我的 sites-enabled/000-default.conf 文件中的以下内容:

<VirtualHost *:80>    
        ServerAdmin *****@school.edu
        ServerName  localhost
        DocumentRoot /var/www/html

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

        Alias /static/ /home/production_code/school/static/

        <Directory /home/production_code/school/>
            Require all granted
        </Directory>
        <Directory /home/production_code/school/>
            <Files wsgi.py>
                Require all granted
            </Files>
        </Directory>
        <Directory /home/production_code/school/static>
            Require all granted
        </Directory>
</VirtualHost>

有谁知道是什么原因造成的,或者我该如何解决这个问题? Apache 错误日志和访问日志在这种情况下并不是特别有用,因为永远不会呈现对请求的响应。同样,Django调试在这里也没有用。

而不是:

# WSGIDaemonProcess application
WSGIPythonPath /home/production_code/python3env/lib/python3.4/site-packages:/home/production_code/school
# WSGIProcessGroup application

使用:

WSGIDaemonProcess application python-path=/home/production_code/python3env/lib/python3.4/site-packages:/home/production_code/school
WSGIProcessGroup application
WSGIApplicationGroup %{GLOBAL}

其中的关键部分是 WSGIApplicationGroup 指令,它被设置为 %{GLOBAL}

这是为了绕过 Python 的有缺陷的第三方扩展模块,这些模块在子解释器中不起作用,并且可能因死锁或崩溃而失败。

参见:

还建议您返回使用守护程序模式。使用嵌入式模式通常不是一个好主意。