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 uwsgisudo systemctl start nginx
Thank You, Happy Coding!!!