How To Deploy Django Project in Digital Ocean

How To Deploy Django Project with Custom Domain And Digital Ocean

Welcome you guys! In this tutorial, you will learn how to deploy the Django project with a custom domain and digital ocean. Before we start, you must create a droplet and configure your domain DNS servers.

If you don’t know how to create and configure , please watch the below videos.

you can also follow the digital ocean official documentations

Initial Server Setup with Ubuntu 20.04: https://www.digitalocean.com/community/tutorials/how-to-set-up-django-with-postgres-nginx-and-gunicorn-on-ubuntu-20-04

For more details: https://www.digitalocean.com/community/tutorials/initial-server-setup-with-ubuntu-20-04

Ok guys, Here are the few Instructions to complete this deployment successfully.

Open your command prompt / terminal, Now type the below command.

ssh@domain.com / ssh root@your_server_ip

Enter your password and login to your Linux server.

1 . Creating new user

Now follow the below commands to create new user and give privileges’ to the user

adduser sammy 

# Enter passwords,skip the remaining.  'sammy' - example username

To granting the administrative privileges follow these commands

usermod -aG sudo <user>
# user - your username

2. Basic Firewall setup

To set up firewall to our server follow these commands.

ufw app list
ufw allow OpenSSH
ufw enable
ufw status
ufw allow 8080

3. Granting Privileges

Please follow to grating the privileges. Please change the user to your username.

mkdir -p /home/user/.ssh
cp ~/.ssh/authorized_keys /home/user/.ssh
chown -R user:user /home/user/.ssh
cd /home/jp/.ssh
ll
logout 

Now logout from your root user and login with new user credentials.

ssh user@jpnan.store
# password

4. Installing the libraries

Now, Update your Linux system and install pip , nginx, and curl . PostgreSQL if you use.

sudo apt update
sudo apt install python3-pip python3-dev libpq-dev postgresql postgresql-contrib nginx curl
sudo ufw app list
sudo ufw allow 'Nginx Full'
sudo apt-get install python3-venv

5. Allow Nginx

Now allow nginx,

sudo ufw app list
sudo ufw allow 'Nginx Full'

Now open your browser and search your domain , check the Nginx welcome screen.

http://jpnan.store/

Now stop Nginx

sudo systemctl stop nginx

Read more from JP

6. Clone your Project from GitHub

Now its time to clone your Django project which you want to deploy it.

git clone https://github.com/PrasadRaoJ/devpyjp.git

7. Create virtual environment

Now create virtual environment for our Django project.

python3 -m venv env
source env/bin/activate

Now install your Django project requirements ,

pip3 install django==2.2 django-ckeditor  pillow
pip3 install django gunicorn psycopg2-binary

# if you have requirements.txt 
pip3 install -r requirements.txt

8. Configuring the settings.py

Its time to configure the settings.py file in our project. Here we need to add our domain and change the databses (if any other ) and finally static files.

1.ALLOWED_HOSTS

ALLOWED_HOSTS = ['jpnan.store','localhost']

2 .Debug

Debug = Flase

3. Static Files

These static files were configured as per my project, please check the official documents if anything goes wrong.

Here a tricky concept for loading static files. we need to change debug 2 times and run our project.

python manage.py 0.0.0.0:8080

Now do migrations

python manage.py makemigrations
python manage.py migrate

Now Create admin for your project

python manage.py createsuperuser

Now collect your static files

python manage.py collectstatic

Now run the server and visit your site

http://jpnan.store:8080/

Now change debug = True and run the server. check the static files configured properly or not. Now again change the debug = False and the run server.

If static files configured successfully then disallow the port 8080.

sudo ufw deny 8080

9. Configure the Gunicorn

Now we need to configure the gunicorn socket and service properly.

1.Gunicorn Socket

Create a gunicorn socket

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

Now copy and paste the below lines

[Unit]
Description=gunicorn socket

[Socket]
ListenStream=/run/gunicorn.sock

[Install]
WantedBy=sockets.target

copy paste and save it.

2. Gunicorn Socket

Now create gunicorn service, copy paste the below lines.

 sudo nano /etc/systemd/system/gunicorn.service
[Unit]
Description=gunicorn daemon
Requires=gunicorn.socket
After=network.target

[Service]
User=sammy
Group=www-data
WorkingDirectory=/home/sammy/myprojectdir #your project
ExecStart=/home/sammy/myprojectdir/myprojectenv/bin/gunicorn \
          --access-logfile - \
          --workers 3 \
          --bind unix:/run/gunicorn.sock \
          myproject.wsgi:application

[Install]
WantedBy=multi-user.target

Change the project directories properly according to your django project.

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

[Service]
User=sammy
Group=sammy
WorkingDirectory=/home/sammy/devpyjp
ExecStart=/home/sammy/env/bin/gunicorn \
          --access-logfile - \
          --workers 3 \
          --bind unix:/run/gunicorn.sock \
          devpyjp.wsgi:application

[Install]
WantedBy=multi-user.target

3. Start gunicorn

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

Now check gunicorn configured properly or not.

file /run/gunicorn.sock

Now check the gunicorn status

sudo systemctl status gunicorn status

Now Test our gunicorn service.

curl --unix-socket /run/gunicorn.sock localhost

4. Configure our site

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

Now add the below script.

server {
    listen 80;
    server_name jpnan.store;

    location = /favicon.ico { access_log off; log_not_found off; }
    location /static/ {
        root /home/sammy/devpyjp;
    }

    location / {
        include proxy_params;
        proxy_pass http://unix:/run/gunicorn.sock;
    }
}

devpyjp – its my project name and jpnan.store is my domain name. Configure them according to your project.

Now enable the added sites

sudo ln -s /etc/nginx/sites-available/jpnan /etc/nginx/sites-enabled
sudo nginx -t

Now check the respone , if nginx configured properly then move forward.

sudo systemctl restart nginx

10. Adding SSL certificates

Now add SSL certificates, install certbot and configure it.

sudo apt install python3-certbot-nginx, sudo certbot --nginx -d jpnan.store

Now restart the gunicorn service.

sudo systemctl daemon-reload
sudo systemctl restart gunicorn.socket gunicorn.service

You need to remember these above 2 commands whenever you will update your project code.

Now search your site on your browser.

https://jpnan.store/

That’s it guys, Finally, we deployed our Django project on the digital ocean. I know you may face errors in the process, but please follow the instructions and also check the official documentation of Django deployment on the digital ocean.

I hope it helps you thank you for the patience.

Leave a Reply