Django application deployment with nginx and uwsgi (Ubuntu)

Deploy for Flask application follow here

Prequisite

  • Django
  • uwsgi
  • nginx

Setup your django project

https://docs.djangoproject.com/en/2.2/intro/tutorial01

Install uwsgi

pip3 install uwsgi

Install Nginx (Web Server)

sudo apt get install nginx

# check nginx is running or not
sudo systemctl status nginx.service

# output should be
● nginx.service - A high performance web server and a reverse proxy server
   Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
   Active: active (running) since Sat 2019-07-20 12:18:28 IST; 4 days ago
  Process: 1642 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
  Process: 1392 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
 Main PID: 1645 (nginx)
   CGroup: /system.slice/nginx.service
           ├─1645 nginx: master process /usr/sbin/nginx -g daemon on; master_process on
           ├─1646 nginx: worker process                           
           ├─1647 nginx: worker process                           
           ├─1648 nginx: worker process                           
           └─1649 nginx: worker process                           

Jul 20 12:18:20 think systemd[1]: Starting A high performance web server and a reverse proxy server...
Jul 20 12:18:28 think systemd[1]: Started A high performance web server and a reverse proxy server.

Configure uWSGI

Check your project working fine with uwsgi.

uwsgi --http :8000 --module mysite.wsgi

Create .ini configuration file

In the same project root directory, create a .ini file for uwsgi configuration. For example myproject.ini

# mysite_uwsgi.ini file
[uwsgi]

project_dir = %d

# Django-related settings
# the base directory (full path)
chdir           = %(project_dir)
# Django's wsgi file
module          = mysite.wsgi

# process-related settings
# master
master          = true
# maximum number of worker processes
processes       = 1
# the socket (use the full path to be safe
socket          = 127.0.0.1:1200
# ... with appropriate permissions - may be needed
# chmod-socket    = 664
# clear environment on exit
vacuum          = true

die-on-term = true

Save file and test ini file, run command in terminal.

uwsgi --ini mysite.ini

Create a uWSGI startup file

So that we can start/stop django application service, as we do for other system services.

create a systemctl startup file at location /etc/systemd/system. For example /etc/systemd/system/mysite.service

[Unit]

Description="uWsgi server instance for mysite"
After=network.target

[Service]
User={you-user}
Group=www-data
WorkingDirectory=/home/{you-user}/path/myproject/
Environment="PATH=/home/{you-user}/virtualenvs/mysite/bin"
ExecStart=/home/{you-user}/virtualenvs/myproject/bin/uwsgi --ini /home/{you-user}/path/myproject/mysite.ini

[Install]
WantedBy=multi-user.target

Start myproject service and check status

sudo systemctl start mysite.service

# Check the status
sudo systemctl status mysite.service

Configure Nginx

server {
    listen 80;
    server_name 127.0.0.1 localhost;

    location @application {
        include uwsgi_params;
        uwsgi_pass 127.0.0.1:1200;
    }
}

Test your Nginx configuration and restart Nginx.

nginx -t -c /etc/nginx/nginx.conf
# output should be
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

/etc/init.d/nginx restart

hat’s it. Open your localhost in browser and you will see application running with nginx and uwsgi.

Please refer for more detail

Django application deployment with nginx and uwsgi (Ubuntu)
Show Buttons
Hide Buttons