Apache + mod_wsgi - Python不加载已安装的模块

时间:2016-11-02 23:19:20

标签: python apache python-2.7 pip mod-wsgi

我有一个带有mod_wsgi的Apache服务器,运行Python 2.7脚本。 该脚本使用python Pillow模块,通过pip安装。

使用python script.py正常运行脚本可以正常工作,但是当从wsgi运行脚本时 - PIL会抛出ImportError异常。

这是来自/etc/apache2/sites-enabled/000-default.conf

的Apache配置
<VirtualHost *:80>
        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/

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

        WSGIScriptAlias /wsgi/ /home/nitay/Desktop/WebsitePath/Python/wsgi.py

        <Directory "/home/nitay/Desktop/WebsitePath/Python">
            Require all granted
        </Directory>
</VirtualHost>

没有安装virtualenv,这台机器上只安装了一个Python。

如何让python找到已安装的模块?

我已经看到围绕同一个球场使用mod_wsgi的daemon mode手动定义python路径的解决方案。有没有办法在嵌入模式下这样做?

编辑: Apache错误日志:

[Wed Nov 02 16:08:02.931400 2016] [wsgi:error] [pid 48202:tid 140100207392512] [client 192.168.1.179:29223] mod_wsgi (pid=48202): Target WSGI script '/home/nitay/Desktop/WebsitePath/Python/wsgi.py' cannot be loaded as Python module., referer: http://192.168.1.247/index.html
[Wed Nov 02 16:08:02.931475 2016] [wsgi:error] [pid 48202:tid 140100207392512] [client 192.168.1.179:29223] mod_wsgi (pid=48202): Exception occurred processing WSGI script '/home/nitay/Desktop/WebsitePath/Python/wsgi.py'., referer: http://192.168.1.247/index.html
[Wed Nov 02 16:08:02.931557 2016] [wsgi:error] [pid 48202:tid 140100207392512] [client 192.168.1.179:29223] Traceback (most recent call last):, referer: http://192.168.1.247/index.html
[Wed Nov 02 16:08:02.931601 2016] [wsgi:error] [pid 48202:tid 140100207392512] [client 192.168.1.179:29223]   File "/home/nitay/Desktop/WebsitePath/Python/wsgi.py", line 9, in <module>, referer: http://192.168.1.247/index.html
[Wed Nov 02 16:08:02.931687 2016] [wsgi:error] [pid 48202:tid 140100207392512] [client 192.168.1.179:29223]     import sprites, referer: http://192.168.1.247/index.html
[Wed Nov 02 16:08:02.931705 2016] [wsgi:error] [pid 48202:tid 140100207392512] [client 192.168.1.179:29223]   File "/home/nitay/Desktop/WebsitePath/Python/sprites.py", line 1, in <module>, referer: http://192.168.1.247/index.html
[Wed Nov 02 16:08:02.931767 2016] [wsgi:error] [pid 48202:tid 140100207392512] [client 192.168.1.179:29223]     from PIL import Image, referer: http://192.168.1.247/index.html
[Wed Nov 02 16:08:02.931830 2016] [wsgi:error] [pid 48202:tid 140100207392512] [client 192.168.1.179:29223] ImportError: No module named PIL, referer: http://192.168.1.247/index.html

sys.path&amp;普通Python和WSGI的版本:

Normal:
>>> sys.version
'2.7.11+ (default, Apr 17 2016, 14:00:29) \n[GCC 5.3.1 20160413]'
>>> sys.path
['', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-x86_64-linux-gnu', '/usr/lib/python2.7/lib-tk', '/usr/lib/python2.7/lib-old', '/usr/lib/python2.7/lib-dynload', '/home/nitay/.local/lib/python2.7/site-packages', '/usr/local/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages/gtk-2.0']

WSGI:
>>> sys.version
2.7.11+ (default, Apr 17 2016, 14:00:29) [GCC 5.3.1 20160413] 
>>> sys.path
['/usr/lib/python2.7', '/usr/lib/python2.7/plat-x86_64-linux-gnu', '/usr/lib/python2.7/lib-tk', '/usr/lib/python2.7/lib-old', '/usr/lib/python2.7/lib-dynload', '/usr/local/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages', '/usr/lib

3 个答案:

答案 0 :(得分:2)

我重新编写了服务器配置,这次正确命名,使用virtualenv和守护进程模式到wsgi。

这是我最终得到的apache配置:

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

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

        WSGIDaemonProcess sprites-toolbox python-path=/home/nitay/Desktop/SpritesToolbox/Python:/home/nitay/Desktop/SpritesToolbox/Python/sprite-toolbox-env/lib/python2.7/site-packages
        WSGIProcessGroup sprites-toolbox

        WSGIScriptAlias /wsgi/ /home/nitay/Desktop/SpritesToolbox/Python/wsgi.py


        <Directory "/home/nitay/Desktop/SpritesToolbox/Python">
            Require all granted
        </Directory>
</VirtualHost>

故事的道德? &#34;时间宝贵,明智地浪费时间&#34; (不要半屁股服务器配置)

答案 1 :(得分:1)

在我的情况下,

模块安装在用户环境中,而不是计算机的环境中。 我刚跑:

sudo -H pip3.7 install mako

-Hsudo说,将模块安装在运行命令的根计算机目录vs用户目录中。

这是因为Apache无法访问/读取个人用户的文件。

答案 2 :(得分:0)

我对此感到困惑,上面的讨论确实有帮助。然而,我的解决方案只是在我的WSGI程序开头设置Python路径,例如:

def application(environ, start_response):
    import sys
    path = "/usr/local/lib64/python2.7/site-packages/"
    if path not in sys.path: sys.path.append(path)