在我的AWS EC2主机上有3条路径:
1) /etc/nginx/ (包含nginx.conf文件)
2) /etc/主管/(包含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.socknginx.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:$SOCKFILEproject.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 logsupervisord.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安装主管”。创建了在venv/bin中可用的“supervisord”和“supervisorctl”
2) 'sudo mkdir -p /etc/主管/conf.d.d‘
3) 'sudo vim /etc/主管/con.d/project.conf‘
4)“sudo vim /etc/主管/监管人.sudo”
5) project.wsgi:application --绑定本地主机: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: 80746) sudo监督-c /etc/监督员/监管人
[No logs/Error. Prompt Return]7) sudo监督-c /etc/监督员/监管人
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/Superorord.conf start项目”
unix:///home/ec2-user/Project/run/gunicorn.sock refused connection你能告诉我如何解决这个问题吗?当我点击52.xx时,我想显示file1.html而不是/usr/share/nginx/html/index.html页面。目前,我正在看到403.html页面
发布于 2016-01-09 15:41:05
用于静态文件的。对于django项目,您需要安装应用服务器,如uwsgi或gunicorn
它将按照这样的逻辑工作:
user <-> nginx <-> app_server <-> django
这是关于nginx + gunicorn + django的指南
http://tutos.readthedocs.org/en/latest/source/ndg.html
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 reload
sudo supervisorctl status
sudo supervisorctl reread
sudo supervisorctl update
https://stackoverflow.com/questions/34695421
复制相似问题