How to install Django on Virtualmin Apache?

SYSTEM INFORMATION
OS type and version Ubuntu 20
Virtualmin version 7.1

I searched whole Internet but unable to find a single tutorial to host and run Django site on Virtualmin. There should be atleast one proper guide to install Django site.

Please help. :frowning:

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

This is the process I was looking for. BTW, how do you activate wsgi mod ? I installed via ssh.

Just restart Apache(you should have a refresh rype icon at the top right on Configure (SSL) Website

I tried on both conf file but got no luck.
Do I have to configure the code you provided on my domain’s conf file or the main apache2.conf file ?

And do I have to run the script manually like this ?
Starting development server at http://127.0.0.1:8000/
I mean manage.py runserver from ssh ?

This is what I am writing on apache2.conf file of my server :

WSGIPythonHome /home/username/public_html/imtenv
WSGIPythonPath /home/username/public_html/
WSGIDaemonProcess username.in python-home=/home/username/public_html/imtenv python-path=/home/username/public_html/
WSGIProcessGroup username.in
<Directory /home/username/public_html/>
    <Files wsgi.py>
        Require all granted
    </Files>
</Directory>

You should do it on your domain conf file, and I think just in one place i.e. either ssl website or regular website.

You should check your apche error log and look for python errors. You’ll certainly see something if mod_wsgi is working at all. Btw, did you configure wsgi.py and Django according to deployment documents?

Make sure mod_wsgi is installed and working.

Yes its working.

OK. Thank you!

I did this according to docs :
WSGIScriptAlias / /home/username/public_html/scripts/wsgi.py

One thing I noticed, you shouldn’t host your python files in public_html. That’s a very bad practice.

What I’m doing (and I’m not saying you should necessarily do it) is two folders above public_html. One tor the environment and a separate one for the code.

Normally, eveything Django should live above public_html. The settings in apache makes it work as it should.

Other than that, I’m not sure what else I can say…

1 Like

Hi. Can you share your .conf files(domain conf, file and wsgy.py) please ? I am having some issues in configuration.
Please share your file by changing all your personal stuffs names. :frowning:

I’m officially on holiday so not anywhere near a computer. The code I pasted initially is literally my domain config regarding mod_wsgi so I’d say keep looking at it and keep trying. I can assure you that domain config works and not on just one site. If in three weeks you don’t get it sorted, leave a line here and I’ll try to help as best as I can.

2 Likes

Hi. Thank you for your kind words and quick helps. With your help, I successfully installed Django on virtualmin. But after making migrations, my app failed to run and showing did you install mysql error. Even I logged in with my user password which is stored in mysql database. So I deleted the project and reinstalled a new project and configured same step and got same mysql install error.
now I decided to ditch the idea and now looking for nodejs deployment.

Thank you for your help. Enjoy your Holidays. :slight_smile:

This topic was automatically closed 8 days after the last reply. New replies are no longer allowed.