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',.........]Copy Code
 

Gunicorn Testing

 

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

 
gunicorn --bind 0.0.0.0:8000 yourproject.wsgiCopy Code
 

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

 
pip install gunicornCopy Code
 

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.socketCopy Code
 

Then the contents of the file should look like this:

 
[Unit]Description=gunicorn socket[Socket]ListenStream=/run/gunicorn.sock[Install]WantedBy=sockets.targetCopy Code
 

Next, create a systemd service file for gunicorn:

 
sudo nano /etc/systemd/system/gunicorn.serviceCopy Code
 

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 daemonRequires=gunicorn.socketAfter=network.target[Service]User=yourusernameGroup=www-dataWorkingDirectory=/home/yourusername/path-to-your-projectdirExecStart=/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.targetCopy Code
 

When you have completed, you can run the following commands to start your systemd file:

 
sudo systemctl start gunicorn.socketsudo systemctl enable gunicorn.socketCopy Code
 

Next check for the status of your gunicorn socket:

 
sudo systemctl status gunicorn.socketCopy Code
 

Check the systemd status:

 
sudo systemctl status gunicornCopy Code
 

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

 
sudo systemctl daemon-reloadsudo systemctl restart gunicornCopy Code
 

Configure Nginx to Django Webapp on digitalocean

 

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

 
sudo apt updatesudo apt install nginxCopy Code
 

Next for your applicate create a new server block:

 
sudo nano /etc/nginx/sites-available/yourprojectCopy Code
 

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

Later close the file and enable it using the command:

 
sudo ln -s /etc/nginx/sites-available/yourproject /etc/nginx/sites-enabledCopy Code
 

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

 
sudo nginx -tCopy Code
 
sudo systemctl restart nginxCopy Code
 

Allow ngix full access If the firewall is activated, 

 
sudo ufw allow 'Nginx Full'Copy Code
 

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 updatesudo apt-get install python-certbot-nginxCopy Code
 

Further install SSL certificate for your website:

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

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 *

Speed issues driving customers away?
We’ve got your back!