1

I have some small django projects working on the same server with apache and mod_wsgi. I installed another one project with virtualenvironment and set settings for it in Apache conf file:

<VirtualHost XXX.XXX.XXX.XXX:81 >
    ServerName mywebsite.com
    CustomLog /var/www/httpd-logs/mywebsite.com.access.log combined
    DocumentRoot /var/www/empirik/data/www/mywebsite.com
    ErrorLog /var/www/httpd-logs/mywebsite.com.error.log
    ServerAlias www.mywebsite.com
    SuexecUserGroup empirik empirik

    WSGIScriptAlias / /var/www/empirik/data/www/mywebsite.com/myproject/wsgi.py
    WSGIDaemonProcess mywebsite.com python-path=/var/www/empirik/data/www/mywebsite.com/env/lib/python2.7/site-packages

    <Directory /var/www/empirik/data/www/mywebsite.com/myproject>
        Order deny,allow
        Allow from All
    </Directory>

    <Directory /var/www/empirik/data/www/mywebsite.com/myproject/static>
        Order deny,allow
        Allow from all
    </Directory>
</VirtualHost>

When I try to load website server returns 500 error or another website's content in about 50% times and there are some strange errors in apache log files that I can't understand because it tries to load another website's project settings:

[Wed May 22 10:18:47 2013] [error] [client 66.249.78.234] mod_wsgi (pid=32361): Exception occurred processing WSGI script '/var/www/empirik/data/www/mywebsite.com/myproject/wsgi.py'.
[Wed May 22 10:18:47 2013] [error] [client 66.249.78.234] Traceback (most recent call last):
[Wed May 22 10:18:47 2013] [error] [client 66.249.78.234]   File "/usr/local/lib/python2.7/site-packages/django/core/handlers/wsgi.py", line 219, in __call__
[Wed May 22 10:18:47 2013] [error] [client 66.249.78.234]     self.load_middleware()
[Wed May 22 10:18:47 2013] [error] [client 66.249.78.234]   File "/usr/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 39, in load_middleware
[Wed May 22 10:18:47 2013] [error] [client 66.249.78.234]     for middleware_path in settings.MIDDLEWARE_CLASSES:
[Wed May 22 10:18:47 2013] [error] [client 66.249.78.234]   File "/usr/local/lib/python2.7/site-packages/django/utils/functional.py", line 184, in inner
[Wed May 22 10:18:47 2013] [error] [client 66.249.78.234]     self._setup()
[Wed May 22 10:18:47 2013] [error] [client 66.249.78.234]   File "/usr/local/lib/python2.7/site-packages/django/conf/__init__.py", line 42, in _setup
[Wed May 22 10:18:47 2013] [error] [client 66.249.78.234]     self._wrapped = Settings(settings_module)
[Wed May 22 10:18:47 2013] [error] [client 66.249.78.234]   File "/usr/local/lib/python2.7/site-packages/django/conf/__init__.py", line 95, in __init__
[Wed May 22 10:18:47 2013] [error] [client 66.249.78.234]     raise ImportError("Could not import settings '%s' (Is it on sys.path?): %s" % (self.SETTINGS_MODULE, e))
[Wed May 22 10:18:47 2013] [error] [client 66.249.78.234] ImportError: Could not import settings 'anotherproject.settings' (Is it on sys.path?): No module named anotherproject.settings

Another question is why it still uses default system python but not from virtualenv? Please help!

Dmitrii Mikhailov
  • 5,053
  • 7
  • 43
  • 69

2 Answers2

1

You cannot use different python executeables under the same apache due to the fact that WSGIPythonHome is set for all virtual hosts globally (see docs). If WSGIPythonHome is not set - mod_wsgi will use system python.

It seems like you have not set up wsgi.py correctly. Your goal in wsgi.py is to manipulate sys.path correctly so that all that your project needs is available. Here's what helped me when I had same problems: apache server not using proper virtualenv with WSGI setting.

Hope that helps.

Community
  • 1
  • 1
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
  • Thanks, specifically for info about WSGIPythonHome. I didn't know about it. I made some changes and now it looks like everything works fine though I still have no idea what could cause my problems because paths in the wsgi file seemed right. – Dmitrii Mikhailov May 22 '13 at 17:09
1

Your configuration is missing:

WSGIProcessGroup mywebsite.com

Without that, your WSGI application isn't even going to be delegated to the daemon process group. You can check with:

Once you have daemon mode being used properly, so long as you are using mod_wsgi 3.4, you can set Python home for that specific daemon process group using:

WSGIDaemonProcess mywebsite.com python-home=/var/www/empirik/data/www/mywebsite.com/env

That way you can simply point at the root of the Python virtual environment and it will be picked up.

You still need though to specify the python-path option as well to the parent directory of your Django project so it and the settings module can be found, which is the problem you are having. Thus likely you want:

WSGIDaemonProcess mywebsite.com \
    python-home=/var/www/empirik/data/www/mywebsite.com/env \
    python-path=/var/www/empirik/data/www/mywebsite.com

Now if running only that site in the daemon process group, set:

WSGIApplicationGroup %{GLOBAL}

to avoid issues with C extension modules that don't work in sub interpreters.

To be safer, instead of:

<Directory /var/www/empirik/data/www/mywebsite.com/myproject>
    Order deny,allow
    Allow from All
</Directory>

you should use:

<Directory /var/www/empirik/data/www/mywebsite.com/myproject>
<Files wsgi.py>
    Order deny,allow
    Allow from All
</Files>
</Directory>

That way if screw up Apache configuration, less risk of someone downloading your code and settings file.

And where you have:

<Directory /var/www/empirik/data/www/mywebsite.com/myproject/static>
    Order deny,allow
    Allow from all
</Directory>

you would appear to be missing the corresponding:

Alias /media /var/www/empirik/data/www/mywebsite.com/myproject/static

If you don't have an Alias for static media directory, Apache will not serve up files there.

Finally, since you want to force daemon mode and want to avoid embedded mode, set:

WSGIRestrictedEmbedded On

That way if you screw up configuration and things run in embedded mode by mistake as you currently are, you will get an error.

Graham Dumpleton
  • 57,726
  • 6
  • 119
  • 134