All-Inclusive Guide to Deploying a Django Project on a VPS

Deploy Django On Vps

In this article, we will learn how to deploy Django on a Virtual Private Server (VPS) using Gunicorn and Ngnix Web server software

Note: In this article, I will deploy the Blog Application Project that we built in the previous article. If you have your own Project, you can use that as well. 

What is VPS?

VPS which stands for Virtual Private Server is a virtual machine sold as a service by various hosting companies. Think of it as separate laptop CPU hardware but in the raw form.

Various companies like Google, Amazon etc provide Cloud servers(GCP, AWS etc) which are basically Servers situated at different locations which you can lend to run your deployed applications.

The advantages of cloud server:

  • Servers are running 24 hrs – in case of localhost(your laptop), you need to keep it running for 24 hrs which is not practical.
  • Scalability – If your storage is full, you can expand your storage in the cloud
  • More Secure – hardware theft is not possible
  • Accessible from any computer/laptop

In this tutorial, I am using the AWS(Amazon Web Services- Cloud server). You can go with any of the VPS providers.


Now, a Django Application deployed onto a VPS has to do the following:

  • Host Static Files
  • Handle Http requests
  • Recover from Crashes
  • Scale up when required

To perform all these tasks, we require different software:

  1. A Web server (like Nginx)
  2. A WSGI Application server (like Gunicorn)
  3. Your Actual Web Application (written using Django framework, flask framework, etc)

Here a web server like Nginx handles the domain logic and accepts the HTTP requests. The appropriate requests are then sent to the Application Server like Gunicorn.

Gunicorn, converts the requests into a python compatible format along with other features like load balancing, etc. These requests are sent to the application code (in Django), which performs the necessary functions of the website.

We will now look at them individually


What is a Web Server Application?

Web Server software is used to host websites on the internet. It acts as a middleman between the server and the client machines. These software applications access the physical files present on the server and display them out to the clients.

Examples of Web Server Software are Apache, NGINX etc.

In this article, we will use the Nginx server, which is increasingly used by most of the web applications on the internet

What is Gunicorn?

Gunicorn (G-unicorn) is a Python WSGI ( Web-server Gateway Interface) Server that acts as an interface between the webserver and your Web application. Gunicorn interacts with the WSGI file of our application.

Gunicorn takes care of everything that happens in between webservers and our Django application. It performs various tasks:

  • Can interact with multiple web servers
  • Balance the load traffic coming on the website
  • Keeping multiple processes of our Web Application running

Deploying a Django Project on a VPS

First, we need to select a server from the VPS provider. They have a variety of servers running on different OS like shown below:

Servers
Servers

After selecting the required one, you will need to go through some steps (different for different providers) to run and connect the cloud server.

Once the server is set up, you will get the IP address or the public DNS of the server, which will be later required to connect to the cloud server.

1. Connecting to the Cloud Server

We use ssh command to connect to the server. Now we will connect to the server OS from our local computer.

  • Using Mac or Linux:

In the shell, simply run the code:

ssh username@<server_IP>
  • Using Windows

For windows, we use a software called PuTTY, which you can download from directly from the internet. Once downloaded, enter the cloud_IP in there and hit open.

PuTTY
PuTTY

My cloud_IP/Public DNS which I will be using to access my server is:

Image 4
Public DNS

For AWS, we need to use the private security key as well. Hence depending on the VPS provider, you might need to use some more commands to connect.

Once it is done, you will be logged into the cloud server OS.

Cloud Server
Cloud Server

2. Setting-up necessary installations

Once the server is connected, it is just like your new laptop. We need to set up and install necessary packages on to it.

So run the following commands:

Installing Upgrades

sudo apt-get update
sudo apt-get upgrade

Always update the system, after starting the server.

Installing python-pip

Then, we need to install python on the system

sudo apt-get install python3-pip

After that update pip using

python3 -m pip install --upgrade pip

Installing Django

Now we must install Django as well.

pip3 install django

Deploying a Django Project

Now in the cloud server itself, you can create and code the entire django project using:

django-admin startproject <project_name>

Or else, you can transfer the local Django file present in your computer to the cloud server.

To transfer the file, we use the code

scp -i <path/to/key> -r <path/to/file> ubuntu@<public_DNS>:<path/to/destination>
Transfer Files
Transfer Files

In services like AWS, we need to insert a security key as well. If your VPS does not require a key then simply add the path of the folder there.

Modifying the settings.py file

Once the Django project is sent to the server, we need to make some changes in the settings.py file. So change the directory into the project and open the settings.py file using the below commands:

cd <project_name>
nano <project_name>/settings.py

Now turn DEBUG to False and in the ALLOWED_HOSTS add the Public DNS of the server.

Settings.py
Settings.py

Also add the following code for static files:

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

Also run the command in the parent project_folder, to collect all the static files

python3 manage.py collectstatic

This is the process that we use to collect all the static files into the project folder. Do check out the Django Static Files article for more information about static files.

4. Installing Gunicorn

Now we will install gunicorn into our cloud server:

pip3 install gunicorn

That’s with Gunicron for now. We will now move on to Nginx and see how to connect it with Gunicorn

4. Installing Nginx

Install nginx on the server using:

sudo apt-get install nginx

Now create a file in the directory /etc/nginx/sites-available/<project_name>. Hence write the code:

sudo nano /etc/nginx/sites-available/<project_name>

Add the following code in the file:

server {
    listen 80;
    server_name 0.0.0.0;

    location = /favicon.ico { access_log off; log_not_found off; }

    location /static/ {
            root /path_to_project/<project_folder_name>;
    }

    location / {
            include proxy_params;
            proxy_pass http://unix:/path_to_project/<project_name>/<project_name>.sock;
    }
}

Save and exit.

Enabling the file created above

To enable the above file, run the code:

sudo ln -s /etc/nginx/sites-available/myproject /etc/nginx/sites-enabled

Checking the configuration file

Now to check the above configuration file was correctly written, run the command:

sudo nginx -t

If everything is correct, you will get the below message

Configuration File
Configuration File

Running the website using the Gunicorn and Nginx

First start the Nginx web server:

sudo service nginx restart

Now to run our application, we need to bind Gunicorn with Nginx.

We use a UNIX socket to bind Nginx and Gunicorn together. In the above code, we are doing the same: binding Gunicorn and Nginx using the socket.

So run the command:

gunicorn --daemon --workers 3 --bind unix:/home/ubuntu/<project_name>/<project_name>.sock <project_name>.wsgi

That’s it!! Now via your local computer browser, go to the Public DNS of your VPS and check. The server will be up and running.

Admin Site
Admin Site

You will need to create a superuser again since the database data is not present on the server. So stop Gunicorn using ctrl + c and then make the necessary changes.

Once its done, use the same command as above to run Gunicorn and you are ready to go!!

Blog App
Blog App

See our Django blogs app is now running on the AWS cloud server.

Importing Project Via GitHub

You can also import your Django project onto the cloud server using GitHub. In this way, you can work on your project using your local computer and then install the whole project on to the loud server, simply using GitHub.

Conclusion

That’s it, guys! Your Django Application is successfully deployed onto a VPS. I hope you have gained enough knowledge from this article.

In the next article, we will see about the extensions provided by the Django Framework