title: Hosting Django App on AWS EC2 Host
body: <p>There are 3 paths on my AWS EC2 Host:</p>
  
  <p>1) /etc/nginx/ (Contains nginx.conf File)</p>
  
  <p>2) /etc/supervisor/ (Contains supervisord.conf &amp; project.conf files)</p>
  
  <p>3) /home/ec2-user/Project/ (Contains gunicorn_start.sh &amp; project related files)</p>
  
  <pre><code>Project
  |-- gunicorn_start.sh
  |-- project
  |   |-- db.sqlite3
  |   |-- manage.py
  |   |-- miscellaneousConfig
  |   |-- README.md
  |   |-- static
  |      |-- 403.html
  |      |-- 50x.html
  |      |-- css
  |      |-- img
  |      |-- js
  |   |-- abc1
  |      |-- admin.py
  |      |-- __init__.py
  |      |-- migrations
  |         |-- __init__.py
  |      |-- models.py
  |      |-- templates
  |      |   `-- abc1
  |      |       |-- file1.html
  |      |       `-- file2.html
  |      |-- tests.py
  |      |-- urls.py
  |      `-- views.py
  |   |-- abc2
  |      |-- functions.py
  |      |-- __init__.py
  |      |-- settings.py
  |      |-- urls.py
  |      |-- views.py
  |      `-- wsgi.py
  |-- logs
  |   |-- gunicorn_supervisor.log
  |   |-- nginx-access.log
  |   `-- nginx-error.log
  |-- run
  |   `-- gunicorn.sock
  </code></pre>
  
  <p>nginx.conf File(updated):</p>
  
  <pre><code>upstream app_server {    
    server unix:/home/ec2-user/Project/run/gunicorn.sock fail_timeout=0;
  }
  server {
          listen       80;
          server_name  localhost;
          root         /usr/share/nginx/html;
  
          location / {
            proxy_pass http://127.0.0.1:8000;
            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;
                  break;
              }
          }
  
          location /static/ {
            alias /home/ec2-user/Project/project/static/;
          }
  
          error_page 403 404 /404.html;
          location = /40x.html {
             root /home/ec2-user/Project/project/static/;
          }
  }
  </code></pre>
  
  <p>gunicorn_start.sh File:</p>
  
  <pre><code>#!/bin/bash
  
  NAME="abc2"                              #Name of the application (*)
  DJANGODIR=/home/ec2-user/Project/project            # Django project directory (*)
  SOCKFILE=/home/ec2-user/Project/run/gunicorn.sock        # we will communicate using this unix socket (*)
  USER=ec2-user                                        # the user to run as (*)
  GROUP=webdata                                     # the group to run as (*)
  NUM_WORKERS=3                                     # how many worker processes should Gunicorn spawn (*)
  DJANGO_SETTINGS_MODULE=abc2.settings             # which settings file should Django use (*)
  DJANGO_WSGI_MODULE=abc2.wsgi                     # WSGI module name (*)
  
  echo "Starting $NAME as `whoami`"
  
  # Activate the virtual environment
  cd $DJANGODIR
  source /home/ec2-user/Project/venv/bin/activate
  export DJANGO_SETTINGS_MODULE=$DJANGO_SETTINGS_MODULE
  export PYTHONPATH=$DJANGODIR:$PYTHONPATH
  
  # Create the run directory if it doesn't exist
  RUNDIR=$(dirname $SOCKFILE)
  test -d $RUNDIR || mkdir -p $RUNDIR
  
  # Start your Django Unicorn
  # Programs meant to be run under supervisor should not daemonize themselves (do not use --daemon)
  exec /home/ec2-user/Project/venv/bin/gunicorn ${DJANGO_WSGI_MODULE}:application \
    --name $NAME \
    --workers $NUM_WORKERS \
    --user $USER \
    --bind=unix:$SOCKFILE
  </code></pre>
  
  <p>project.conf File: </p>
  
  <pre><code>[program:project]               # by this name you will call supervisor
  command=/home/ec2-user/Project/venv/bin/gunicorn --bind localhost:8000 project.wsgi:application
  enviroment=PYTHONPATH=/home/ec2-user/Project/venv/bin  # path to virtualenv
  directory=/home/ec2-user/Project/project  # you path to project
  autostart=true
  autorestart=true
  stdout_logfile = /home/ec2-user/Project/logs/gunicorn_supervisor.log;        # Where to write log messages
  redirect_stderr = true ;          # Save stderr in the same log
  </code></pre>
  
  <p>supervisord.conf File:[Updated]</p>
  
  <pre><code>[include]
  files = /etc/supervisor/conf.d/*.conf
  
  [inet_http_server]
  port = 9001
  username = ec2-user     # Basic auth username
  password =              # Basic auth password
  
  [supervisord]
  logfile = /tmp/supervisord.log
  logfile_maxbytes = 50MB
  logfile_backups=10
  loglevel = info
  pidfile = /tmp/supervisord.pid
  nodaemon = false
  minfds = 1024
  minprocs = 200
  umask = 022
  user = ec2-user
  identifier = supervisor
  directory = /tmp
  nocleanup = true
  childlogdir = /tmp
  strip_ansi = false
  
  [supervisorctl]
  serverurl = unix:///home/ec2-user/Project/run/gunicorn.sock
  username = ec2-user
  </code></pre>
  
  <p>Access Logs:</p>
  
  <pre><code>122.xxs.xxx.xx - - [10/Jan/2016:19:41:32 +0000] "GET / HTTP/1.1" 502 3795 "-" "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:43.0) Gecko/20100101 Firefox/43.0"
  122.xxx.xxx.xx - - [10/Jan/2016:19:41:33 +0000] "GET /%7B%%20static%20'poweredby.png'%20%%7D HTTP/1.1" 400 172 "-" "-"
  122.xxx.xxx.xx - - [10/Jan/2016:19:41:33 +0000] "GET /%7B%%20static%20'nginx-logo.png'%20%%7D HTTP/1.1" 400 172 "-" "-"
  </code></pre>
  
  <p>Error Logs:</p>
  
  <pre><code>2016/01/10 18:59:04 [error] 3388#0: *1 connect() to unix:/home/ec2-user/Project/run/gunicorn.sock failed (111: Connection refused) while connecting to upstream, client: xxx.xx.xx.xx, server: 52.xx.xx.xx, request: "GET / HTTP/1.1", upstream: "http://unix:/home/ec2-user/Project/run/gunicorn.sock:/", host: "52.xx.xx.xx"
  </code></pre>
  
  <p>Code Update/Host Changes:<br>
  1) 'pip install supervisor'. [created 'supervisord' &amp; 'supervisorctl' available in venv/bin]</p>
  
  <p>2) 'sudo mkdir -p /etc/supervisor/conf.d'</p>
  
  <p>3) 'sudo vim /etc/supervisor/conf.d/project.conf'</p>
  
  <p>4) 'sudo vim /etc/supervisor/supervisord.conf'</p>
  
  <p>5) gunicorn project.wsgi:application --bind localhost:8000</p>
  
  <pre><code>[2016-01-12 05:41:49 +0000] [8069] [INFO] Starting gunicorn 19.4.3
  [2016-01-12 05:41:49 +0000] [8069] [INFO] Listening at: http://127.0.0.1:8000 (8069)
  [2016-01-12 05:41:49 +0000] [8069] [INFO] Using worker: sync
  [2016-01-12 05:41:49 +0000] [8074] [INFO] Booting worker with pid: 8074
  </code></pre>
  
  <p>6) sudo supervisord -c /etc/supervisor/supervisord.conf</p>
  
  <pre><code>[No logs/Error. Prompt Return]
  </code></pre>
  
  <p>7) sudo supervisorctl -c /etc/supervisor/supervisord.conf</p>
  
  <pre><code>unix:///home/ec2-user/Project/run/gunicorn.sock refused connection
  supervisor&gt; 
  supervisor&gt; reload
  Really restart the remote supervisord process y/N? y
  error: &lt;class 'socket.error'&gt;, [Errno 111] Connection refused: file: /usr/lib64/python2.7/socket.py line: 228
  supervisor&gt;
  </code></pre>
  
  <p>8) 'sudo supervisorctl -c /etc/supervisor/supervisord.conf start project'</p>
  
  <pre><code>unix:///home/ec2-user/Project/run/gunicorn.sock refused connection
  </code></pre>
  
  <p>Can you please tell how to resolve this issue ? 
  [I want to display file1.html instead of /usr/share/nginx/html/index.html page when I hit 52.xx.xx.xx. Currently, I am seeing 403.html page]</p>