1. 程式人生 > >nginx+django+gunicorn+gevent+supervisor

nginx+django+gunicorn+gevent+supervisor

nginx django gunicorn gevent

安裝環境介紹:

python2.7.10

django1.7.9

linux CentOS release 6.5 (Final) 64


假設我的項目位置為/var/www/myweb

服務器IP為192.168.0.100


1、插件安裝

gunicorn-19.7.1 uwsgi組件

gevent-1.2.1 異步組件

greenlet-0.4.12 異步組件

supervisor-3.3.0 進程管理組件


直接下載然後運行 對應的 python setup.py install


2、myweb wsgi 配置

位置: /var/www/myweb/wsgi.py

import sys

import os

p = os.path.abspath(os.path.dirname(__file__))

sys.path.insert(0, os.path.split(p)[0])

os.environ["DJANGO_SETTINGS_MODULE"] = "settings"

from django.core.wsgi import get_wsgi_application

application = get_wsgi_application()


3、運行 gunicorn

cd /var/www/myweb

/usr/local/python/bin/gunicorn -w 4 -k gevent -m 0002 -b 0.0.0.0:8000 --error-logfile /var/log/gunicorn/myweb_gunicorn_error.log wsgi:application

-w 啟動多少個工作進程,可以根據CPU的個數開啟,一個CPU開一個

-k 工作進程類

-m umask權限

-b 地址端口配置

wsgi:application wsgi實例

對於直接運用gunicorn不通過supervisor的話,可以守護的方式啟動,加入參數 -D

/usr/local/python/bin/gunicorn -w 4 -k gevent -m 0002 -b 0.0.0.0:8000 --error-logfile /var/log/gunicorn/myweb_gunicorn_error.log -D wsgi:application

可以通過 gunicorn -h 查看更多配置參數

確保8000端口,防火墻已經開啟

通過 http://192.168.0.100:8000 訪問


4、nginx的安裝與配置

為了提高網站的效率,對於靜態文件通過nginx發送,

其他應用URL轉發到gunicorn啟動的進程

安裝:

./configure

make

make install

配置:

server {

listen 8080;

location / {

proxy_connect_timeout 600;

proxy_send_timeout 600;

proxy_read_timeout 600;

server_name_in_redirect off;

proxy_set_header Host $host:$server_port;

proxy_set_header X-Real-IP $remote_addr;

proxy_set_header REMOTE-HOST $remote_addr;

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

client_max_body_size 300m;

proxy_pass http://127.0.0.1:8000;

}

location /media/ {

alias /var/www/myweb/static/;

}

location /static/ {

alias /var/www/myweb/static/;

}

}

sudo /usr/local/nginx/sbin/nginx -s reload

確保8080端口,防火墻已經開啟

通過 http://192.168.0.100:8080 訪問


5、 supervisor 方式部署gunicorn

守護方式運行,也為了後續維護,

比如修改參數,啟動,停止等,supervisor都非常方便

/etc/supervisor/conf.d 下新增 gunicorn.conf

[program:myweb_gunicorn]

directory=/var/www/myweb

command=/usr/local/python/bin/gunicorn -w 4 -k gevent -m 0002 -b 0.0.0.0:8000 --error-logfile /var/log/gunicorn/myweb_gunicorn_error.log wsgi:application

autostart = true

startsecs = 5

user=myuser

redirect_stderr = true

stdout_logfile_maxbytes = 20MB

stdout_logfile_backups = 20

stdout_logfile =/var/log/gunicorn/myweb_gunicorn.log

特備註意,改了gunicorn.conf一定要重新加載配置,否則重啟不生效

/usr/local/python/bin/supervisorctl reload myweb_gunicorn

#重啟服務

/usr/local/python/bin/supervisorctl restart myweb_gunicorn

6、linux配置其他用戶sudo權限啟動

vim /etc/sudoers

myuser ALL=(ALL) NOPASSWD: /usr/local/nginx/sbin/nginx


本文出自 “centos升級openssh” 博客,請務必保留此出處http://withyx.blog.51cto.com/12666966/1946408

nginx+django+gunicorn+gevent+supervisor