[Django]-Setting up django with nginx on EC2

3๐Ÿ‘

I created my Django site on EC2 using Nginx and Gunicorn and These are the steps i followed

easy_install gunicorn
apt-get install nginx

nano /etc/init/site1.conf and added

description "site1 web server"
start on runlevel [2345]
stop on runlevel [06]
respawn
respawn limit 10 5
exec /home/scripts/gunicorn_runserver.sh

and in gunicorn_runserver.sh

#!/bin/bash
  set -e
  LOGFILE=/var/log/nginx/site1.log
  NUM_WORKERS=10
  # user/group to run as
  USER=www-data
  GROUP=adm
  cd /home/projects/project_name/
#  source ../../bin/activate
  exec gunicorn_django -w $NUM_WORKERS \
    --user=$USER --group=$GROUP --log-level=error \
    --log-file=$LOGFILE 2>>$LOGFILE

and in Nginx conf

upstream app_server_site1 {
    server localhost:8000 fail_timeout=0;
}

location  / {
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_redirect off;

    if (!-f $request_filename) {
            proxy_pass http://app_server_site1;
            break;
    }
}

finally

/etc/init.d/nginx restart
service site1 start

Detail description about Nginx+Django+Gunicorn here and Nginx+Django+Fcgi here

๐Ÿ‘คRakesh

Leave a comment