Tuesday 3 May 2016

Host your Django Website In Ubuntu Server With Ngnix and Uwsgi.

Introduction 

What is Ngnix ?

     Nginx (pronounced engine-x) is a free, open-source, high-performance HTTP server and reverse proxy, as well as an IMAP/POP3 proxy server. Igor Sysoev started development of Nginx in 2002, with the first public release in 2004. Nginx now hosts nearly 12.18% (22.2M) of active sites across all domains. Nginx is known for its high performance, stability, rich feature set, simple configuration, and low resource consumption.



What is Uwsgi ?

      The uWSGI project aims at developing a full stack for building hosting services.applicaton servers( for various programing languages and protocols),proxies , process managers and monitors are all implemented using a common api and common configuration style.



Working Diagram for Ngnix, uWSGI with Python frameworks.



Request and Response Diagram



Configuration  

First install required applications:

sudo apt-get install nginx uwsgi uwsgi-plugin-python python-virtualenv, Here we are installing all the packages in single command ( ngnix , uwsgi and uwsgi packages and virtual env ) .I think you guys have ready  project in Django . Now I'm going to explain about the configuration for Hosting our django project with Ubuntu Server.

Versions of packages that will be used:
  • Nginx 
  • Uwsgi
  • Virtualenv
  • Django 1.7.7

Step 1 :  Activate your virtual env and run your django project ,

Example : 
  •  cd `/myprojects/
  •  virtualenv  global
  •  source gobal/bin/activate
  •  pip install django 
  •  django-admin.py startprojeect logiconline
Step 2 : Nginx configuration.

open Ubuntu terminal 

        Configuration files are stored in /etc/ngnix/site-available. Go to this Directory and create new file for our project logiconline.

  • cd /etc/nginx/site-available
Example . create a file

sudo subl logiconline.in  ( This command will create  a file for project , subl( Sublime text editor )


Example configuration :

server {
     listen  80;
    server_name logiconline.in www.logiconline.in;
    access_log /var/log/nginx/logiconline.in_access.log;
    error_log /var/log/nginx/logiconline.in_error.log;

    location / { 
        uwsgi_pass  unix:///tmp/logiconline.in.sock;
        include     uwsgi_params;
    }   

    location /media/  {
         alias /home/myprojects/logiconline/media/;
    }

    location  /static/ {
        alias /home/myprojects/logiconline/static/;
    }
}

We must  to enable this file in Ngnix conf

cd /etc/ngnix/sites-enabled

ln -s ../sites-available/logiconline.in.

Uwsgi configuration 

Like with Nginx.. configuration files are stored in /etc/uwsgi/apps-available. Go  to this directory and create a new file.

  • cd /etc/uwsgi/apps-available 
sudo subl logiconline.in.ini

Example : Uwsgi File 

[uwsgi]
vhost = true
plugins = python
socket = /tmp/logiconline.in.sock
master = true
enable-threads = true
processes = 2
wsgi-file = /home/myprojects/logiconline/logiconline/wsgi.py
virtualenv = /home/global
chdir = /home/myprojects/logiconline
touch-reload = /home/myprojects/logiconline/reload

Enable this.

cd /etc/uwgi/apps-enabled/
ln -s ../apps-available/logiconline.in.ini

Step 3 : That's all. Now, run this services.

  • sudo service nginx start
  • sudo service uwsgi start




No comments:

Post a Comment