Save 50% for your first year of web hosting!

How to setup Python Django application on shared hosting

In this tutorial I will show you how to setup a Python Django application that starts by passenger whenever someone visits the site.

Step 1: Setup document root

In this example we’ll use the main domain, site.tld but you can use a subdomain if you prefer that. The important part is that the structure is correct. Passenger will expect to find the document root in /public and the site files in /.

For this step we’ll want to head on over to the terminal. You may access it via Dev > Terminal or via SSH by using the login details sent to you in the “Get started with your web host” email.

Once there we’ll first create the correct document root.

cd /var/www && mv html html-django && mkdir html-django/public && ln -s html-django/public html

Step 2: Install a python version of your choice

First find out what python version you’d like to use. In my case I chose 3.10 but you can choose any version you like.

Run the following command to install the python version

pyenv install 3.10

It can take a few minutes for it to compile and install python.

Step 3: Create all the necessary files.

First navigate one level below the document root of your site.

cd /var/www/html-django

Next specify the python version you’d like to use

pyenv local 3.10

Step 4: Install Django and create the passenger file

To install django run the following command

pip3 install django

Now when Django is installed lets create a test application to make sure everything works fine.

django-admin startproject myproject .
python3 manage.py migrate

After this let’s create an admin account

python3 manage.py createsuperuser

Make sure to save the username and password.

After this we need to edit the settings.py file to add the domain to the allow

Open myproject/settings.py with an editor of your choice and make sure that ALLOWED_HOSTS = [] contains something like this

ALLOWED_HOSTS = ['site.tld']

Great now we’re done! While you can start Django directly at a port that’s allocated to your account with python3 manage.py runserver 0.0.0.0:41411 command, it’s best to setup passenger. This is because if the server should restart or something else happens you don’t have to keep on starting the Django server all the time, passenger does that for you!

Create a Passenger startup file to run Django as a Python WSGI application:

touch passenger_wsgi.py

Edit the file with the editor of your choice and make sure it has the following content:

import myproject.wsgi
application = myproject.wsgi.application

If the project name is different from myproject make sure to replace it to match your project name.

Step 5: Create the .htaccess file to start passenger

The last thing to do is to create the .htaccess file that will start passenger.

First navigate to your document root and create a file called .htaccess

cd /var/www/html-django/public && touch .htaccess

Next edit the file with the editor of your choice and make sure it has the following content:

PassengerAppRoot /var/www/html-django
PassengerPython /usr/local/share/python/pyenv/shims/python
PassengerEnabled on

The last thing to do is delete the template index.html file that was generated.

rm /var/www/html-django/public/index.html

That’s it. We’re done.