Deploy Django App Digitalocean Ubuntu 20.04 Server

Deploy Django App Digitalocean Ubuntu 20.04 Server

In this article we are going to conclude our Django series. We will deploy Django app to digitalocean droplet, ubuntu 20.04 server. If you have been following along, you should be set up for a successful deployment. We will be working with a Digitalocean ubuntu 20.04 server, that only cost us $5 when we purchased it.

Although this is sufficient for a small scale web app, keep in mind if you do grow your web traffic — you will need to upgrade the droplet capacity.

If you have not been following along and are here just to learn about deploying your Django app, look at this set-up tutorial that covers what you need to do to set-up your droplet from scratch, install python3 development environment and the Django start-up project.

Getting started with Django Tutorial.

Or

Getting Started with Python Django Web Development
Python Django is one of the most commonly used web development platforms. It offers a simplified approach to web…skolo-online.medium.com

deploy django digitalocean ubuntu 20
deploy django digitalocean ubuntu 20

Your Django app ready for deployment?

Before you can deploy, you will need a domain, you can get one from any domains.co.za. You will then need to log in to the client area and adjust your DNS settings. You must edit the A — record of your domain to point to your droplet. If you are not sure how to do this, the customer support of your domain hosting service should be able to assist you.

Steps to follow are:

  1. Purchase a droplet from digital ocean for $5 — follow tutorial link above to set it up
  2. Build your Django application (you can follow the rest of the series for more guidance)
  3. Purchase a domain to use for your website
  4. Edit the DNS records of the domain to point to the droplet you have bought from digitalocean
  5. Come back and follow the rest of the tutorial

High level steps to deploy a Django web app to digitalocean ubuntu 20.04 server

How to deploy Django webapp
  • Edit settings file to add new hosts
  • Test that gunicorn was installed correctly
  • Create gunicorn systemd service file
  • Configure Nginx to serve your web app
  • Install PythonCertbot for SSL certificates and install an SSL certificate for your website

Edit the Django settings file

Find the settings file, which should be in your Django project directory and add to the allowed hosts you domain. Look for ALLOWED_HOSTS and add to the existing hosts:

ALLOWED_HOSTS = [........'yourdomain.com','www.yourdomain.com',.........]

Test Gunicorn was installed correctly

To test this, you need to allow gunicorn to run your application. Run the following command:

gunicorn --bind 0.0.0.0:8000 yourproject.wsgi

Then test the IP address location, port 8000 for your application.

When done, deactivate your virtual environment.

deactivate

Create Gunicorn systemd file

Basically a systemd file is a system file that will run your application automatically. So instead of of running the command above with gunicorn every-time and watching your app, you will let the systemd file manage that.

Even if the server goes down for maintenance, if there was a power outage — you don’t have to worry about restarting your app.The systemd file will take care of that — if you want more details — read this article from digital ocean:Understanding Systemd Units and Unit Files | DigitalOcean
Increasingly, Linux distributions are adopting or planning to adopt the systemd init system. This powerful suite of…www.digitalocean.com

Start by creating a systemd socket file for gunicorn.

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

The contents of the file should look like this:

[Unit]Description=gunicorn socket

[Socket]ListenStream=/run/gunicorn.sock

[Install]WantedBy=sockets.target

Next we create a systemd service file for gunicorn with the following command:

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

Paste this inside the file, I have highlighted in bold the sections that you need to change. Replace yourusername — with you droplet username. And make sure the path-toprojectdir is correct for the root of the project (where the manage.py file is) and the projectenv folder. These are very important.

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

[Service]User=yourusername
Group=www-data
WorkingDirectory=/home/yourusername/path-to-your-projectdir
ExecStart=/home/yourusername/path-to-your-  projectdir/yourprojectenv/bin/gunicorn \          
      --access-logfile - \          
      --workers 3 \          
      --bind 

unix:/run/gunicorn.sock \          
       yourproject.wsgi:application

[Install]WantedBy=multi-user.target

When you have completed and checked your file, you can run the following commands to start your systemd file.

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

You can check for the status of your gunicorn socket with:

sudo systemctl status gunicorn.socket

Check the systemd status:

sudo systemctl status gunicorn

Everytime you make changes to your python files, if you want to add or update any project file — you will need to restart the systemd files:

sudo systemctl daemon-reload
sudo systemctl restart gunicorn

Configure Nginx to server your Django Webapp on digitalocean ubuntu 20.04

If you do not already have nginx installed:

sudo apt update
sudo apt install nginx

Create a new server block for your project

sudo nano /etc/nginx/sites-available/yourproject

Inside the file, paste the following:

server {
    listen 80;
    listen [::]:80;

    server_name yourdomain.com www.yourdomain.com; 

    location = /favicon.ico { access_log off; log_not_found off; } 
    location /static/ { root /home/yourusername/path-to-youprojectdir; } 
    location / { include proxy_params; proxy_pass http://unix:/run/gunicorn.sock; }
}

Close the file. Enable it using the command:

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

Check for nginx configurations, make sure you have no errors:

sudo nginx -t

Restart nginx:

sudo systemctl restart nginx

If you have the firewall activated, allow ngix full access:

sudo ufw allow 'Nginx Full'

Install Python Certbot for FREE SSL certificates for your website from Letsencrypt

This part is optional, you can purchase an SSL or install a free one with certbot. Run the commands to install:

sudo apt-get update
sudo apt-get install python-certbot-nginx

Then you can install an SSL certificate for your website with just one command, pretty neat 😃

sudo certbot --nginx -d yourdomain.com -d www.yourdomain.com

Your website should be up and running with a secure SSL.

Full video tutorial on youtube

You can follow along on youtube here, we are finally deploying the Job Search website we have been building. The tutorial starts with some other stuff, if you want to jump right to the deployment stage, use the timestamps in the video description.

Get more Python Django Tutorials – https://blog.tati.digital/category/tutorials/

Leave a Reply

Your email address will not be published.

Subscribe to our mailing list

And get this 💃💃 Free😀 eBook !

Holler Box