DEV Community

Gias Uddin
Gias Uddin

Posted on • Edited on

Django application deployment by using uwsgi, Nginx

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

you have successfully installed the library in your pc now we can start the deployment.

Creating a Django project

django-admin startproject mysite
Enter fullscreen mode Exit fullscreen mode

Test your Django project.

make sure that your mysite project works:

python manage.py runserver 0.0.0.0:8000

Enter fullscreen mode Exit fullscreen mode

And if that works, run it using uWSGI:

uwsgi --http :8000 --module mysite.wsgi
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Run application, make sure you are in the project root directory

uwsgi --ini uwsgi.ini

Enter fullscreen mode Exit fullscreen mode

make sure uWSGI is up

ps axu | grep uwsgi
Enter fullscreen mode Exit fullscreen mode

reload uwsgi

uwsgi --reload pid_5000.pid
Enter fullscreen mode Exit fullscreen mode

stop uWsgi

uwsgi --stop pid_5000.pid

Enter fullscreen mode Exit fullscreen mode

Now we will configure Nginx, create mysite conf

touch /etc/nginx/sites-available/mysite.conf
Enter fullscreen mode Exit fullscreen mode

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;
        }
}
Enter fullscreen mode Exit fullscreen mode

make available your conf in Nginx sites enable

sudo ln -sf /etc/nginx/sites-available/mysite.conf /etc/nginx/sites-enabled/mysite.conf
Enter fullscreen mode Exit fullscreen mode

restart Nginx


sudo /etc/init.d/nginx restart
Enter fullscreen mode Exit fullscreen mode

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. You can also learn more about me and my work by visiting my Giasuddin Bio and following me on LinkedIn and Twitter. Thank you for your support!

Top comments (0)