CentOS+nginx+uwsgi+Python 多站点环境搭建

环境:

CentOS X64 6.4

nginx 1.5.6

Python 2.7.5

正文:

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

安装必要的开发包

yum groupinstall "Development tools"

yum install zlib-devel bzip2-devel pcre-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel

CentOS 自带Python2.6.6,但我们可以再安装Python2.7.5:

cd ~
wget http://python.org/ftp/python/2.7.5/Python-2.7.5.tar.bz2
tar xvf Python-2.7.5.tar.bz2
cd Python-2.7.5
./configure --prefix=/usr/local
make && make altinstall

安装完毕后,可是使用”python2.7”命令进入python2.7的环境。

二:安装Python包管理

easy_install包 https://pypi.python.org/pypi/distribute

方便安装Python的开发包

cd ~
wget https://pypi.python.org/packages/source/d/distribute/distribute-0.6.49.tar.gz
tar xf distribute-0.6.49.tar.gz
cd distribute-0.6.49
python2.7 setup.py install
easy_install --version

红色部分必须是“python2.7”,否则将安装到默认的2.6环境内。

pip包 https://pypi.python.org/pypi/pip

安装pip的好处是可以pip list、pip uninstall 管理Python包, easy_install没有这个功能,只有uninstall

easy_install pip
pip --version

三:安装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”输出,若没有输出,请检查你的安装过程。

四:安装django

pip install django

测试django是否正常,运行:

django-admin.py startproject demosite
cd demosite
python2.7 manage.py runserver 0.0.0.0:8002

在浏览器内输入:http://127.0.0.1:8002,检查django是否运行正常。

五:安装nginx

cd ~
wget http://nginx.org/download/nginx-1.5.6.tar.gz
tar xf nginx-1.5.6.tar.gz
cd nginx-1.5.6
./configure --prefix=/usr/local/nginx-1.5.6 --with-http_stub_status_module --with-http_gzip_static_module
make && make install

六:配置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配置

server {
        listen       80;
        server_name  localhost;

        location / {
            include  uwsgi_params;
            uwsgi_pass  127.0.0.1:9090;              //必须和uwsgi中的设置一致
            uwsgi_param UWSGI_SCRIPT demosite.wsgi;  //入口文件,即wsgi.py相对于项目根目录的位置,“.”相当于一层目录
            uwsgi_param UWSGI_CHDIR /demosite;       //项目根目录
            index  index.html index.htm;
            client_max_body_size 35m;
        }
    }

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


#!/bin/sh
#
# nginx - this script starts and stops the nginx daemon
#
# chkconfig:   - 85 15
# description:  Nginx is an HTTP(S) server, HTTP(S) reverse #               proxy and IMAP/POP3 proxy server
# processname: nginx
# config:      /usr/local/nginx/conf/nginx.conf
# pidfile:     /var/run/nginx.pid

# Source function library.
. /etc/rc.d/init.d/functions

# Source networking configuration.
. /etc/sysconfig/network

# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0

nginx="/opt/nginx-1.5.6/sbin/nginx"
prog=$(basename $nginx)

NGINX_CONF_FILE="/opt/nginx-1.5.6/conf/nginx.conf"

[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx

lockfile=/var/lock/subsys/nginx  

start() {
    [ -x $nginx ] || exit 5
    [ -f $NGINX_CONF_FILE ] || exit 6
    echo -n $"Starting $prog: "
    daemon $nginx -c $NGINX_CONF_FILE
    retval=$?
    echo
    [ $retval -eq 0 ] && touch $lockfile
    return $retval
}

stop() {
    echo -n $"Stopping $prog: "
    killproc $prog -QUIT
    retval=$?
    echo
    [ $retval -eq 0 ] && rm -f $lockfile
    return $retval
}

restart() {
    configtest || return $?
    stop
    sleep 1
    start
}

reload() {
    configtest || return $?
    echo -n $"Reloading $prog: "
    killproc $nginx -HUP
    RETVAL=$?
    echo
}

force_reload() {
    restart
}

configtest() {
  $nginx -t -c $NGINX_CONF_FILE
}

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|configtest)
        $1
        ;;
    reload)
        rh_status_q || exit 7
        $1
        ;;
    force-reload)
        force_reload
        ;;
    status)
        rh_status
        ;;
    condrestart|try-restart)
        rh_status_q || exit 0
            ;;
    *)
        echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
        exit 2
esac

nginx

然后在终端执行:

-- 添加服务
chkconfig --add nginx
-- 设置开机启动
chkconfig nginx on

八:测试

OK,一切配置完毕,在终端运行

service uwsgi9090 start
service nginx start

在浏览器输入:http://127.0.0.1,恭喜你可以看到django的“It work”了~

九:多站配置

我采用运行多个uwsgi服务的方法来实现多个站点。

重复第六步,创建uwsgi9091.ini,并相应修改文件中的

socket = 127.0.0.1:9091
pidfile = /var/run/uwsgi9091.pid
daemonize = /website/uwsgi9091.log

并创建服务uwsgi9091,设置开机启动。

然后修改nginx的配置文件为:


server {
        listen       80;
        server_name  localhost;

        location / {
            include  uwsgi_params;
            uwsgi_pass  127.0.0.1:9090;
            uwsgi_param UWSGI_SCRIPT demosite.wsgi;
            uwsgi_param UWSGI_CHDIR /website/demosite;
            index  index.html index.htm;
            client_max_body_size 35m;
        }
    }

    server {
        listen       1300;

        location / {
            include  uwsgi_params;
            uwsgi_pass  127.0.0.1:9091;
            uwsgi_param UWSGI_SCRIPT DjangoStudy.wsgi;
            uwsgi_param UWSGI_CHDIR /website/DjangoStudy;
            index  index.html index.htm;
        }
    }

nginx

然后我们就可以通过http://127.0.0.1:1300来访问新的网站了。

十:其他配置

防火墙设置

CentOS默认关闭外部对80、3306等端口的访问,所以要在其他计算机访问这台服务器,就必须修改防火墙配置,打开/etc/sysconfig/iptables

在“-A INPUT –m state --state NEW –m tcp –p –dport 22 –j ACCEPT”,下添加:

-A INPUT -m state --state NEW -m tcp -p -dport 80 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p -dport 3306 -j ACCEPT

然后保存,并关闭该文件,在终端内运行下面的命令,刷新防火墙配置:

service iptables restart

安装Mysqldb

yum -y install mysql-devel

easy_install-2.7 MySQL-python

注意红色部分,easy_install-2.7,否则它将默认安装到Python2.6环境内。

------------------------------------------------------------------------------------------------------------------

2014年12月02日添加:

CentOS 7中默认使用Firewalld做防火墙,所以修改iptables后,在重启系统后,根本不管用。

Firewalld中添加端口方法如下:

firewall-cmd --zone=public --add-port=3306/tcp --permanent

firewall-cmd --reload

时间: 2024-07-29 21:44:36

CentOS+nginx+uwsgi+Python 多站点环境搭建的相关文章

Centos+nginx+uwsgi+Python多站点环境搭建

前言 新公司的第一个项目,服务器端打算用python作为restful api.所以需要在Centos上搭建nginx+fastcgi+python的开发环境,但后面网上很多言论都说uwsgi比fastcgi在很多方面存在优势,推荐用uwsgi而不是fastcgi,详见:http://sunxiunan.com/?p=1778.因此,改为搭建nginx+uwsgi+python这样的技术组合. 正题 步入正题,开始搭建环境.http://www.cnblogs.com/xiongpq/p/338

Ubuntu下nginx+uwsgi+flask的运行环境搭建

选择web framwork是个很艰难的事情, 主要分为轻量级和重量级框架. 由于没有搭建网站这种需要, 所以回避SSH, Django这种框架, 而选择一个轻量级框架. 自己也比较青睐python这门语言, 就选择了flask框架, nginx代理服务器享誉盛名, 所以拿来使用咯. 一. 开发环境搭建 采用离线安装方式, ubuntu开发环境(centos等环境类似) nginx 安装 $ wget http://nginx.org/download/nginx-1.6.0.tar.gz #仅

CentOS7.2安装配置nginx+uwsgi+python+flask运行环境

操作系统:CentOS 7.2 Nginx安装请参考centos7.2安装nginx这个文章 1.  安装python3.5 执行命令 wget --no-check-certificate https://www.python.org/ftp/python/3.5.0/Python-3.5.0.tgz 等待下载完成 下载完成后,执行tar -zxvf Python-3.5.0.tgz解压安装包,因文件太多,只取最后几个截图 Cd到python的解压目录下执行./configure命令,同样只取

Centos下搭建 nginx+uwsgi+python

python做web应用最麻烦的还是配置服务器了,此话不假,光中间件就有好几种选择,fastcgi.wsgi.uwsgi,难 免让人眼花缭乱. 而听说uwsgi的效率是fastcgi和wsgi的10倍,因此初学python的我就有点跃跃欲试了,打算在centos下搭建个 nginx+uwsgi+python玩玩. 下面是本人经过google和亲身实践所得: 准备工作: yum install python-devel libxml2-devel python-setuptools zlib-de

《Python入门》Linux 下 Python Web开发环境搭建笔记

之前写过 Windows 7下Python Web开发环境搭建笔记,今天写一下在Linux系统下搭建Python Web的开发测试环境. 我使用的系统是:ubuntu 14.04 server,根据个人经验,CentOS 6.5 下也适用. 关于Python的版本 进入Python的网站,鼠标移到导航条上的下载,我们会发现提供两下主版本的下载链接! 这两个之间存在什么差别呢? 个人理解,2.7.x的版本为更加稳定的版本,而3.x的版本则是比较前卫的版本,包含了很多新功能新特性之类的: 但如果想要

Nginx + uWSGI + Python + Django部署实例

Nginx: Nginx 是一个高性能的 Web 和反向代理服务器, 它具有有很多非常优越的特性: 作为 Web 服务器:相比 Apache,Nginx 使用更少的资源,支持更多的并发连接,体现更高的效率,这点使 Nginx 尤其受到虚拟主机提供商的欢迎.能够支持高达 50,000 个并发连接数的响应,感谢 Nginx 为我们选择了 epoll and kqueue 作为开发模型. 作为负载均衡服务器:Nginx 既可以在内部直接支持 Rails 和 PHP,也可以支持作为 HTTP代理服务器

Python + Selenium 环境搭建

Python + Selenium 环境搭建 注:本文是根据网上资料收集验证整理而得,仅供学习 准备如下: 1.下载 python http://python.org/getit/ 2.下载 setuptools http://pypi.python.org/pypi/setuptools 3.下载 pip https://pypi.python.org/pypi/pip setuptools 是 python 的基础包工具,可以帮助我们轻松的下载,构建,安装,升级,卸载 python的软件包.

python(pyqt)开发环境搭建

eric+pyqt 安装(python开发工具) 更多 0 Python python Eric是一个开源的.跨平台的python&ruby集成开发环境,基于python和pyqt运行.eric有以下特点 1.跨Windows/Linux/Mac等开台 2.调试器给力.支持设置断点,单步调试,查看变量值. 3.支持工程. 4.支持自动补全. 5.支持智能感知,即输入变量名和一个点,会自动提示可能的函数. 6.自动语法检查.每次保存时自动检查. 7.支持自动缩进,会自动判断if, while等语句

nginx整合php+lua+oracle环境搭建

nginx整合php+lua+oracle环境搭建 标签: nginxluaoraclephplinux 2014-09-25 10:39 1473人阅读 评论(0) 收藏 举报 分类: 技术(70) 版权声明:本文为博主原创文章,未经博主允许不得转载. *执行 yum update 为了得到一个最新的系统 *执行 rpm -ivh oracle-instantclient12.1-basic-12.1.0.2.0-1.x86_64.rpm oracle-instantclient12.1-de