LCWD LogoLearn Code With DurgeshLCWD
HomeCoursesHandbooksBlogsContact
About LCWDAbout Durgesh Tiwari
Flex Box Hero
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
  • FlexBox Game

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 durgeshkumar8896 • Sun Jul 07 2024

Deploying Django using Ngnix and Gunicorn on Ubuntu

Deploying Django using Ngnix and Gunicorn on Ubuntu

learn code with durgesh images

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

iphone 17 Launch 2025: Features, Price in India, Pros & Cons

iphone 17 Launch 2025: Features, Price in India, Pros & Cons

Apple has officially unveiled the **iPhone 17 series** on **September 9, 2025**, at its **“Awe Dropping” event**. The lineup includes **iPhone 17, iPhone 17 Air, iPhone 17 Pro, and iPhone 17 Pro Max**. With **120 Hz displays across all models, ultra-thin design, A19 chips, and camera upgrades**, Apple is raising the bar again.

Async/Await vs Promises in JavaScript: Which Should You Use?

Async/Await vs Promises in JavaScript: Which Should You Use?

Asynchronous programming is one of the most important topics in modern JavaScript development. Whether you’re building a web app, mobile application, or integrating APIs, you’ll often deal with operations that don’t finish instantly, such as fetching data or reading files.

AI: Boon or Curse? Advantages & Disadvantages Explained

AI: Boon or Curse? Advantages & Disadvantages Explained

Artificial Intelligence (AI) is no longer just a concept of the future—it is already shaping our present. From asking Alexa or Siri to play music, using Google Maps for directions, or experiencing personalized recommendations on Netflix, AI has become a part of our everyday life.

The Future of Technology: Quantum Computing and Its Ecosystem

The Future of Technology: Quantum Computing and Its Ecosystem

Quantum computing isn’t just another buzzword—it’s a completely new way of thinking about computers. Unlike our everyday laptops or phones that run on classical computing, quantum computers use the strange but powerful rules of quantum mechanics.

Tailwind CSS Cheat Sheet – Complete Utility Class Guide 2025

Tailwind CSS Cheat Sheet – Complete Utility Class Guide 2025

Tailwind CSS Cheat Sheet – Complete Utility Class Guide 2025

Expert-Level JavaScript Interview Q&A for 2025: Crack Advanced JS Interviews

Expert-Level JavaScript Interview Q&A for 2025: Crack Advanced JS Interviews

Get ready for tough coding interviews with this collection of advanced JavaScript interview questions and answers. Designed for experienced developers who want to master complex JS concepts and impress recruiters.

Share this article ...

💬WhatsApp📘Facebook💼LinkedIn🐦X