DEV Community

Mayank Thakur
Mayank Thakur

Posted on

Beginner's guide to deploy Django application using Gunicorn and Nginx

Before getting into serious business, here are some pre-requisites for following the post:

  1. This post assumes you have your Django project without any bugs on GitHub (we are going to use GitHub to transfer our
    project to the server).

  2. Make sure you have your requirements.txt file in the repository to setup the virtual environment. You can generate the requirements file using pip freeze > requirements.txt command.

  3. We will be looking at commands for a server using Ubuntu OS, the commands for other Linux based systems remain the same
    except for the package manager part.

  4. You would also need the ssh tool installed in your system. To install it in Ubuntu based system use the following commands:

sudo apt-get update
Enter fullscreen mode Exit fullscreen mode
sudo apt-get upgrade
Enter fullscreen mode Exit fullscreen mode
sudo apt-get install openssh-client
Enter fullscreen mode Exit fullscreen mode

Serious Business:

Now starts the serious business, in this section we will do all the configurations and make our application run on the remote server.

To do so, we will follow a series of steps listed below:

Step-1 :

  • First log into your remote server using ssh tool.

  • If you have username and password to log into the remote server you can do it like this:

ssh username@domain 
Enter fullscreen mode Exit fullscreen mode

where domain can be your server's assigned domain name or simply the IP address of your server.
After doing this it would ask you to for your password. After successful completion of the process you will be logged into the server.

Step-2 :

Next we would need to run a series of commands to install the necessary packages which would be required, the command are listed below:

sudo apt update
Enter fullscreen mode Exit fullscreen mode
sudo apt install python3-pip python3-dev nginx git
Enter fullscreen mode Exit fullscreen mode
sudo pip3 install virtualenv
Enter fullscreen mode Exit fullscreen mode

Step-3 :

After we are done with the step-2 ,we would need to clone our GitHub repository into the server. To clone the repository use the git clone command. for example:

git clone https://github.com/mayankt18/glugle

Step-4 :

In this step, we are going to setup the virtual environment for our project.

  • Switch into your project directory:
    cd projectdir

  • Create a new virtual environment, activate it and install the packages required into it using the commands listed below:

python3 -m venv env
Enter fullscreen mode Exit fullscreen mode
source ./env/bin/activate
Enter fullscreen mode Exit fullscreen mode
pip install -r requirements.txt
Enter fullscreen mode Exit fullscreen mode
  • Also we need to install gunicorn which would be our web server. To do so follow the commands:
pip install gunicorn
Enter fullscreen mode Exit fullscreen mode
  • Deactivate the virtual environment
deactivate
Enter fullscreen mode Exit fullscreen mode

Step-5 :

After installing the required packages in the virtual environment, we need to configure the gunicorn socket and service files to run our application.

  • First let us configure the gunicorn.socket file. Run the command to edit the socket file in nano.
sudo nano /etc/systemd/system/gunicorn.socket
Enter fullscreen mode Exit fullscreen mode

Now paste the following into the file

[Unit]
Description=gunicorn socket

[Socket]
ListenStream=/run/gunicorn.sock

[Install]
WantedBy=sockets.target
Enter fullscreen mode Exit fullscreen mode

Save the file using ctrl+s and exit nano using ctrl+x.

  • Let us now configure the gunicorn.service file:
sudo nano /etc/systemd/system/gunicorn.service
Enter fullscreen mode Exit fullscreen mode

and paste the following content into the file

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

[Service]
User=<username>
Group=www-data
WorkingDirectory=/home/<username>/<projectdir>
ExecStart=/home/<username>/<projectdir>/env/bin/gunicorn \
          --access-logfile - \
          --workers 3 \
          --bind unix:/run/gunicorn.sock \
          <application_name>.wsgi:application

[Install]
WantedBy=multi-user.target
Enter fullscreen mode Exit fullscreen mode

and don't forget to replace the <username> with your username, <projectdir> with the name of your project directory and <appliation_name> with the name of module in your django project which contains the wsgi.py file.

Save and exit nano as instructed before.

We are done with configuring the gunicorn configs and now we need to start and enable gunicorn workers. To do that use the commands below:

sudo systemctl start gunicorn.socket
Enter fullscreen mode Exit fullscreen mode
sudo systemctl enable gunicorn.socket
Enter fullscreen mode Exit fullscreen mode

Step-6 :

We need to configure nginx to communicate with our running application. To do so you would first need to create a new nginx config file.

  • Run the command below to create a new nginx config file and open it in nano
sudo nano /etc/nginx/sites-available/<application_name>
Enter fullscreen mode Exit fullscreen mode

replace the <application_name> with your application name

  • Paste the following in the nginx config with the required modifications.
server {
    server_name www.domain_name.com;

    location = /favicon.ico { access_log off; log_not_found off; }
    location /static/ {
        root /home/<username>/<projectdir>;
    }

    location / {
        include proxy_params;
        proxy_pass http://unix:/run/gunicorn.sock;
    }
}
Enter fullscreen mode Exit fullscreen mode
  • We would also need to create a link of the config file inside the /etc/nginx/sites-enabled/, to do so we need to run
sudo ln -s /etc/nginx/sites-available/<application_name> /etc/nginx/sites-enabled/
Enter fullscreen mode Exit fullscreen mode

and replace <application_name> with your app name.

  • Check the correctness of your nginx config file using the command
sudo nginx -t
Enter fullscreen mode Exit fullscreen mode

Correct Nginx config
if you see any result like above, you are good to go.

  • Now just restart the nginx daemon and we are all done.
sudo systemctl restart nginx
Enter fullscreen mode Exit fullscreen mode

Pre-Final step :

Go to your browser and type the domain name and check the working of your deployment.
If it is working Congratulations🎉!!! You have successfully deployed a Django project on a server. Now you can finally move to the final step.

Now, if you are among the people who are facing errors don't be discouraged. I am going to tell you the ninja way to identify and tackle the errors that might have crept.

  1. Run cat /var/log/nginx/error.log and see what error nginx is facing

  2. You can have a look at the running status of gunicorn server using sudo systemctl status gunicorn command, you would probably find if the server is running or not.

In most of the cases the above steps will help you identify the errors and provide you the error codes, you can have a look at them.

Pro Tip : If you make changes to your nginx config file, just do

sudo systemctl reload nginx
Enter fullscreen mode Exit fullscreen mode

for loading the changes.

Final Step

If you are at the final step i would like to thank you for patiently reading through my post.
If you liked this post and my writing style, do leave a like so i get encouragement to keep posting such articles and do leave your comments on the post. Even a đź‘Ť would mean a lot.

Top comments (0)