How to run django with nginx

In this tutorial we will learn how to configure nginx with django project in Ubuntu 12.04 LTS .
Here in this scenario I have already putted the Django Project code in /opt and before this I installed django from its source code.

Actually in this scenario the code was developed in staging machine and I moved it to live server.In your case it may be in your own server

Project Directory Location : /opt/myproject
Python Version : Python 2.7
Django Version 1.5.1


Follow the given below steps to move your project code in new server or /opt in same server

Download the django and install it

wget https://www.djangoproject.com/m/releases/1.5/Django-1.5.1.tar.gz
tar -xzvf Django-1.5.1.tar.gz
cd Django-1.5.1
sudo python setup.py install


Now copy your project code to /opt
(Python modules you have to install as per your project requirement) I transfer my project code through scp into new server and copied it to /opt

cp -rvf /root/myproject /opt

Follow the given below steps to configure with nginx

Step 1: Install nginx and python flup

apt-get -y install nginx python-flup

Step 2: Create a file in /etc/nginx/sites-available with the name
myproject.conf

vi /etc/nginx/sites-available/myproject.conf


server {
    listen 80;
    server_name ubuntu.example.com;
    charset     utf-8;
    root   /opt/myproject/;
    access_log /var/log/nginx/ubuntu.example.com.access.log;
    error_log /var/log/nginx/ubuntu.example.com.error.log;
 
    location / {
        include fastcgi_params;
        fastcgi_pass ubuntu.example.com:8080;
         fastcgi_split_path_info ^()(.*)$;
    }
 
     location ~* .(ico|css|js|gif|jpe?g|png|svg|flv)(?[0-9]+)?$ {
     expires max;
     log_not_found off;
     }
}

Step 3: Create a symlink to enable the new site

 ln -s /etc/nginx/sites-available/myproject.conf /etc/nginx/sites-enabled/myproject.conf 

Step 4: Now run the fastcgi

Note: Python Version is 2.7

Here manage.py is main python file through which the project will run. Contact to your python developer and ask him to change the host url in file.

python /opt/myproject/manage.py runfcgi host=http://ubuntu.example.com port=8080

Step 5: Now restart the Nginx

 /etc/init.d/nginx restart 

Now open the web-browser and check the site.
in my scenario link was http://ubuntu.example.com

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.