2021-03-12 20:07

Deploying django website on ubuntu server

We will be using digitalocean for vps. You can follow along with me

https://m.do.co/c/c2ce8c26e4d2 click and get $100 credit on digitalocean droplet.

Here in this blog post we will be using basic droplet 1gb of RAM & 1vCPU 25gb ssd.

Above config can manage upto 4 django sites on devlopement server.

number of website depends on the project.

Lets start with deployment process.

Step 1: Create fresh new droplet on digitalocean. Login to your droplet with $ssh [email protected]_server_ip.

Update the vps by below command

$sudo apt-get update.

Install the basic modules required for python3 & Django .

$sudo apt install python3-pip python3-dev python3-venv libpq-dev nginx curl

Step 2 : Create a seprate linux user with sudo privileges.

adding user.

$adduser username

Granting the sudo privileges.

$usermod -aG sudo username

switch to new user

$sudo su username

$cd~

create a virtualenv

$python3 -m venv myproject_env

$cd myproject_env

$source bin/activate

$pip3 install django

$django-admin startproject demo_project

make sure you don’t use django keywords eg.test

$cd demo_project

$python3 manage.py migrate

$python3 manage.py runserver 0.0.0.0:8000

Now you can see you new django app is running on

http://your_ip:8000

Till now we configure our django app as it works on local system.

Lets dive on main process. I assume you have FQD routed on on your current droplet ip address.Eg. $ping aruntiwari.in return my server_ip_address.

NOTE: You can also continue without FQD. All you have to do is replace domain_name with your ip address.

open your settings.py file located demo_project/settings.py

$vim demo_project/settings.py

Find ALLOWED_HOST=[‘your_domain_name’,’your_ip_address’]

Press Esc butten and then :wq for saving the file.

Step 3: Settingup gunicorn

$pip3 install gunicorn

$deactivate

$sudo vim /etc/systemd/system/demo.service

[Unit]
Description=gunicorn daemon
Requires=gunicorn.socket
After=network.target
[Service]
User=username #username which we created at the begining
Group=www-data
WorkingDirectory=/home/username/myproject_env/demo_project
ExecStart=/home/username/myproject_env/bin/gunicorn \
          --access-logfile - \
          --workers 3 \
          --bind unix:/home/username/myproject_env/demo_project/demo.sock
          demo_project.wsgi:application
[Install]
WantedBy=multi-user.target

$:wq

$sudo systemctl enable demo

$sudo systemctl restart demo

$sudo systemctl status demo

It should be working fine. If it is not working please check the directory structure properly.

$ls

you must seee demo.sock in your working directory.

Step 4: Setting up Nginx webserver.

Create a directory in /home/username/myproject_env/demo_project

$cd /home/username/myproject_env/demo_project

$mkdir static

Open settings.py and configure static files.

STATIC_ROOT = os.path.join(BASE_DIR, 'static/')

Note: don’t add “/static/” it will give permission error.It should be same as it is “static/”

$python3 manage.py collectsatic

$sudo vim /etc/nginx/sites-availabel/demo

server {
    listen 80;
    server_name server_domain_or_IP;
    location /static/ {
        root /home/username/myproject_env/demo_project;
    }
    location / {
        include proxy_params;
        proxy_pass http://unix:/home/username/myproject_env/demo_project/demo.sock;
    }
}

Save the file :wq

lets create a symlink for this file.

$ sudo ln -s /etc/nginx/sites-available/demo /etc/nginx/sites-enable/

Test the nginx for syntax

$ sudo nginx -t

If test is successfull restart the nginx.

$ sudo systemctl restart nginx.

your website should be live on your domain and ip.

Conclusion: Although we have coverd how to deploy django website with nginx and gunicorn this is not still not that good for production. For Production you might have add some additional configuration in nginx to secure your website. We will be covering those topics in my next blog till that time create something cool.