Bobcares

Deploy Django app on Digitalocean

by | Sep 7, 2022

Django is a powerful web framework that allows you to deploy Python applications or your websites. Bobcares, as a part of DigitalOcean Managed Service guides on how to Deploy Django app on Digitalocean .

Deploy a Django Web App on DigitalOcean

 

We will be working with a Digitalocean ubuntu 20.04 server, if you do grow your web traffic you will need to upgrade the droplet capacity. Before you can deploy, you will need a domain and adjust your DNS settings. Where you need to edit the A record of your domain to point to your droplet.

Deploy Django app on Digitalocean

Edit Django settings file

 

Open your settings file, this should be in your Django project directory, and add allowed hosts of the domain. YOu can add ALLOWED_HOSTS as shown below:

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

Gunicorn Testing

 

For testing, you need to allow “gunicorn” to run the application:

 
gunicorn --bind 0.0.0.0:8000 yourproject.wsgi
 

If Gunicorn is not installed, then from the virtual environment run :

 
pip install gunicorn
 

Once it is done, deactivate your virtual environment: “Deactivate“.

 

Create Gunicorn systemd file

 

Basically, systemd file is a system file that will run your application automatically so instead of running the command of gunicorn every time repeatedly, you can rely on systemd file manage.

 

Even if the server goes down for maintenance or during a power outage, you don’t have to worry about restarting the application. The systemd file will take care of it.

 

Initially start by creating a systemd socket file for gunicorn.

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

Then the contents of the file should look like this:

 
[Unit]
Description=gunicorn socket

[Socket]
ListenStream=/run/gunicorn.sock

[Install]
WantedBy=sockets.target
 

Next, create a systemd service file for gunicorn:

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

Paste the given code inside the file and replace the username with your droplet username. Make sure that 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, you can run the following commands to start your systemd file:

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

Next check for the status of your gunicorn socket:

 
sudo systemctl status gunicorn.socket
 

Check the systemd status:

 
sudo systemctl status gunicorn
 

Every time you make changes to your python files you will need to restart the systemd files:

 
sudo systemctl daemon-reload
sudo systemctl restart gunicorn
 

Configure Nginx to Django Webapp on digitalocean

 

Install the Nginx server if you do not have it installed:

 
sudo apt update
sudo apt install nginx
 

Next for your applicate create a new server block:

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

Then paste the following code:

 
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;
}
}
 

Later close the file and enable it using the command:

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

Check for Nginx configurations and then restart the service if there are no errors encountered:

 
sudo nginx -t
 
sudo systemctl restart nginx
 

Allow ngix full access If the firewall is activated, 

 
sudo ufw allow 'Nginx Full'
 

Install Python Certbot SSL certificates for website

 

To get your connection secured you can install SSL with certbot. Run the commands to install:

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

Further install SSL certificate for your website:

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

By this, your website should be up and running with a secure SSL.

 

[Need assistance with similar queries? We are here to help]

 

Conclusion

In this article, we are going to conclude our Django series by finally deploying our web app to a Digitalocean droplet. By this, you should be successfully set up deployment to the application and configured the Django project to connect to a database.

 

PREVENT YOUR SERVER FROM CRASHING!

Never again lose customers to poor server speed! Let us help you.

Our server experts will monitor & maintain your server 24/7 so that it remains lightning fast and secure.

GET STARTED

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

Never again lose customers to poor
server speed! Let us help you.