Installing Apache mod_wsgi for django

Posted under » Apache » Django on 23 April 2021

After installing Apache Python mod_wsgi we have to adjust the settings if you want to run django.

By the way if you want to use NGINX as your server, use uwsgi instead.

Apparently by default mod-wsgi was for python2 on older versions of ubuntu. So you might have to remove it. Replace it with the version for python 3 if it is not installed.

$ sudo apt-get remove libapache2-mod-python libapache2-mod-wsgi
$ sudo apt-get install libapache2-mod-wsgi-py3

With that out of the way, now lets setup the apache.

<VirtualHost *:80>
ServerName student.com
        ServerAdmin webmaster@localhost
        DocumentRoot /www/student/alsstudent

WSGIScriptAlias / /www/student/alsstudent/wsgi.py
WSGIDaemonProcess deyp python-path=/www/student:/venv/bri
WSGIProcessGroup deyp

Alias /static/ /var/www/student/static/

    <Directory /var/www/student/static/>
     Require all granted
    </Directory>

<Directory /www/student/alsstudent/>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
        ErrorLog ${APACHE_LOG_DIR}/error-student.log
        CustomLog ${APACHE_LOG_DIR}/access-student.log combined
</VirtualHost>

WSGI uses process and in this case, I called it deyp.

Python django path has two parts. The first is where you created the django site. 2nd is the where your python venv is.

For more info on static files.

Now edit the wsgi.py with similar info. as above to the following.

import os, sys
sys.path.append('/www/student')

sys.path.append('/venv/bri/lib/python3.10/site-packages')

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'alsstudent.settings')

from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

Now edit the settings.py to the following.

ALLOWED_HOSTS = ['student.com']

You have to restart your apache webserver and you will see the success page.

web security linux ubuntu python django git Raspberry apache mysql php drupal cake javascript css AWS data