CentOS 搭建uWSGI+Nginx部署Python环境

CentOS 搭建uWSGI+Nginx部署Python环境

一:安装需要的类库及Python2.7

yum install  -y vim  wget gcc gcc-c++ zlib-devel libxml2-devel libxslt-devel libffi python-cffi libffi-devel  openssl openssl-devel bzip2-devel ncurses-devel sqlite-devel sqlite 
wget http://python.org/ftp/python/2.7.3/Python-2.7.3.tar.bz2 

tar -jxvf Python-2.7.3.tar.bz2  

echo "===== install python2.73 ===="
cd Python-2.7.3  
./configure  
make 
make install 
make clean

二:安装pip

echo "===== install PiPi python ===="
wget http://pypi.douban.com/packages/source/s/setuptools/setuptools-2.2.tar.gz#md5=04a7664538957b832710653fd7d5b4e6
tar  xvfz  setuptools-2.2.tar.gz
cd  setuptools-2.2
 
python2.7 setup.py  install

cd  ../

wget http://pypi.douban.com/packages/source/p/pip/pip-1.5.tar.gz#md5=6969b8a8adc4c7f7c5eb1707118f0686

tar xvfz pip-1.5.tar.gz 
cd pip-1.5
python setup.py install 
cd ../

三:安装nginx

vi /etc/yum.repos.d/nginx.repo

添加如下源:

[nginx] 
name=nginx repo 
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/ 
gpgcheck=0 
enabled=1

执行yum命令安装nginx
yum install -y nginx

四:安装uwsgi

uwsgi:https://pypi.python.org/pypi/uWSGI

uwsgi参数详解:http://uwsgi-docs.readthedocs.org/en/latest/Options.html

pip install uwsgi
uwsgi --version

测试uwsgi是否正常:

新建test.py文件,内容如下:

def application(env, start_response):
        start_response('200 OK', [('Content-Type','text/html')])
        return "Hello World"

然后在终端运行:

uwsgi –http :8001 –wsgi-file test.py
在浏览器内输入:http://127.0.0.1:8001,看是否有“Hello World”输出,若没有输出,请检查你的安装过程。

五:配置uwsgi
uwsgi支持ini、xml等多种配置方式,但个人感觉ini更方便:

在/ect/目录下新建uwsgi9090.ini,添加如下配置:

[uwsgi]
socket = 127.0.0.1:9090
master = true         #主进程
vhost = true          #多站模式
no-stie = true        #多站模式时不设置入口模块和文件
workers = 2           #子进程数
reload-mercy = 10     
vacuum = true         #退出、重启时清理文件
max-requests = 1000   
limit-as = 512
buffer-sizi = 30000
pidfile = /var/run/uwsgi9090.pid    #pid文件,用于下面的脚本启动、停止该进程
daemonize = /website/uwsgi9090.log

设置uwsgi开机启动,在/ect/init.d/目录下新建uwsgi9090文件,内容如下:

#! /bin/sh
# chkconfig: 2345 55 25
# Description: Startup script for uwsgi webserver on Debian. Place in /etc/init.d and
# run 'update-rc.d -f uwsgi defaults', or use the appropriate command on your
# distro. For CentOS/Redhat run: 'chkconfig --add uwsgi'
 
### BEGIN INIT INFO
# Provides:          uwsgi
# Required-Start:    $all
# Required-Stop:     $all
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: starts the uwsgi web server
# Description:       starts uwsgi using start-stop-daemon
### END INIT INFO
 
# Author:   licess
# website:  http://lnmp.org
 
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DESC="uwsgi daemon"
NAME=uwsgi9090
DAEMON=/usr/local/bin/uwsgi
CONFIGFILE=/etc/$NAME.ini
PIDFILE=/var/run/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAME
 
set -e
[ -x "$DAEMON" ] || exit 0
 
do_start() {
    $DAEMON $CONFIGFILE || echo -n "uwsgi already running"
}
 
do_stop() {
    $DAEMON --stop $PIDFILE || echo -n "uwsgi not running"
    rm -f $PIDFILE
    echo "$DAEMON STOPED."
}
 
do_reload() {
    $DAEMON --reload $PIDFILE || echo -n "uwsgi can't reload"
}
 
do_status() {
    ps aux|grep $DAEMON
}
 
case "$1" in
 status)
    echo -en "Status $NAME: \n"
    do_status
 ;;
 start)
    echo -en "Starting $NAME: \n"
    do_start
 ;;
 stop)
    echo -en "Stopping $NAME: \n"
    do_stop
 ;;
 reload|graceful)
    echo -en "Reloading $NAME: \n"
    do_reload
 ;;
 *)
    echo "Usage: $SCRIPTNAME {start|stop|reload}" >&2
    exit 3
 ;;
esac
 
exit 0

uwsgi9090

然后在终端执行:

— 添加服务
chkconfig –add uwsgi9090
— 设置开机启动
chkconfig uwsgi9090 on

六:设置nginx
找到nginx的安装目录,打开conf/nginx.conf文件,修改server配置 yum 安装的一般在/etc/nginx/conf.d/default.conf

修改如下:

location / {
        #root   /usr/share/nginx/html;
        #index  index.html index.htm;
        include  uwsgi_params;
            uwsgi_pass  127.0.0.1:9090; #             //必须和uwsgi中的设置一致
            uwsgi_param UWSGI_SCRIPT wsgi; # //入口文件,即wsgi.py相对于项目根目录的位置
            uwsgi_param UWSGI_CHDIR /opt/website/desktop/webapp;    #   //项目根目录
            index  index.html index.htm;
            client_max_body_size 35m;
    }

注意:uwsgi_pass 的相关配置必须和启动uwsgi时的一致!UWSGI_CHDIR是指程序所在的目录

UWSGI_SCRIPT是指启动哪个程序(注意,这里必须去掉py后缀)
UWSGI_CHDIR 和UWSGI_SCRIPT在部署多个网站共用一个uwsgi进程时用到

安装Mysqldb

yum -y install mysql-devel

easy_install-2.7 MySQL-python

八:启动
service nginx start
service uwsgi9090 start