python+django+gunicorn+nginx的配置

来源:互联网 发布:人工智能市场规模 编辑:程序博客网 时间:2024/06/05 17:38

CentOS7下用gunicorn和nginx来部署python项目就比apache容易多了

python,django,gunicorn的安装及新建一个django项目上一节详细讲解了.

一些开发环境的依赖包需要安装,上一节也已经详细讲解了.

编译安装nginx.以稳定版的nginx1.8为例:

tar -zxvf nginx-1.8.0.tar.gzcd nginx-1.8.0./configure  # 默认安装在/usr/local/niginxmakemake install

安装完毕后,使用命令whereis nginx查看是否安装成功

配置nginx:

nginx的默认配置文件是/usr/local/nginx/conf/nginx.conf,为了个更灵活的配置和使用nginx,我们另外再新建一个配置文件.在项目文件p1里新建mime.types(文件内容为空)和nginx.conf,编辑nginx.conf:

user qiu;events {  worker_connections 1024;   accept_mutex off; }http {  include mime.types;  default_type application/octet-stream;  sendfile on;  upstream app_server {    server unix:/tmp/gunicorn.sock fail_timeout=0;  }  server {    listen 80;    client_max_body_size 4G;    server_name example.com www.example.com;    keepalive_timeout 5;    root /path/to/app/current/public;    location / {      try_files $uri @proxy_to_app;    }    location @proxy_to_app {      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;      proxy_set_header Host $http_host;      proxy_redirect off;      proxy_pass http://127.0.0.1:8888;    }    error_page 500 502 503 504 /500.html;    location = /500.html {      root /path/to/app/current/public;    }  }}

很多配置使用默认的就行,最主要的是把请求转发到8888端口.

启动nginx和gunicorn:

可以看到,配置文件里,nginx监听80端口,代理和转发8888端口.所以gunicorn使用8888端口就行了.
启动nginx:
/usr/local/nginx/sbin/nginx -c /home/qiu/workspace/p1/nginx.conf
在p1目录里启动gunicorn:
/usr/local/python2711/bin/gunicorn p1.wsgi:application -b 127.0.0.1:8888
然后在浏览器里输入[ip]/admin就可以看到django的管理界面了.

0 0
原创粉丝点击