How to install Django on Virtualmin Apache?

It’s a bit of a head ache, at least it was for me. The way I’ve done it was to install mod_wsgi and then through trial and error I followed Django’s docs here:

https://docs.djangoproject.com/en/4.1/howto/deployment/wsgi/modwsgi/

Once you get mod_wsgi installed, you edit the Apache configuration and add the following (this is from my current setup). Go to Services → Cnfigure (SSL) Website → Edit directives

Be aware, the code below contains paths you need to change according to your needs (the setup is from a subdomain I use for testing things out).

Alias /media/ /home/[[main.domain.name]]/domains/[[sub.domain.name]]/[[folder-containing-django-project]]/media/
Alias /static/ /home/[[main.domain.name]]/domains/[[sub.domain.name]]/[[folder-containing-django-project]]/static/

WSGIDaemonProcess [[sub.domain.name]] python-home=/home/[[main.domain.name]]/domains/[[sub.domain.name]]/[[venv-folder]] python-path=/home/[[main.domain.name]]/domains/[[sub.domain.name]]/[[folder-containing-django-project]]
WSGIProcessGroup [[sub.domain.name]]
WSGIPassAuthorization On

<Directory /home/[[main.domain.name]]/domains/[[sub.domain.name]]/[[folder-containing-django-project]]/static>
    Require all granted
</Directory>

<Directory /home/[[main.domain.name]]/domains/[[sub.domain.name]]/[[folder-containing-django-project]]/media>
    Require all granted
</Directory>

WSGIScriptAlias / /home/[[main.domain.name]]/domains/[[sub.domain.name]]/[[folder-containing-django-project]]/[[django-project-settings-folder]]/wsgi.py

<Directory /home/[[main.domain.name]]/domains/[[sub.domain.name]]/[[folder-containing-django-project]]/[[django-project-settings-folder]]>
    <Files wsgi.py>
        Require all granted
    </Files>
</Directory>

I think this will get you going. Also, if my memory serves right, you need to be careful what mod_wsgi you install i.e. you need to choose the one for Python3

1 Like