Setting Up Django 1.5 Application Using Gunicorn and Nginx


Setting Up Django 1.5 Application Using Gunicorn and Nginx

  1. Installing pip, and using it to install virtualenv.

  • $ sudo apt-get install python-setuptools
  • $ sudo easy_install pip

  • $ sudo pip install virtualenv

  1. Create new environtment with virtualenv.

  • $ mkvirtualenv myweb-env or virtualenv myweb-env

  1. The next step is to activate virtual environment:

  • $ cd myweb-env/

  • $ source bin/activate

  • Should now be seeing prompt changed

  • (myweb-env)~/home/userx/virtualEnv/myweb-env $

  1. Go to Django project directory

  • (myweb-env)~ /home/userx/webproject/myweb $

  1. Installing Gunicorn

  • (myweb-env)~ /home/userx/webproject/myweb $ sudo pip install gunicorn

  1. Updating settings.py to add Gunicorn.

INSTALLED_APPS = (

‘gunicorn’,

)

  1. Starting server using Gunicorn

  1. Now we can access website at http://127.0.0.1:8080

  2. Installing Ngix Web Server

  • $ sudo apt-get install nginx-full

  1. Configuring Nginx

  • Remove the default config #sudo rm -f /etc/nginx/sites-enabled/default

  • #sudo vim /etc/nginx/sites-available/myweb
server {
    listen       80;
    server_name  myweb.xx;
    access_log   /var/log/nginx/myweb .access.log;
    error_log    /var/log/nginx/myweb .error.log;
    root /srv/sites/neat/;

    location  /static/ {
        alias   /home/userx/myweb/static/;
    }

    location  / {
        proxy_pass            http://127.0.0.1:8080;
        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;
        client_max_body_size  10m;
    }

}
  • link the file :
  • ln -s /etc/nginx/sites-available/myweb /etc/nginx/sites-enabled/myweb

  1. Running Gunicorn

  1. Restarting Nginx

  • $ sudo service nginx restart

  1. Access site directly with gunicorn 127.0.0.1:8080

  2. Access site with Nginx 127.0.0.1 / localhost

  3. Setting up Virtual Host

  • $ sudo vim /etc/hosts

  • Add this following line or update existing line at /etc/hosts

  • 127.0.0.2 myweb.nn

  • Save updated hosts file

  • Open /etc/nginx/sites-available/myweb them update to sample below

server {
    listen       127.2:80;
    server_name  myweb.nn;
    access_log   /var/log/nginx/myweb .access.log;
    error_log    /var/log/nginx/myweb .error.log;
    root /srv/sites/neat/;

    location  /static/ {
        alias   /home/userx/myweb/static/;
    }

    location  / {
        proxy_pass            http://127.0.0.1:8080;
        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;
        client_max_body_size  10m;
    }

}
  • Now we can access site using url myweb.nn

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s