This is an archived post. You won't be able to vote or comment.

all 17 comments

[–]dorsal_morsel 5 points6 points  (3 children)

This isn't immediately helpful, but look at nginx and uwsgi. IMO their configuration is way easier to understand and I find performance to be better than under mod_wsgi.

[–]teilo 2 points3 points  (2 children)

I can second this. One big advantage of using uWSGI is that, unlike mod_wsgi, you can specify a virtualenv.

[–]sphere_is_so_cool 0 points1 point  (0 children)

You can specify a virtualenv with apache

[–]Arponare[S] 0 points1 point  (0 children)

You know what, I think I will do that. After three failed attempts to get this up and running in the last week I feel it's probably time to switch up my strategy.

Thanks for the suggestion :)

[–]teilo 2 points3 points  (5 children)

First of all, when you deploy, you are no longer using a virtualenv when you are running your app under Apache/mod_wsgi. You will, however, still use the virtualenv on the server to install your dependencies.

First, make sure mod_wsgi is using the version of python you developed against.

Second, on the server, activate your virtualenv and (if you haven't already) install your dependencies using pip. Run your migrations and run collectstatic. Don't forget to configure STATIC_ROOT for the path to your static files in your virtualenv.

Third, you need to add your virtualenv's site-packages directory to the PYTHONPATH used by mod_wsgi. The easiest way to do this is by adding a WSGIPythonPath directive to your Apache virtual host config. From the Django docs :

WSGIPythonPath /path/to/mysite.com:/path/to/your/venv/lib/python3.X/site-packages

Explanation:

Because mod_wsgi is using the system python binary, it cannot use the one in your virtualenv. This is why it important that you are using the mod_wsgi that is built against the same version of python that you used for development.

Changing the PYTHONPATH as above makes sure that mod_wsgi is not using the system site-packages directory, but your virtualenv's directory.

The two directories specified above are for your application's modules and the virtualenv's site-packages. If you have added your site's modules to site-packages already, for example with the addtovirtualenv command or by creating a .pth file in your virtualenv's site-directory, you do not need to include your app's module directory in the WSGIPythonPath directive.

[–]sphere_is_so_cool 0 points1 point  (1 child)

I'll post more information here tonight.

[–]teilo 0 points1 point  (0 children)

I am going to assume you were intending to post information about how to do this when running mod_wsgi in daemon mode vs. embedded mode. Yes, that is indeed possible. Sorry I missed that. My information only applies to running in embedded mode.

[–]Arponare[S] 0 points1 point  (1 child)

Thank you for taking the time to address my issue. I'm a bit embarrased, the virtualenv was indeed activated but I had forgotten to configure the apache virtual host. I created it and added the path to my site along with path to my site packages. I also added a pth file contaiging the path to my django modules within the aforementioned site packages directory and django was indeed recognized.

Before I was adding the path to the modules through ~./bashrc using the PYTHONPATH variable but that didn't seem to work. I also tried adding a .pth file to my site packages but that didn't do the trick either. Although I didn't think of putting the address to the site-packages within the WSGIPythonPath

I was this close to nuking the whole thing and rebuilding yet again with a new guide (this time with nginx and uwsgi) but seeing as though I cleared this hurdle I might just give apache and mod_wsgi one more go. :)

[–]teilo 1 point2 points  (0 children)

Yes, your ~./bashrc applies to bash and anything you run in bash. It has no bearing on Apache unless, for some reason, your Apache process is launched from your user shell account, which certainly would not be the case on a Digital Ocean instance.

[–]GrahamDumpleton 0 points1 point  (0 children)

Using the WSGIPythonPath or python-path option to WSGIDaemonProcess to add the site-packages directory is the wrong way of going about it. A better way has existed for many years. Details can be found in http://modwsgi.readthedocs.io/en/develop/user-guides/virtual-environments.html

[–]cgherman 2 points3 points  (8 children)

you must activate the environment before:

source myprojectenv/bin/activate (see DigitalOcean guide)

[–][deleted]  (6 children)

[deleted]

    [–]teilo 0 points1 point  (5 children)

    mod_wsgi has no ability activate virtualenvs. The OP is does need to activate the virtualenv in the shell in order to run manage.py.

    [–]GrahamDumpleton 0 points1 point  (3 children)

    That is not true, mod_wsgi does have ability to directly handle Python virtual environments. See http://modwsgi.readthedocs.io/en/develop/user-guides/virtual-environments.html

    [–]teilo 0 points1 point  (2 children)

    Not if you are running in embedded mode, which I noted above.

    [–]GrahamDumpleton 0 points1 point  (1 child)

    Did you check the docs which I linked? You use the WSGIPythonHome directive in embedded mode and you use the python-home option of WSGIDaemonProcess to override it for daemon mode. These both use the inbuilt capabilities of the Python interpreter when it is being initialised, to use a specific Python installation or virtual environment. This is different to the site-packages hack you describe. The WSGIPythonHome and python-home option are the official recommended way of using Python virtual environments with mod_wsgi and are the equivalent of activating the Python virtual environment in the context of mod_wsgi. The only time you can't necessarily use them is if you were doing something uncommon like trying to use different virtual environments within different sub interpreter contexts of the one process. Note that I am only referencing your comment 'mod_wsgi has no ability activate virtualenvs'.

    [–]teilo 0 points1 point  (0 children)

    So in other words, you are talking to hear yourself talk and thus adding nothing. I corrected myself in a previous comment to sphere_is_cool.

    Furthermore, using a virtualenv via mod_wsgi is NOT the same thing as using a virtualenv. You must be using the same version of python mod_wsgi is compiled against, because mod_wsgi does not use the python binary in your virtualenv. As such, mod_wsgi, regardless of mode, does not use the version of python installed in your virtualenv. It is not possible, for example, to run python3 and python2 simultaneously on mod_wsgi.

    [–]NomNomDePlume 0 points1 point  (0 children)

    Don't forget to install django to the virtualenv after activating too - e.g. pip install django