Serve Django Application with Nginx and uwsgi

/var/www/nginx_django/project is the path to django project.

mysite is the name of my django project.

Install Nginx and configure Nginx

To install the Nginx: https://www.nginx.com/resources/wiki/start/topics/tutorials/install/

Configuration file path for nginx: /etc/nginx/sites-available/django.conf

Now, enable the nginx configuration file you created, so that nginx can read:

  • sudo ln -s /etc/nginx/sites-available/django.conf /etc/nginx/sites-enabled/

After linking the files, reload Nginx to reflect the change and enable the server block’s configuration file:

  • sudo systemctl reload nginx

If you don’t have a static and media folder in your django project directory, then you should create it.

Restart the service again:

  • sudo systemctl restart nginx

Configure the uwsgi:

Run the uwsgi using pip

  • pip3 install uwsgi

Create the uwsgi directory, if not created already, inside /etc directory.

Create an emperor.ini in /etc/uwsgi directory.

Create a directory named vassals in /etc/uwsgi directory.

Create a .ini file in /etc/uwsgi/vassals for your django application, e.g. mysite.ini(You can give any name to this file)

Here,

  • pythonpath is the path where my wsgi.py file exists.
  • chdir is the path where my manage.py file exists.
  • home is the path where my environment directory named env exists.

Configure service file for uwsgi:

Create a service file uwsgi.service in /etc/systemd/system/ directory.

Create a user named uwsgi.

  • useradd -s /bin/false -r uwsgi

Create a directory sock in /opt.

  • mkdir /opt/uwsgi

Create directory uwsgi in /opt/sock

  • mkdir /opt/uwsgi/sock

Make uwsgi(user you created before) owner of uwsgi directory.

  • sudo chown uwsgi:uwsgi -R /opt/uwsgi

Start the services.

  • sudo systemctl start uwsgi
  • sudo systemctl start nginx

--

--

--

DevOps Engineer

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Most Important Java Developer Skills

UppLabs created Healthcare App for preventing hair loss

The Autumn 2020 Batch of Alibaba Cloud MVP Application Opens Now!

Power BI: Datasets settings and its workload

Graviton Exploit Exploration Mission

Taming Messaging on Red Hat AMQ with Red Hat Quarkus

Why is Java so dumb?

Using Cloudflare Origin Certificates with Azure App Services

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Parth Vijay

Parth Vijay

DevOps Engineer

More from Medium

Install Node.js using NVM on Ubuntu

How To Install and Use Docker on Ubuntu 20.04

Install Docker and Docker-Compose on Linux

Create a simple custom image