Deploy Django app on Shared Hosting using CPanel

This post is about deploying a Django application on Linux shared hosting using Cpanel. I will be guiding you step by step in the process of deploying a new or an existing Django app on shared hosting.

Why Shared Hosting?

When it comes to deploying Django applications, you have many options available. Deploying on shared hosting might not be the best option overall but it surely has some advantages. The most prominent advantage is cost. Popular options like pythonanywhere or heroku 5$ to 7$ a month on the basic plan which could limit the RAM usage at 512MB and disk usage at 1GB. However, you can get a shared hosting which gives you 1 GB RAM and 20GB SSD for as less as 1.44$ a month. In addition to this, if you already have a website on shared hosting, you do not need to spend anything at all.

Check if your hosting is compatible

To follow this tutorial, your shared hosting must have following features:

  • Linux Hosting
  • Access to CPanel
  • Support for python apps
  • Command-Line access to the server

Most of the shared hostings are Linux hostings. You can check your hosting specifications from whichever the hosting company you bought it.

To check if your hosting supports python, log in to your CPanel and look for the option “Setup Python App” marked in the following screenshot. If you can find it, you are good to go.

To check if you have command line access to your server. Try to find “SSH Access” or “Terminal” options in your Cpanel.

If you are having any confusion in checking your hosting for compatibility, you can contact your hosting support. Good hosting providers have good support always ready to assist you on any issue.

If your hosting is compatible, then great!. You can follow this tutorial.

If your hosting is not compatible or you don’t have a hosting yet, you will have to purchase a shared hosting. Make sure to check the four features in the above list when buying a shared hosting for Django. It is always better to contact the customer support of the hosting and ask them the questions. In this way, you will also test how good their support is. Remember! “A good hosting has a good support”. The best way to contact is via live chat.

Let me mention here the shared hosting that I am using. I am using the NameCheap shared hosting, the stellar plan. I can recommend this to you because I am satisfied with their service. At the time I am writing this post, it will cost you $17.28 for one year or $ 1.44 for one month_ depending on whether you pay monthly or yearly. I recommend the yearly payment because you will save much money that way. The prices keep on varying, sometimes there is a special discount, so you can see the current pricing here.

You will get a free .website domain with the hosting so you will not have to purchase a domain separately if you want to do testing. If you buy a domain in the future, you can attach that too without a problem. With the cheapest plan, you will get 1GB RAM and 20GB SSD. The backend Django app for this blog’s app is also hosted on this hosting.

If you plan to buy a shared hosting for deploying Django, consider using my affiliate link. It will not increase the price for you but I will get a commission from whatever you buy in your first order.

Deploy a Django App

Get Command-Line Access

You can get command-line access to your hosting server in two ways: using SSH or via an online terminal. If you want to work via SSH, read your hosting provider’s documentation on how to do that. I will be using the online terminal because it is less complicated to use.

In your CPanel, open the Manage Shell option.

Turn on Enable SSH access

Now you will see the Terminal option in CPanel. Open it to check if it is working.

If a terminal like shown in the below screenshot opens, you have got the terminal access to your server. Keep this browser tab open. We will need it in the upcoming steps.

Setup a new Python app

In your CPanel, open Setup Python App

Click on the Create Application button.

  • Set the Python Version to 3.7.3
  • The Application Root is the folder in your hosting where the python application will be created. This will be the folder where you will start or upload your Django project.
  • In the Application URL, select the domain/subdomain and sub-directory where you want your Django application to be live. This will be the web address where you will see your Django app.
  • In the Application startup file, type
passenger_wsgi.py
  • In the Application entry point, type
application

You can also see the settings in the screenshot below. After you are done, click on Create.

Open the URL where you set up your python app. You should see a page like this.

Install Django

After your python app is set up, you will see a page as shown in the screenshot below. Copy the command to enter the virtual environment.

Enter the command into the online terminal by Right Click / Paste and press enter. You will enter the python virtual environment. In my case, the command is

source /home/umercxio/virtualenv/django/3.7/bin/activate && cd /home/umercxio/django

It will not be the same for you. If there is no “&&cd …” part in your command, you will manually have to cd to the directory where you have set up your application.

Now you have to install Django. Do not install the latest version of Django. It will not work properly on shared hosting. I had problems with it after which the hosting support told me to use version 2.1. Install version 2.1 of Django by running the following command.

pip install django==2.1

If any other modules are required for your project, install them here using pip.

Note: If you are facing an error with SSL when trying to use pip, use python version 3.6 instead of 3.7. This will solve the problem and you will be able to use pip.

Confirm the Django installation by running the following command.

django-admin --version

Start a Django project

Create a new project

Go to the terminal in your Cpanel and enter the following command

django-admin startproject myapp ~/django

Make sure to replace “myapp” with your application name and “django” at the end with the directory name where you set up your python app.

Now open the file manager in CPanel (You can also use FTP) and go the folder where you set up your Django app. In my case, it is the “django” folder. You will see a folder with your app’s name and the manage.py file here.

Edit the passenger_wsgi.py file.

Delete everything in the file and add just one line.

from myapp.wsgi import application

Do not forget to replace “myapp” with your application name. Click on Save Changes to save the changes to the file.

Now open your application folder_ in my case, it is “myapp”.

Edit the settings.py file.

In the ALLOWED_HOSTS list, add the URL where our application will be running (which you provided while setting up the python app). In my case, it is “test.umer.link”. If your site has a www variant, make sure to add that here too if you want your site to be working in www as well.

Now go to Set up Python app in your CPanel and Restart your application.

Now open the URL where you have set up your application and you should see the Django welcome page.

Upload an existing project

Upload your project into the folder where you have set up the python app.

Edit the passenger_wsgi.py file

Delete everything and add the following code:

from app.wsgi import application

Make sure to replace “app” with your own app name.

Edit your project’s settings.py file and add your application URL to the ALLOWED_HOSTS list. If your domain also has a www variant, make sure to add that too in the list.

Set up the database

In this part, we will set up a MySQL database from the hosting with Django.

Go to the MySQL Databases option in CPanel.

Create a new Database

Create a new User. Copy the password of this user somewhere because we will need it in the upcoming steps.

Add the user to the database

Allow all the privileges to this user.

Go to the terminal again. Enter the virtual environment and run the following command to install mysql with pip.

pip install pymysql

Edit your settings.py file. Replace the default database code with this.

 'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'databasename',
        'USER': 'databaseusername',
        'PASSWORD': 'databasepassword',
        'HOST': 'localhost',
        'PORT': '3306',
    }

Make sure to replace the databasename with the database name, databaseusername with the user name and databasepassword with the password for the user.

Now edit the __init__.py file in the app directory.

Add the following code to the file and save it.

import pymysql

pymysql.install_as_MySQLdb()

Apply migrations to the database.

python manage.py makemigrations
python manage.py migrate

Create a superuser now if you want to access the admin panel. This step is optional.

python manage.py createsuperuser

Set up static files

Edit your settings.py file and add the following two lines at the end.

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')

Now open the terminal and run the following command.

python manage.py collectstatic

The static files will be copied to a folder named “static” inside your Django app folder. You have to copy this folder to your domain root.

If you have setup Django app on a subdomain like me, the domain root folder will be a folder in your hosting. If you have set up on the main domain, it will be the “public_html” folder.

Go to the domain root folder and check if the static folder has been successfully copied.

Go to “Setup Python App” in CPanel and Restart the app.

Test the app

Now go to http://yourappurl/admin and log in using the superuser details you created previously.

If you see the admin panel, congratulations! your Django app has been deployed on your shared hosting.

Deploy in 5 minutes

After you have followed this tutorial successfully and practiced deploying an existing application twice, you can deploy the Django app to your shared hosting in under 5 minutes using the pdf handbook that I have created. It just highlights the steps that you have to go through in the process of deploying your application.

I hope this post was helpful. If you have any problem, feel free to ask in the comments.

Comments

  1. Mike

    Thank you! This was really very helpful. I just wish I would have seen it a few days ago. It would have saved several fruitless support communications and my meandering discoveries. All the information I have needed so far is here for implementing a django app on a shared host.

    1. Post
      Author
      Hafiz Muhammad Umer

      Please contact me using the contact page of the website to discuss the problem you are facing with the media files so that we can solve the problem and I will update the post accordingly.

  2. usama

    pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available.
    Collecting django==2.1
    Cache entry deserialization failed, entry ignored
    Could not fetch URL https://pypi.python.org/simple/django/: There was a problem confirming the ssl certificate: Can’t connect to HTTPS URL because the SSL module is not available. – skipping
    Could not find a version that satisfies the requirement django==2.1 (from versions: )
    No matching distribution found for django==2.1

    getting this error

    1. Post
      Author
      Hafiz Muhammad Umer

      This problem never existed before but it is occurring now. The solution is to use python 3.6 instead of 3.7. I have also updated this in the post.

      1. Post
        Author
        Hafiz Muhammad Umer

        I contacted the NameCheap support about the problem and it is fixed now on their servers. Python 3.7 will work fine now.

    1. Post
      Author

Leave a Reply

Your email address will not be published. Required fields are marked *