Apache + mod_wsgi - Python doesn't load instal

2019-07-09 04:49发布

I have an Apache server with mod_wsgi, running an Python 2.7 script. The script uses the python Pillow module, installed via pip.

Running the script normally using python script.py works okay, but when running the script from wsgi - An ImportError exception is thrown for PIL.

This is the Apache configuration from /etc/apache2/sites-enabled/000-default.conf:

<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>

There's no virtualenv installed, and there's just one Python installation on this machine.

What can I do to make python find its installed modules?

I've seen solutions around the same ballpark that use mod_wsgi's daemon mode to manually define python path. Is there a way to do so in embedded mode?

EDIT: Apache error log:

[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 & version for the normal Python and 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

2条回答
男人必须洒脱
2楼-- · 2019-07-09 05:27

I got stuck with this and the discussion above really helped. My solution however, was simply to set up the Python path at the beginning of my WSGI program, e.g.:

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)
查看更多
太酷不给撩
3楼-- · 2019-07-09 05:34

I redid the server configuration, this time naming things properly, used virtualenv, and Daemon mode to wsgi.

Here's the apache configuration I ended up with:

<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>

Moral of the story? "Time is precious, waste it wisely" (Don't half-ass server configurations)

查看更多
登录 后发表回答