在 AWS EC2 主机上托管 Django 应用程序

Posted

技术标签:

【中文标题】在 AWS EC2 主机上托管 Django 应用程序【英文标题】:Hosting Django App on AWS EC2 Host 【发布时间】:2016-04-14 05:17:35 【问题描述】:

我的 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)“点安装主管”。 [在 venv/bin 中创建了 'supervisord' 和 'supervisorctl']

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 启动项目'

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】:

Nginx 用于静态文件。对于 django 项目,您需要安装应用程序服务器,例如 uwsgigunicorn 它将按以下逻辑运行:user &lt;-&gt; nginx &lt;-&gt; app_server &lt;-&gt; django 这是 nginx + gunicorn + django 的指南http://tutos.readthedocs.org/en/latest/source/ndg.html

用于 cmets 的 UPD 给你一些配置,也许有帮助。 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;


安装 gunicorn 并由主管自动启动sudo apt-get install supervisor 打开文件 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 reloadsudo supervisorctl statussudo supervisorctl rereadsudo supervisorctl update

【讨论】:

“项目”路径是否必须为:/var/www/? 当我尝试从路径 '/home/ec2-user/project/' 运行 'gunicorn project.wsgi:application' 时,它会抛出一个很长的错误跟踪:venv/local/lib/ python2.7/site-packages/gunicorn/arbiter.py",第 515 行,在 spawn_worker worker.init_process() ImportError: No module named wsgi No module named wsgi wsgi 它是 django 中的应用程序文件的名称,包含在项目文件夹中,靠近 settings.py,你从错误的路径中调用它。因此,为艰巨的任务做好准备。当我第一次上传时,我整天都在这样做。我建议您逐步完成教程中的所有事情,以及您将拥有的谷歌错误 当我尝试命令:'systemctl enable gunicorn_project'时,它说 -bash: systemctl: command not found。您能否就如何安装它提供一些替代/建议? 你可以在google上查,这个问题,会在第一页。但老实说我不使用它,你停留在哪一步?你需要配置nginx,配置gunciorn,然后在supervisor中添加这个,phoro自动启动。您可以查看另一个指南来解决这个问题,当我第一次这样做时,我会阅读 3 或 4 个指南 :)

以上是关于在 AWS EC2 主机上托管 Django 应用程序的主要内容,如果未能解决你的问题,请参考以下文章

AWS EC2 免费套餐 django 应用程序

FCM 与 Prase-server 托管在 AWS EC2 上

在 AWS EC2 实例上使用 nginx 配置 django

如何调试在 AWS EC2 中运行的 Django 应用程序

将托管在 AWS EC2 上的 Asp.net Web api 连接到 AWS API Gateway

如何在运行 Ubuntu 的 AWS EC2 上配置多个虚拟主机? [关闭]