nginx+gunicorn+django

来源:互联网 发布:react native app.js 编辑:程序博客网 时间:2024/06/08 03:50

1下载(略过)
2配置gunicorn:
2.1 编写gunicorn文件:
  gunicorn.sh

#!/bin/bashNAME="travel"                              #Name of the application (*)DJANGODIR=/path/to/app            # Django project directory (*)SOCKFILE=/path/to/app/run/gunicorn.sock    # we will communicate using this unix socket (*)NUM_WORKERS=3                          # how many worker processes should Gunicorn spawn (*)DJANGO_SETTINGS_MODULE=travel.settings         # which settings file should Django use (*)DJANGO_WSGI_MODULE=travel.wsgi                     # WSGI module name (*)echo "Starting $NAME as `whoami`"# Activate the virtual environmentcd $DJANGODIRsource /path/to/app/venv/bin/activateexport DJANGO_SETTINGS_MODULE=$DJANGO_SETTINGS_MODULEPATHONPATH=/path/to/venv/bin/python3export PYTHONPATH# Create the run directory if it doesn't existRUNDIR=$(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 /path/to/app/venv/bin/gunicorn ${DJANGO_WSGI_MODULE}:application \  --name $NAME \  --workers $NUM_WORKERS \  --bind=unix:$SOCKFILE  \  --log-level=debug#  --user $USER #  --bind=unix:$SOCKFILE 

3配置nginx
3.1修改/etc/nginx/nginx.conf:
删除其中关于server的定义,保留include;
在/etc/nginx/conf.d中添加自定义文件 app_nginx.conf

upstream test_server {  server unix:/path/to/app/run/gunicorn.sock fail_timeout=10s;}# This is not neccessary - it's just commonly used# it just redirects example.com -> www.example.com# so it isn't treated as two separate websitesserver {        listen 80;        server_name example.com;        return 301 $scheme://www.example.com$request_uri;        }server {    listen 80;    server_name www.example.com;    client_max_body_size 4G;    access_log /path/to/app/logs/nginx-access.log;    error_log /path/to/app/logs/nginx-error.log warn;    location /static/ {        autoindex on;        alias   /path/to/app/static/;    }    location /media/ {        autoindex on;        alias   /path/to/app/media/;    }    location / {        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://test_server;            break;        }    }    #For favicon    location  /favicon.ico {        alias /path/to/app/static/img/favicon.ico;    }    #For robots.txt    location  /robots.txt {        alias /path/to/app/static/robots.txt ;    }    # Error pages    error_page 500 502 503 504 /500.html;    location = /500.html {        root /path/to/app/static/;    }}

3.2

重新载入nginx.conf并重启服务:
nginx -s reload

service nginx restart

如果报错,错误为:
open() “/somewhere” failed (13: Permission denied)
需要检查当前SELinux mode:
getenforce
如果显示Enforcing,则通过输入setenforce 0将mode设置为Permissive
然后重新执行3.2步骤,不出意外的话则可以正常运行!

0 0