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

Apparently by default mod-wsgi was for python2. So you have to remove it and replace it with the version for python 3.

$ 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/alsstudent/alsstudent

WSGIScriptAlias / /www/alsstudent/alsstudent/wsgi.py
WSGIDaemonProcess deyp python-path=/www/alsstudent:/www/python/student_als
WSGIProcessGroup deyp

Alias /static/ /var/www/alsstudent/static/

    <Directory /var/www/alsstudent/static/>
        Order allow,deny
        Allow from all
    </Directory>

<Directory /www/alsstudent/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 to the following.

import os, sys

sys.path.append('/www/python/student_als/lib/python3.8/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