python – 使用WSGIDaemonProcess的django apache配置不起作用

更新的问题

[Mon Jul 18 09:20:10.517873 2016] [:error] [pid 30316:tid 139756302964480] [remote 122.164.94.99:48261] Traceback (most recent call last):
[Mon Jul 18 09:20:10.518005 2016] [:error] [pid 30316:tid 139756302964480] [remote 122.164.94.99:48261]   File "/var/www/rent/Rent/wsgi.py", line 20, in <module>
[Mon Jul 18 09:20:10.518141 2016] [:error] [pid 30316:tid 139756302964480] [remote 122.164.94.99:48261]     from django.core.wsgi import get_wsgi_application
[Mon Jul 18 09:20:10.518236 2016] [:error] [pid 30316:tid 139756302964480] [remote 122.164.94.99:48261] ImportError: No module named django.core.wsgi

我的虚拟主机

<VirtualHost *:80>
    ServerName  ip_address
    ServerAdmin webmaster@localhost

    Alias /static/  /var/www/rent/static/

    Alias /media/  /var/www/rent/media/

    WSGIScriptAlias /   /var/www/rent/Rent/wsgi.py

    WSGIDaemonProcess   Rent  python-path=/var/www/rent:/root/.virtualenvs/rent/lib/python2.7/site-packages

    WSGIProcessGroup    Rent

    <Directory /var/www/rent/static>
        Options -Indexes
        Order deny,allow
        Allow from all
    </Directory>

    <Directory /var/www/rent/media>
        Options -Indexes
        Order deny,allow
        Allow from all
    </Directory>

    LogLevel warn

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

解决方法:

WSGIDaemonProcess   Rent  python-path=/var/www/rent:/root/.virtualenvs/rent/lib/python2.7/site-packages

这是问题的最可能原因.您已在超级用户的主文件夹中创建了virtualenv.但该文件夹不太可能被apache访问.默认情况下,任何其他用户都无法访问用户的主文件夹.

Web服务器和WSGI进程将作为非特权用户运行,通常名为nobody,httpd,apache或类似的东西.虽然您可以通过更改/ root /上的权限来解决此问题,这是一个很大的不.如果它是一个普通用户,那就不那么危险了,但仍然不是一个好主意.

最好的解决方案是将virtualenv放在非特权用户可访问的位置. /usr/local/virtualenv是个好位置.

请注意,将/root/.virtualenvs/移至/usr/local/virtualenv您必须重新创建如下

 source /root/.virtualenvs/rent/bin/activate
 pip freeze > /tmp/requirements.txt
 cd /usr/local/
 virtualenv virtualenv
 source virtualenv/bin/activate
 pip install -r /tmp/requirements.txt

然后编辑httpd.conf文件以反映新路径.

上一篇:初识WSGI接口


下一篇:Flask面试问题