centos 部署nginx + python3 +uwsgi

uwsgi 是python的web容器,nginx作为转发代理
一、安装nginx

yum nginx

二:安装python
下载python3的源码进行安装

wget https://www.python.org/ftp/python/3.5.2/Python-3.5.2.tgz #下载python源码
chmod 777 Python-3.5.2.tgz #授权,不然不能解压
tar -zxvf Python-3.5.2.tgz #解压包在目前目录
cd Python-3.5.2 #进到解压后的文件夹
./configure --prefix=/opt/python3 #指定安装目录
make all  #编译
make install #安装
./opt/python3/python -V  #查看是否看装成功
make clean
make distclean

这时安装的python还不是默认的python工具。centos默认会带一个python2的环境。需要将python3设调为默认

mv /usr/bin/python /usr/bin/python2.6 
ln -s /opt/python3/bin/python3.5 /usr/bin/python
python -V

yum是python脚本,使用的是2.6版本。为了让yum能继续工作需要修改yum第一行,指定python 2.6版本

vi /usr/bin/yum
#!/usr/bin/python改成#!/usr/bin/python2.6 保存退出

三、安装uwsgi

cd /opt/python3/bin/
./pip3.5 install uwsgi #使用pip安装uwsgi
./uwsgi --version #检是否安装成功

四、安装django

./pip3.5 install django

五、配置uwsgi
在/etc下建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 = /data/logs/uwsgi/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:/opt/python3/bin
DESC="uwsgi deamo"
NAME=uwsgi9090
DAEMON=/opt/python3/bin/uwsgi
CONFIGFILE=/opt/python3/uwsgi-server/$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配置
在conf.d下建同域名相同的文件如:xxx.xxx.com.conf

server {
        listen       80;
        server_name  data.teanet.im;

        #配置资源文件
        location /resource {
            root   /opt/webapps/data/;
            autoindex on;
            autoindex_exact_size on;
            autoindex_localtime on;
            allow   all;
        }
        #重定向到uwsgi的配置
        location / {            
            include  uwsgi_params;
            uwsgi_pass  127.0.0.1:9090;
            uwsgi_param UWSGI_SCRIPT data.wsgi;
            uwsgi_param UWSGI_CHDIR /opt/webapps/;
            index  index.html index.htm;
            client_max_body_size 35m;
        }
    }

配置完后可以启动服务验证了

service uwsgi9090 start
service nginx start

如想配置多站点就得重复第五步、第六步

你可能感兴趣的:(python,python,nginx)