Configure uwsgi with nginx for Django applications

Date Posted: 11-05-2018

In this article, we’ll be explaining how to run uwsgi server with nginx for Django applications.

Prerequisties:-

1. python2.7
2. nginx
3. pip
4. uwsgi

Note: We are configuring this setup on RHEL 7.4. The same can be used for CentOS versions too. However for ubuntu system, you might need to use apt.

Installation:-

Run yum update

yum -y update

Install dependencies packages

yum install gcc openssl-devel bzip2-develpython-devel python-setuptools python-pip

1. Setup Python2.7

First install Python application,

cd /usr/src
wget https://www.python.org/ftp/python/2.7.13/Python-2.7.13.tar.xz
tar -xvf Python-2.7.13.tar.xz
cd Python-2.7.13/
./configure
make install 

Now verify python version

# python2.7 -V
Python 2.7.13

2. Install nginx

 yum install nginx

Make it start after server reboot
systemctl enable nginx

Start service

systemctl start nginx

Note: If you are using firewall, you need to allow port 80,443 to access sites. You will see nginx welcome page when you load http://your_server_ip_address/

3. Install Django & uWSGI

pip install django
pip install uwsgi

4. Configure website 

Create a new domain called web.pheonixsolutions.com. In our example, we used web.pheonixsolutions.com.

# cat /etc/nginx/conf.d/web.pheonixsolutions.com.conf
upstream web_pheonix {
     server unix:///var/www/pheonix/web_pheonix.sock; # for a file socket
}
server {
    # the port your site will be served on
    listen      80;
    # the domain name it will serve for
    server_name web.pheonixsolutions.com; # substitute your machine's IP address or FQDN
    charset     utf-8;

    # max upload size
    client_max_body_size 75M;   # adjust to taste
    location /media  {
        alias /var/www/pheonix/media;  # your Django project's media files - amend as required
    }

    location /static {
        alias /var/www/pheonix/static; # your Django project's static files - amend as required
    }

    # Finally, send all non-media requests to the Django server.
    location / {
        uwsgi_pass  web_pheonix;
        include     /etc/nginx/uwsgi_params; # the uwsgi_params file you installed
    }
}

Add the contents to uwsgi_params file

uwsgi_param  QUERY_STRING       $query_string;
uwsgi_param  REQUEST_METHOD     $request_method;
uwsgi_param  CONTENT_TYPE       $content_type;
uwsgi_param  CONTENT_LENGTH     $content_length;

uwsgi_param  REQUEST_URI        $request_uri;
uwsgi_param  PATH_INFO          $document_uri;
uwsgi_param  DOCUMENT_ROOT      $document_root;
uwsgi_param  SERVER_PROTOCOL    $server_protocol;
uwsgi_param  REQUEST_SCHEME     $scheme;
uwsgi_param  HTTPS              $https if_not_empty;

uwsgi_param  REMOTE_ADDR        $remote_addr;
uwsgi_param  REMOTE_PORT        $remote_port;
uwsgi_param  SERVER_PORT        $server_port;
uwsgi_param  SERVER_NAME        $server_name;

Reference link – http://uwsgi-docs.readthedocs.io/en/latest/tutorials/Django_and_nginx.html

Create uwsgi config file

vi web_pheonix.ini

# site_uwsgi.ini file
[uwsgi]

# Django-related settings
# the base directory (full path)
chdir           = /var/www/pheonix/
# Django's wsgi file
module          = pheonix.wsgi

# process-related settings
# master
master          = true
# maximum number of worker processes
processes       = 10
# the socket (use the full path to be safe
socket          = /var/www/pheonix/web_pheonix.sock
# ... with appropriate permissions - may be needed
chmod-socket    = 666
# clear environment on exit
vacuum          = true
#max-requests = 5000
#limit-as = 128
#daemonize =  /var/log/uwsgi-emperor.log

Run uWSGI service to test its working

uwsgi --ini web_pheonix.ini

Now run uWSGI in ‘emperor’ mode. In this mode it keeps an eye on a directory of uWSGI config files, and will spawn instances (‘vassals’) for each one it finds.

Whenever a config file is amended, the emperor will automatically restart the vassal.

sudo mkdir -p /etc/uwsgi/vassals

sudo mkdir -p /etc/uwsgi/vassals

uwsgi --emperor /etc/uwsgi/vassals

The website http://web.pheonixsolutions.com should work now.

Make uWSGI starts at server reboot

vi /opt/scripts/uwsgi_restart.sh

#!/bin/bash
/usr/local/bin/uwsgi --emperor /etc/uwsgi/vassals /var/log/uwsgi-emperor.log

Change the ownership of the file.

chmod +x /opt/scripts/uwsgi_restart.sh

Add the restart script at startup

vi /etc/rc.local

/opt/scripts/uwsgi_restart.sh

 

Leave a Reply