nginx+uwsgi+flask配置記錄

nginx部分:html

nginx使用nginx官方yum源 詳情:http://nginx.org/en/download.htmlpython

nginx的配置文件: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部分: web

uwsgi經過pypi來來裝:shell

pip install uwsgi

配置uwsgi有些麻煩,把uwsgi源碼裏的用於cenots的init.d script文件提取出來,稍做修改:flask

#!/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模式。bash

接下來只要把虛擬主機的配置文件放入/etc/uwsgi/目錄下就能夠了,附上一個配置文件:iday.me.iniapp

[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文件:socket

# -*- 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
}
相關文章
相關標籤/搜索