nginx部分: nginx使用nginx官方yum源 详情:http://nginx.org/en/download.html nginx的配置文件: server { listen
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; 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/functionsPATH=/opt/uwsgi:/sbin:/bin:/usr/sbin:/usr/binprog=/usr/local/bin/uwsgiOWNER=nginxNAME=uwsgiDESC=uwsgiDAEMON_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/uwsgilockfile=/var/lock/subsys/uwsgistart () { 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]mastermax-requests =10000processes = 2 pythonpath =/www/%nmodule =maincallable=appenable-threadssocket = /dev/shm/%nuid = nginxpost-buffering=4096logto=/var/log/uwsgi/%n.logpidfile=/var/run/uwsgi/%n.piddisable-logginglisten=10240ignore-sigpipe
module=main 说的是flask项目的入口文件,main.py文件:
# -*- coding: utf-8 -*-import sysfrom models import *from views import *from app import appreload(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}