Django application deployment by using uwsgi, Nginx
Tags: django, devops, python
Published: 2022-11-26 10:52:17

This blog is about how to deploy a Django application by using uwsgi Nginx.

Dependency needed:

Ubuntu 18.04
Python 3
Django > 2
Nginx

Install required packages using apt

```
sudo apt install nginx
sudo apt update
sudo apt install -y python3-pip
sudo apt install build-essential libssl-dev libffi-dev python3-dev
```
Let’s assume you already know python virtual environment and you already install in your pc.
Install required packages using pip

```
pip install django
pip install uwsgi
```
you have successfully installed the library in your pc now we can start the deployment.

Creating a Django project

```
django-admin startproject mysite
```
Test your Django project.

make sure that your mysite project works:

```
python manage.py runserver 0.0.0.0:8000

```
And if that works, run it using uWSGI:

```
uwsgi --http :8000 --module mysite.wsgi
```
Testing is done, now our application will run in demon mode.

create sock file and log with proper permission

```
touch /tmp/mysite.sock
touch /var/log/mysite.log
```
create a django_uwsgi.ini in the project root directory.

```
[uwsgi]
# Django-related settings
# the base directory (full path)
chdir=/var/www/html/mysite
# Django's wsgi file
module=mysite.wsgi
# the virtualenv (full path)
home=/root/envDjango
# process-related settings
# master
master          = true
# maximum number of worker processes
processes       = 10
# the socket (use the full path to be safe
socket          = /tmp/mysite.sock
# ... with appropriate permissions - may be needed
#chmod-socket    = 666
#uid = www-data
#gid = www-data
env = DJANGO_SETTINGS_MODULE=bdalljob.settings
# clear environment on exit
vacuum          = true
# for graceful reloading project
pidfile=./pid_5000.pid
# uwsgi log write in this location
daemonize=/var/log/mysite.log
#max-requests = 5000
```
Run application, make sure you are in the project root directory

```
uwsgi --ini uwsgi.ini

```
make sure uWSGI is up

```
ps axu | grep uwsgi
```
reload uwsgi

```
uwsgi --reload pid_5000.pid
```
stop uWsgi

```
uwsgi --stop pid_5000.pid

```
Now we will configure Nginx, create mysite conf

```
touch /etc/nginx/sites-available/mysite.conf
```

open mysite by using nano mysite.conf add below configuration

```
server {
        listen       80;
        server_name  mysite.com;
        charset utf-8;
location / {
            include             uwsgi_params;
            uwsgi_pass          unix:/tmp/mysite.sock;
            proxy_redirect      off;
            proxy_set_header    Host $host;
            proxy_set_header    X-Real-IP $remote_addr;
            proxy_set_header    X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header    X-Forwarded-Host $server_name;
        }
}
```
make available your conf in Nginx sites enable

```
sudo ln -sf /etc/nginx/sites-available/mysite.conf /etc/nginx/sites-enabled/mysite.conf
```
restart Nginx

```

sudo /etc/init.d/nginx restart
```
now you can check mysite.com by using it in the host file.

Thank you for reading my article! If you enjoyed it and would like to support my work, please consider buying me a coffee at **[Buy Me a Coffee](https://www.buymeacoffee.com/giasuddin)**. You can also learn more about me and my work by visiting my **[Giasuddin Bio ](https://bio.link/giasuddi)**and following me on LinkedIn and Twitter. Thank you for your support!