简体   繁体   中英

Hosting Django App on AWS EC2 Host

There are 3 paths on my AWS EC2 Host:

1) /etc/nginx/ (Contains nginx.conf File)

2) /etc/supervisor/ (Contains supervisord.conf & project.conf files)

3) /home/ec2-user/Project/ (Contains gunicorn_start.sh & project related files)

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

nginx.conf File(updated):

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/;
        }
}

gunicorn_start.sh File:

#!/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

project.conf File:

[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

supervisord.conf File:[Updated]

[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

Access Logs:

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 "-" "-"

Error Logs:

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 Update/Host Changes:
1) 'pip install supervisor'. [created 'supervisord' & 'supervisorctl' available in venv/bin]

2) 'sudo mkdir -p /etc/supervisor/conf.d'

3) 'sudo vim /etc/supervisor/conf.d/project.conf'

4) 'sudo vim /etc/supervisor/supervisord.conf'

5) gunicorn project.wsgi:application --bind localhost:8000

[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

6) sudo supervisord -c /etc/supervisor/supervisord.conf

[No logs/Error. Prompt Return]

7) sudo supervisorctl -c /etc/supervisor/supervisord.conf

unix:///home/ec2-user/Project/run/gunicorn.sock refused connection
supervisor> 
supervisor> reload
Really restart the remote supervisord process y/N? y
error: <class 'socket.error'>, [Errno 111] Connection refused: file: /usr/lib64/python2.7/socket.py line: 228
supervisor>

8) 'sudo supervisorctl -c /etc/supervisor/supervisord.conf start project'

unix:///home/ec2-user/Project/run/gunicorn.sock refused connection

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]

Nginx used for static files. For django project you need to install application server like uwsgi or gunicorn
It will work by this logic:
user <-> nginx <-> app_server <-> django
Here is guide for nginx + gunicorn + django
http://tutos.readthedocs.org/en/latest/source/ndg.html

UPD for comments
some config for you, maybe it helps.
Nginx

server {
listen 80;
server_name your_public_ip;
access_log  /var/log/nginx/your_nginx.log;

location /media  {
    alias /home/ubuntu/Project/project/media;     
    #It is path to your media, you can check this by 'pwd' command
}

location /static {
    alias /home/ubuntu/Project/project/static_root;
}
    #same
location / {
    proxy_pass http://127.0.0.1:8000;
    proxy_set_header Host $server_name;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}

install gunicorn and make it autostart by supervisor
sudo apt-get install supervisor
open file sudo vim /etc/supervisor/conf.d/project.conf
and write this config

[program:project] #by this name you will call supervisor
command=/home/ubuntu/venv/bin/gunicorn --bind localhost:8000 project.wsgi:application    
enviroment=PYTHONPATH=/home/ubuntu/venv/bin    #path to virtualenv
directory=/home/ubuntu/project_folder/project  #you path to project
user=ubuntu  #your user

start it sudo supervisorctl start project

also comand for supervisor:
sudo supervisorctl reload
sudo supervisorctl status
sudo supervisorctl reread
sudo supervisorctl update

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM