nginx+uwsgi+flask配置记录

  • 来源:
  • 更新日期:2018-05-14

摘要:nginx部分: nginx使用nginx官方yum源 详情:http://nginx.org/en/download.html nginx的配置文件: server { listen 80; server_name www.iday.me; access_log /var/log/nginx/iday.me.access.log main;

nginx部分:

002UASMrzy7605pjKJv15&690.jpg

nginx的配置文件:

server { listen 80; server_name www.iday.me; access_log /var/log/nginx/iday.me.access.log main; error_log /var/log/nginx/iday.me.error.log ; location / { include uwsgi_params; uwsgi_pass unix:/dev/shm/iday.me; } location =/favicon.ico { alias /www/iday.me/static/img/favicon.ico; } location /static { alias /www/iday.me/static ; } }

 

 

uwsgi部分:

uwsgi通过pypi来来装:

 

pip install uwsgi

 

配置uwsgi有些麻烦,把uwsgi源码里的用于cenots的init.d script文件提取出来,稍作修改:

#!/bin/bash # uwsgi - Use uwsgi to run python and wsgi web apps. # # chkconfig: - 85 15 # description: Use uwsgi to run python and wsgi web apps. # processname: uwsgi # author: Roman Vasilyev # Source function library. . /etc/rc.d/init.d/functions PATH=/opt/uwsgi:/sbin:/bin:/usr/sbin:/usr/bin prog=/usr/local/bin/uwsgi OWNER=nginx NAME=uwsgi DESC=uwsgi DAEMON_OPTS="--emperor \'/etc/uwsgi/*.ini\' --listen 1024 -d /var/log/uwsgi/$NAME.log --uid $OWNER -M --pidfile /var/run/$NAME.pid" [ -f /etc/sysconfig/uwsgi ] && . /etc/sysconfig/uwsgi lockfile=/var/lock/subsys/uwsgi start () { echo -n "Starting $DESC: " daemon $prog $DAEMON_OPTS retval=$? echo [ $retval -eq 0 ] && touch $lockfile return $retval } stop () { echo -n "Stopping $DESC: " killproc $prog retval=$? echo [ $retval -eq 0 ] && rm -f $lockfile return $retval } reload () { echo "Reloading $NAME" killproc $prog -HUP RETVAL=$? echo } force-reload () { echo "Reloading $NAME" killproc $prog -TERM RETVAL=$? echo } restart () { stop start } rh_status () { status $prog } rh_status_q() { rh_status >/dev/null 2>&1 } case "$1" in start) rh_status_q && exit 0 $1 ;; stop) rh_status_q || exit 0 $1 ;; restart|force-reload) $1 ;; reload) rh_status_q || exit 7 $1 ;; status) rh_status ;; *) echo "Usage: $0 {start|stop|restart|reload|force-reload|status}" >&2 exit 2 ;; esac exit 0

然后用chkconfig 把uwsgi加入服务。这里我使用--emperor \'/etc/uwsgi/*.ini\' 参数即用 emperor模式来运行虚拟主机

配置uwsgi最麻烦的是虚拟主机的配置,uwsgi有virtualHosting模式,但是该模式只适合你有少量网站,而且它以一种相当复杂的方式运行,并且很不安全。uwsgi建议使用emperor模式替代virtualhosting模式。

接下来只要把虚拟主机的配置文件放入/etc/uwsgi/目录下就可以了,附上一个配置文件:iday.me.ini

[uwsgi] master max-requests =10000 processes = 2 pythonpath =/www/%n module =main callable=app enable-threads socket = /dev/shm/%n uid = nginx post-buffering=4096 logto=/var/log/uwsgi/%n.log pidfile=/var/run/uwsgi/%n.pid disable-logging listen=10240 ignore-sigpipe

 

 

module=main 说的是flask项目的入口文件,main.py文件:

# -*- coding: utf-8 -*- import sys from models import * from views import * from app import app reload(sys) sys.setdefaultencoding(\'utf-8\') if __name__ == \'__main__\': app.run(host=\'0.0.0.0\',port=81)

 

 

最后讲下面的uwsgi文件放入logrotate.d以便处理打包uwsgi产生的日志:

"/var/log/uwsgi/*.log" { copytruncate daily rotate 5 compress delaycompress missingok notifempty }