Hostwizly

Host Django Website On Hostinger VPS

Published 16 September 2024 by Jarvis Silva

Want to know how to deploy a Django project to Hostinger VPS then you are at the right place. This article will provide a detailed step by step guide on how to host Django website on Hostinger VPS.

In this tutorial, I will cover everything from setting up a VPS in Hostinger, Installing the necessary software and libraries, setting up nginx, connecting domain, installing SSL and setting up the firewall.

Prerequisites

  • A Hostinger VPS plan.
  • Basic knowledge of the command line and SSH
  • A Django website project to host.

By the end of this article, you will have a fully functional Django website hosted on Hostinger.

STEP 1: Get & Setup Hostinger VPS

To host a Django website on Hostinger you need to get their VPS plan as you cannot host Django website on shared hosting.

If you don’t have a VPS plan yet, you can purchase one using this discount link: Get Hostinger VPS.

After purchasing a VPS Plan follow below steps to setup it:

  1. Login to your Hostinger account, click on your VPS Hosting and click on Setup which initiate the setup
  2. First it will ask to Select your Server Location. Choose the one which you fill is the closest to your audience.
  3. Next, It will ask to Choose Operating System, Plain OS and then choose Ubuntu.
  4. Lastly it will ask to Setup your VPS Name and Root Password. Set strong root password and store it securely.

This will setup your VPS on Hostinger

STEP 2: Obtain VPS Details At Hostinger

We need to obtain the IP address, Username and Password of the VPS For using SSH so to obtain this follow below steps:

  1. Login to your Hostinger account and Navigate to the VPS Section to access your server.
  2. In your VPS dashboard choose the server you want to connect to.
  3. Hostinger vps server select page
  4. On the overview page, Go to VPS Access > SSH Access. Here you will find the SSH details required for connecting to the server. Note the username, ip address and SSH password if you don't know then change it by clicking on change.
  5. Hostinger vps ssh details

STEP 3: Connect To Hostinger VPS Using SSH

We have obtained the SSH Details of our VPS now to connect to the server we will have to use a SSH Client we can use Putty on windows or Terminal on macOS/linux.

To connect to server follow below steps:

For Windows

  1. Download and Install Putty SSH Client. To download go here: Download Putty.
  2. Open Putty. In the Hostname enter your vps ip address and click on open.
  3. Putty page
  4. It will open a terminal and ask you to enter your username and password (For security purpose password will not be shown when you type it). Enter the correct credentials of your VPS and you will be logged in to the server.

For MacOs and Linux

  1. Open your Terminal.
  2. Run the below command. Replace username and ip address with your VPS username and ip we obtained earlier in Step 2.
  3. ssh username@<vps_ip_address>
  4. It will prompt you to enter your username and password (For security purpose password will not be shown when you type it). Enter the correct credentials of your VPS and you will be logged in to the server.

So everytime you want to login in to your server you can use the above method. Now we can start setting up the environment for hosting Django website.

STEP 4: Installing Required Softwares & Libraries

To run a Django website on VPS Server we need to install various packages on our VPS such as Python, PIP, VENV, Nginx and GIT. So let's install each one by one.

1. Update the server

It is a good practice to update the server package list and upgrade all installed packages so use run below command on your server.

sudo apt update 
sudo apt upgrade -y

2. Install Nginx Server

Nginx is a popular web server which we will use as a reverse proxy to pass requests to the Django application. To install run below command:

sudo apt install nginx -y

Start Nginx server

sudo systemctl start nginx 
sudo systemctl enable nginx

Now if you visit your server ip address you should see the default nginx welcome page which means your server is running.

3. Install Python3 & Pip

As we need python to run Django we need to install python and pip for installing python packages.

sudo apt install python3 -y 
sudo apt install python3-pip -y

The above commands will install python3 and pip on your server, to verify the installation run the below command:

python3 --version
pip3 --version

The output should display the installed versions of Python and pip.

4. Upload your Django Project to VPS

  1. Create your project folder at var/www/your-project-folder-name. Here we will upload all our Django website code. Replace your-project-folder-name with your desired project folder name.
  2. cd /var/www
    mkdir project-folder-name
  3. To upload the code you can use GIT or SCP.
  4. To upload using GIT (Recommended):

    sudo apt install git -y
    cd /var/www/your-Django-project-folder
    git clone repo-url .

    To upload using SCP:

    On your local machine run the below command

    scp -r /path/your-project-folder username@your_ip:/var/www/your-project-folder

    Replace /path/Django_project with the path to your local project. Replace username and your_ip with your VPS username and IP address.

5. Setting up the project on VPS

  1. First install virtualenv in python which allows us to isolate python packages from server packages. To install use below command:
  2. pip3 install virtualenv
  3. Activate the virtual environment:
  4. cd /var/www/your-Django-project-folder
    virtualenv venv
    source venv/bin/activate
  5. Install Django dependencies:
  6. pip install -r requirements.txt

    If you don't have requirements.txt file create one in the project directory and add all the names of dependencies.

  7. Configure settings.py
  8. nano /var/www/your_project_folder/settings.py
    • Add Server IP to ALLOWED_HOSTS:
    • ALLOWED_HOSTS = ['your-server-ip']
    • Set Up Static Files Directory:
    • STATIC_ROOT = os.path.join(BASE_DIR, 'static/')
  9. Collect Static Files:
  10. python manage.py collectstatic
  11. Run the Django Server:
  12. python manage.py runserver 0.0.0.0:8000

    Now if you try to access your website at http://your_vps_ip:8000 you will not see it running as we have not yet configured nginx so let's configure it.

6. Configure Nginx Server

  1. Create a New Nginx Configuration File:
  2. sudo nano /etc/nginx/sites-available/your-project-folder-name
  3. Add Configuration Settings:
  4. server {
        listen 80;
        server_name enter_your_vps_ip_or_domain;
    
        location / {
            proxy_pass http://127.0.0.1:8000;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Proto $scheme;
        }
    
        location /static/ {
            alias /var/www/your-project-folder-name/static/;
        }
    
        location /media/ {
            alias /var/www/your-project-folder-name/media/;
        }
    }
    
    • Enter your_server_ip_address at server_name
    • Enter your-project-folder-name at alias /var/www/your-project-folder-name/static/
    • Enter your-project-folder-name at alias /var/www/your-project-folder-name/media/
  5. Save and Exit: (CTRL + X, then Y, then Enter).
  6. Create a Symbolic Link:
  7. sudo ln -s /etc/nginx/sites-available/your-project-folder /etc/nginx/sites-enabled/
  8. Test the configuration:
  9. sudo nginx -t
  10. Restart Nginx server:
  11. sudo systemctl restart nginx
  12. Rerun your Django website server:
  13. cd /var/www/your-Django-project-folder
    python manage.py runserver 0.0.0.0:8000

Now if you visit at your_ip_address and your Django website should be served. If you want to run the Django website for production follow this tutorial: Run Django for production using Gunicorn.

STEP 5: Connect Domain To VPS

If you don’t already have a domain name, you need to purchase one from a domain registrar. Once you have a domain, configure it to point to your VPS. Follow below steps:

Configure Your Domain DNS

NameTypeAddressTTL
your-domain.comAyour-server-ip-address14440 or auto
www.your-domain.comAyour-server-ip-address14440 or auto

Add the above records to your Domain DNS and save. Note it can take around 24 hours to propogate.

Update your Nginx Configuration

sudo nano /etc/nginx/sites-available/your-project-folder-name

Modify the server_name directive to your domain name, earlier we have added ip address now replace it with your domain name.

server {
    listen 80;
    server_name your_domain.com;

    location / {
        proxy_pass http://127.0.0.1:8000;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }

    location /static/ {
        alias /var/www/your-project-folder-name/static/;
    }

    location /media/ {
        alias /var/www/your-project-folder-name/media/;
    }
  }

Now if you visit your website at http://your-domain.com then you should see your Django website serving.

STEP 6: Install SSL

To access the website using https you need to install a SSL Certificate so let's install one using Certbot.

  1. Install Certbot
  2. sudo add-apt-repository ppa:certbot/certbot 
    sudo apt update
    sudo apt install certbot python3-certbot-nginx -y
  3. Obtain and install the SSL Certificate:
  4. sudo certbot --nginx -d your_domain.com -d www.your_domain.com

Now it will install SSL Certificate for your domain, you can now visit your website at https://your_domain.com

STEP 6: Setup Firewall

We will use the pre-installed firewall software UFW. To setup the firewall run below commands

  1. Allow essential ports:
    • SSH (Port 22): To maintain SSH access.
    • HTTP (Port 80): For serving web traffic.
    • HTTPS (Port 443): For secure web traffic.
    sudo ufw allow OpenSSH 
    sudo ufw allow 'Nginx HTTP'
    sudo ufw allow 'Nginx HTTPS'
  2. Enable the firewall:
  3. sudo ufw enable 
    sudo ufw status

It will display firewall status as active with all the enabled rules.

Summary

So this was everything for this tutorial, I have covered all the necessary steps, from setting up your server to configuring Nginx, uploading your code, and securing your site with SSL.

Also Read: Host Node JS on Hostinger VPS

I hope you found the guide easy to follow and that you have successfully hosted your Django project on Hostinger. If you ran into any issues a simple google search should able to solve it.

Thank you for reading, Have a nice day 😊

About the author

I am Jarvis Silva, a tech enthusiast. I have 5 years of experience in website hosting and development, Now sharing all my knowledge related to web hosting and other various tech on the internet through Hostwizly.

Related Articles

How To Add SPF Record In Hostinger

How To Increase WP Memory Limit In Hostinger

How To Download Files From Hostinger File Manager

How To Find FTP Password In Hostinger

How To Add Signature In Hostinger Webmail

How To Connect Namecheap Domain To Hostinger

How To Add DKIM Record In Hostinger Email

How To Add DMARC Record In Hostinger

How To Transfer Domain From Wix To Hostinger

How To Switch From Hostinger Website Builder To Wordpress

How To Find Ip Address Of Website In Hostinger

How To Purchase Domain And Hosting From Hostinger

How To Install Wordpress In Hostinger

How To Extract Zip File In Hostinger

How To Install Wordpress Theme In Hostinger

How To Backup Emails From Hostinger

How To Connect Hostinger Domain To Shopify

How To Setup Hostinger Email On Outlook

How To Setup Hostinger Email On Gmail

How To Add TXT Record In Hostinger

How To Add CNAME Record In Hostinger

How To Set A Cron Job In Hostinger

How To Install Wordpress On Subdomain In Hostinger

Deploy Angular App On Hostinger Shared Hosting

How To Change PHP Version In Hostinger

How To Install Moodle On Hostinger

How To Get FREE Hosting On Hostinger

Difference Between Hostinger.com And Hostinger.in

Host Laravel Project On Hostinger Shared Hosting

Host Node JS On Hostinger

Host React App On Hostinger Shared Hosting

Is hostinger good for buying domain?

How to add hostinger domain to netlify

How to transfer domain from hostinger to godaddy

How to uninstall wordpress in hostinger

How to connect Godaddy domain to Hostinger

How to host PHP website on Hostinger

How to change server location in Hostinger

How To Host Next JS Website On Hostinger

How to create subdomain in hostinger