Search code examples
pythondjangoapachemod-wsgidjango-deployment

Deploying django3 project with mod_wsgi


I'm experimenting to publish my django project with Apache mod_wsgi. I created a simple django3 project named sampleapp in virtualenv. Then, I configured my /etc/apache2/sites-enabled/000-default.conf file as follows

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

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

WSGIDaemonProcess sampleapp python-home=/var/www/html/sampleapp/env python-path=/var/www/html/sampleapp:/var/www/html/sampleapp/env/lib/python3.7/site-packages
WSGIProcessGroup sampleapp
WSGIScriptAlias /sampleapp /var/www/html/sampleapp/sampleapp/wsgi.py process-group=sampleapp

</VirtualHost>

Although my virtualenv's python version is 3.7, mod_wsgi is used system's python version(3.5), so it doesn't see my virtualenv and doesn't use its python version. My apache error log as follows:

[Sat Feb 20 15:36:02.390188 2021] [mpm_prefork:notice] [pid 20090] AH00169: caught SIGTERM, shutting down
[Sat Feb 20 15:36:03.226065 2021] [mpm_prefork:notice] [pid 20204] AH00163: Apache/2.4.18 (Ubuntu) mod_wsgi/4.3.0 Python/3.5.2 configured -- resuming normal operations
[Sat Feb 20 15:36:03.226278 2021] [core:notice] [pid 20204] AH00094: Command line: '/usr/sbin/apache2'
[Sat Feb 20 15:36:03.294321 2021] [wsgi:error] [pid 20207] mod_wsgi (pid=20207): Call to 'site.addsitedir()' failed for '(null)', stopping.
[Sat Feb 20 15:36:03.295568 2021] [wsgi:error] [pid 20207] mod_wsgi (pid=20207): Call to 'site.addsitedir()' failed for '/var/www/html/sampleapp/env/lib/python3.7/site-packages'.
[Sat Feb 20 15:36:07.084368 2021] [wsgi:error] [pid 20207] mod_wsgi (pid=20207): Call to 'site.addsitedir()' failed for '(null)', stopping.
[Sat Feb 20 15:36:07.084455 2021] [wsgi:error] [pid 20207] mod_wsgi (pid=20207): Call to 'site.addsitedir()' failed for '/var/www/html/sampleapp/env/lib/python3.7/site-packages'.
[Sat Feb 20 15:36:07.085660 2021] [wsgi:error] [pid 20207] [remote 127.0.0.1:47028] mod_wsgi (pid=20207): Target WSGI script '/var/www/html/sampleapp/sampleapp/wsgi.py' cannot be loaded as Python module.
[Sat Feb 20 15:36:07.085769 2021] [wsgi:error] [pid 20207] [remote 127.0.0.1:47028] mod_wsgi (pid=20207): Exception occurred processing WSGI script '/var/www/html/sampleapp/sampleapp/wsgi.py'.
[Sat Feb 20 15:36:07.086188 2021] [wsgi:error] [pid 20207] [remote 127.0.0.1:47028] Traceback (most recent call last):
[Sat Feb 20 15:36:07.086266 2021] [wsgi:error] [pid 20207] [remote 127.0.0.1:47028]   File "/var/www/html/sampleapp/sampleapp/wsgi.py", line 12, in <module>
[Sat Feb 20 15:36:07.086280 2021] [wsgi:error] [pid 20207] [remote 127.0.0.1:47028]     from django.core.wsgi import get_wsgi_application
[Sat Feb 20 15:36:07.086321 2021] [wsgi:error] [pid 20207] [remote 127.0.0.1:47028] ImportError: No module named 'django'

How do I configure mod_wsgi to use the python version of my venv? Or where is my fault? Thanks for the answers.


Solution

  • So the mod_wsgi is compiled to a specific version of Python for Apache. You cannot make it run a different version of python. You can either rebuild that mod_wsgi to the correct version, or change the virtual environment. This deletes virtualenv wrapper but keeps project files.

    I think that "changing the virtualenv" would be easier.

    rmvirtualenv env-name
    mkvirtualenv -p python3.5 env-name
    setvirtualenvproject env-name
    

    When you use 'mkvirtualenv' use the -r tag for a requirements.txt

    Or rebuild the mod_wsgi:

    Documentation