在AWS EC2主机上托管Django App

时间:2016-01-09 15:37:34

标签: django nginx amazon-ec2 gunicorn supervisord

我的AWS EC2主机上有3条路径:

1)/ etc / nginx /(包含nginx.conf文件)

2)/ etc / supervisor /(包含supervisord.conf和project.conf文件)

3)/ home / ec2-user / Project /(包含gunicorn_start.sh&项目相关文件)

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文件(已更新):

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文件:

#!/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文件:

[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文件:[更新]

[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

访问日志:

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

错误日志:

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"

代码更新/主机更改:
1)' pip install supervisor'。 [创建' supervisord' &安培; ' supervisorctl'在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)&#39; sudo supervisorctl -c /etc/supervisor/supervisord.conf启动项目&#39;

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

您能告诉我们如何解决这个问题吗? [当我点击52.xx.xx.xx时,我想显示file1.html而不是/usr/share/nginx/html/index.html页面。目前,我正在看403.html页面]

1 个答案:

答案 0 :(得分:2)

Nginx用于静态文件。对于django项目,您需要安装应用程序服务器,如gunicornuser <-> nginx <-> app_server <-> django
它将通过这种逻辑起作用:
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; } }
这是nginx + gunicorn + django的指南 http://tutos.readthedocs.org/en/latest/source/ndg.html

UPD征求意见
一些配置给你,也许它有帮助。
Nginx

sudo apt-get install supervisor

安装gunicorn并由主管自动启动 sudo vim /etc/supervisor/conf.d/project.conf
打开文件 [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
并编写此配置

sudo supervisorctl start project

开始吧 sudo supervisorctl reload

还要求主管:
sudo supervisorctl status
sudo supervisorctl reread
sudo supervisorctl update
{{1}}