Back to Blog
Mar 10, 20235 min read

Deploy Django Project on Digital Ocean VPS (Droplet) 🚀

DockerAWSDjangoDevOpsPython
Deploy Django Project on Digital Ocean VPS (Droplet) 🚀

The popularity of web development frameworks like Django has made it easier than ever to build complex and scalable web applications. However, deploying a Django project can be a daunting task for many developers, especially when it comes to choosing the right hosting solution.

In this article, we will guide you through the process of deploying a Django project on a Digital Ocean VPS (Droplet). Digital Ocean is a cloud hosting provider that offers fast and affordable virtual private servers, making it an excellent choice for developers looking to host their Django projects.

We will start by setting up a new Droplet on Digital Ocean and configuring it for our Django project. We will then walk through the process of installing all the necessary software and packages, setting up a virtual environment, and configuring our project settings.

Finally, we will show you how to deploy your Django project using Gunicorn and Nginx, two popular tools that can help you serve your web application to the world. By the end of this article, you will have a fully functional Django application running on your own VPS, ready to serve your users. So let’s get started!

Step 1: Creating a VPS(Droplet) on Digital Ocean

Go to https://cloud.digitalocean.com/droplets and click on Create Droplet the button. Then, select Ubuntu 16.04

Step 2: Installing dependencies

#shell script that installs Docker, Nginx, Python, Virtualenv, Java, and Jenkins on an EC2 instance running Ubuntu, and includes 
#reload commands for Nginx and Jenkins:

#!/bin/bash

# Install Docker
sudo apt update

# Install Nginx
sudo apt install -y nginx

# Install Python
sudo apt install -y python3 python3-pip

# Install Virtualenv
sudo pip3 install virtualenv


# start Jenkins
sudo systemctl start jenkins
sudo systemctl enable jenkins

# start Nginx
sudo systemctl start nginx
sudo systemctl enable nginx

# It reloads the systemd manager configuration.
sudo systemctl daemon-reload

Step 3: Setting up our project and its environment

ssh to your server using the terminal.

Clone Your Django Project and create a virtual environment inside that directory.

git clone https://github.com/rashiddaha/drfblogproject.git
cd projectdirectory
then
virtualenv env

A virtual environment named env will be created. Let’s activate this virtual environment:

source env/bin/activate
pip install -r requirements.txt
pip install django gunicorn

This installs Django and gunicorn in our virtual environment

Add your IP address or domain to the ALLOWED_HOSTS variable in settings.py.

If you have any migrations to run, perform the action:

python manage.py makemigrations
python manage.py migrate
python manage.py collectstatic

Import Thing about static files, You must make sure to add few lines in your seeting.py file.

  1. add this line “whitenoise.runserver_nostatic”, into your Installed_apps of setting file.
  2. add ‘whitenoise.middleware.WhiteNoiseMiddleware’, into MiddleWare of your setting File.
  3. Also, add these lines at the bottom of the blog/urls. py file.
if settings.DEBUG:
    urlpatterns += static(settings.MEDIA_URL, document_root = settings.MEDIA_ROOT)
    urlpatterns += static(settings.STATIC_URL, document_root = settings.STATIC_URL)

4. Also, add these imports lines at the top of the blog/urls. py file.

from django.conf import settings # new
from  django.conf.urls.static import static #new

5. Run this command

$ pip install whitenoise

Configuring gunicorn

Deactivate the virtual environment by executing the command below:

deactivate

Let’s create a system socket file for gunicorn now:

sudo vim /etc/systemd/system/gunicorn.socket

Paste the contents below and save the file

[Unit]
Description=gunicorn socket

[Socket]
ListenStream=/run/gunicorn.sock

[Install]
WantedBy=sockets.target

Next, we will create a service file for gunicorn

sudo vim /etc/systemd/system/gunicorn.service

Paste the contents below inside this file:

[Unit]
Description=gunicorn daemon
Requires=gunicorn.socket
After=network.target

[Service]
User=root
Group=www-data
WorkingDirectory=/root/YourProjectDirectoryName
ExecStart=/root/YourProjectDirectoryName/env/bin/gunicorn \
          --access-logfile - \
          --workers 3 \
          --bind unix:/run/gunicorn.sock \
          yourSettingsFileFoldername.wsgi:application
[Install]
WantedBy=multi-user.target

Lets now start and enable the gunicorn socket

sudo systemctl start gunicorn.socket
sudo systemctl enable gunicorn.socket

Configuring Nginx as a reverse proxy

Before You create Nginx File.

With this command, you can check if already a file exists.

cd /etc/nginx/sites-enabled

You can delete the existing default file using the command.

sudo rm -f FileName

Create a configuration file for Nginx using the following command

sudo vim /etc/nginx/sites-available/blog

Paste the below contents inside the file created

server {
    listen 80 default_server;
    server_name _;
    location = /favicon.ico { access_log off; log_not_found off; }
    location /YourStaticFilesDirectoryName/ {
        root /root/YourProjectDirectoryName;
    }
    location / {
        include proxy_params;
        proxy_pass http://unix:/run/gunicorn.sock;
    }
}

Create a configuration file for Nginx using the following command

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

Run this command to load a static file

$ sudo gpasswd -a www-data username

Restart Nginx and allow the changes to take place.

sudo systemctl restart nginx
sudo service gunicorn restart
sudo service nginx restart

So we are Done.

Additionally in case of errors

To check error logs

$ sudo tail -f /var/log/nginx/error.log

To check nginx working fine

$ sudo systemctl status nginx

If you enjoyed my article, show your appreciation with a round of applause! 👏 Your support means the world to me!

Feel free to connect with me across various social media channels! Join me on LinkedIn, YouTube, Twitter, GitHub, and Upwork. Your support means a lot. Thanks for taking the time to read this!

💬 Got questions? Ask me anything!