How to deploy a Django application on Shared Servers

Django is an open-source Python framework that allows you to build web applications. It has both front-end and back-end capabilities, which help develop projects from scratch.

In this article, we will take a look at deploying a simple Django application on Shared servers.

The process of deployment consists of a few steps:

Let’s take a look at each of these steps:


Create a Python application

Since Django requires Python to operate, we first need to set up a Python environment on our shared hosting.

  1. Go to cPanel >> the “Setup Python App” menu:

    In our example, it will look like this:



  2. Inside the menu, click on the “Create Application” button:

    In our example, it will look like this:



  3. In the application form, you’ll be asked to fill in the following details:

    • Python version: set the preferred Python version. In this article, we use version 3.8.20.
    • Application root: specify the directory for your application. We use django_app in this example.
    • Application URL: select a domain or subdomain for the application. We will use django.nctest.info as an example.
    • Leave the Application startup file text box and Application Entry point text box blank.

    Once done, click the Create button in the top-right corner:

    In our example, it will look like this:



  4. After the application is created, a command will appear in a blue box at the top of the page. Save this command, as you will need it later to access the Python virtual environment:

    In our example, it will look like this:



Configuring a Django project

Once the Python application is set up, you need to install the Django module and then create and configure the Django application.

To do that, follow these steps:

  1. Log in to the account via SSH. For more details, refer to the SSH access guide. You can also do it using the “Terminal” menu of your cPanel account:



    Note: Make sure that SSH access is enabled for your account.

  2. Use the command you saved in step 5 to enter the Python virtual environment. For example:

    source /home/nctests/virtualenv/django_test/3.8/bin/activate && cd /home/nctests/django_test



  3. After entering the Python virtual environment, run the following command to install Django:

    pip install django



  4. Do not close an SSH client or disconnect, since we will need to work with SSH later.

    NOTE 1: By default, pip install django installs the latest available Django version. To install a specific version, use the following command (replace version with the desired version number):

    pip install django==version

    NOTE 2: Django 4.2 is compatible with Python 3.8, 3.9, 3.10, 3.11, and 3.12. If you want to use an older version of Python, refer to the Django–Python compatibility table below.

    Module/Python version2.7.183.3.73.4.93.5.93.6.15/3.7.16
    Django1.11.181.11.292.0.132.2.283.2.18

    NOTE 3: To check the installed Django version, run the following command:

    django-admin --version



  5. Now open cPanel > File Manager and locate the directory that you set as the Application root in step 3 previously during the Python application creation.

  6. You can upload files of your Django application here and extract them.

  7. Open the passenger_wsgi.py file and replace the content there with the following:

    import myproject.wsgi application = myproject.wsgi.application



    where myproject should be replaced with your Django project name.

  8. Now, we need to go back to our SSH client or cPanel > the “Terminal” menu and run the following command:

    python manage.py migrate



  9. After that, please open your settings.py file, locate the ALLOWED_HOSTS = [] line, and specify your domain or subdomain that you set as Application URL in step 3 previously during Python application creation. In this example, we will edit it as follows:

    ALLOWED_HOSTS = [‘django.nctest.info’]



  10. Once it is done, restart the application from the “Setup Python App” menu:



  11. The final step is to open the domain or subdomain for your application in a browser and check how it works:


Installing modules

If your Django application needs modules to be installed and you have a requirements.txt file uploaded along with all your application files, you can install them via the “Setup Python App” cPanel menu.

  1. Scroll down to the Execute python script field, click the Add button, and specify your requirements.txt file:



  2. Then click Run Pip Install and select requirements.txt from the drop-down menu:



    This will launch the Python modules installation.

That’s it!
Updated
Viewed
4427 times

Need help? We're always here for you.

notmyip