Learn Code With Durgesh LogoLCWD
HomeCoursesBlogsContact Us
HomeCoursesBlogsContact Us
LearnCodeWithDurgesh Logo

Learn Code With Durgesh

Offering free & premium coding courses to lakhs of students via YouTube and our platform.

Explore

  • About Us
  • Courses
  • Blog
  • Contact

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Support

Contact

  • šŸ“ž +91-9839466732
  • [email protected]
  • Substring Technologies, 633/D/P256 B R Dubey Enclave Dhanwa Deva Road Matiyari Chinhat, Lucknow, UP, INDIA 226028
Ā© 2025 Made with ā¤ļø by Substring Technologies. All rights reserved.
Deploying Django using Ngnix and Gunicorn on Ubuntu

Deploying Django using Ngnix and Gunicorn on Ubuntu

By durgesh • Sun Jul 07 2024

Deploying Django using Ngnix and Gunicorn on Ubuntu

Deploying Django using Ngnix and Gunicorn on Ubuntu

image

First of all,Ā  I am going to install MySQL database because we are using it in our project

Installing MySQLĀ 

UpdateĀ 

sudo apt update

Then install the mysql-server package:

sudo apt install mysql-server

Ensure that the server is running using the systemctl start command:

sudo systemctl start mysql.service

Configure Mysql

mysql_secure_installation

We areĀ  not going to use root user , so we will create new userĀ 

Creating New MySQL User

mysql -u root -p

CREATE USER 'username'@'host' IDENTIFIED BY 'password';

GRANT ALL PRIVILEGES ON *.* TO ā€˜user’@'host’;

FLUSH PRIVILEGES;

exit

Now you can login with your username and password using command:

mysql -u username -p 

Ā 

Deploying Django with Nginx and Guicorn

Install python, pip and NgnixĀ 

sudo apt install python3-pip python3-dev nginx

Creating a python virtual environmentĀ 

sudo apt install virtualenv

Create project folderĀ 

cd /
mkdir projects
cd projects

I have created projects folder at root.

Create and activate the virtual env

virtual env 
source env/bin/activate

Upload the django projectĀ Ā 

I am going to clone the project from github as i have project on github.

git clone https://github.com/DurgeshCoder/lcwd-back.git

cd lcwd-back

pip3 install -r requirements.txt

Ā 

If you have any migrations then migrate and also put the correct database details in settings.py file

python3 manage.py migrate

Now test application is running on development server or not

python3 manage.py runserver 0.0.0.0:8000

Check using IP and Port 8000.

Now use GunicornĀ 

Let's create a system socket file for gunicorn now:

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

Paste the contents below and save the file

Description=gunicorn socket

[Socket]
ListenStream=/run/gunicorn.sock

[Install]
WantedBy=sockets.target```

Next, we will create a service file for gunicorn

sudo vim /etc/systemd/system/gunicorn.serviceĀ 

Paste the contents below inside this file:

Description=gunicorn daemon
Requires=gunicorn.socket
After=network.target

[Service]
User=ubuntu
Group=www-data
WorkingDirectory=/projects/lcwd-back
ExecStart=/projecgs/env/bin/gunicorn \
          --access-logfile - \
          --workers 3 \
          --bind unix:/run/gunicorn.sock \
          textutils.wsgi:application

[Install]
WantedBy=multi-user.target```

Lets now start and enable the gunicorn socket

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

Configurations of Ngnix

sudo vim /etc/nginx/sites-available/lcwd

Paste the below contents inside the file created

listen 80;
server_name www.learncodewithdurgesh.com;

    location = /favicon.ico { access_log off; log_not_found off; }
    location /static/ {
        root /projects/lcwd-back;
    }

    location /media/ {
        root /projects/lcwd-back;
    }

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

}

Activate the configuration using the following command:

sudo ln -s /etc/nginx/sites-available/lcwd /etc/nginx/sites-enabled/Ā 

Restart nginx and allow the changes to take place.

sudo systemctl restart nginx

Now check on port 80 your site will run.

Share this article ...

šŸ’¬WhatsAppšŸ“˜FacebookšŸ’¼LinkedIn🐦X

Trending Blogs...

Maven Tutorial

Maven Tutorial

Maven is a build automation tool primarily used for Java projects. It addresses two main aspects of building software: dependency management and project build lifecycle management.

Important DSA Topics for placements

Important DSA Topics for placements

Data Structures and Algorithms (DSA) are fundamental building blocks of computer science. A strong understanding of DSA is essential for every developer, regardless of their specialization.

Amazing Windows key shortcuts in Windows 11

Amazing Windows key shortcuts in Windows 11

Shortcuts are everyone’s favorite, be it in life or in Keyboard. We don’t know about life but we have some amazing shortcut tricks for your keyboard, which are newly introduced in windows 11. Let's have a look and understand these amazing shortcuts to reduce our finger pain

Share this article ...

šŸ’¬WhatsAppšŸ“˜FacebookšŸ’¼LinkedIn🐦X