centos+nginx+uwsgi+virtualenv+flask 多站点环境搭建

转载自 http://www.cnblogs.com/echoyy/p/4221408.html


环境:

centos x64 6.6

nginx 1.6.2

python 2.7.9

uwsgi 2.0.9

virtualenv 12.0.5

flask 0.10.1

正文:

1、安装nginx(nginx配置按照 nginx服务器安装及配置文件详解

相关网站 http://nginx.com

1.1 配置yum第三方源

centos默认的源里没有nginx安装包,所以我们来修改第三方源。

#cd ~
#wget http://www.atomicorp.com/installers/atomic
#sh ./atomic
#yum check-update

1.2 安装nginx

#yum install nginx
#service nginx start
#chkconfig nginx on

2、安装类库

#cd ~
#yum groupinstall "Development tools" #yum install zlib-devel bzip2-devel pcre-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel

3、安装Python

相关网站:

python http://www.python.org

3.1 安装最新版

centos 6.6默认安装的是python2.6.6版本,我们现在安装最新版的python2.7.9

#cd ~
#wget https://www.python.org/ftp/python/2.7.9/Python-2.7.9.tgz
#tar xvf Python-2.7.9.tgz
#cd Python-2.7.9
#./configure --prefix=/usr/local
#make && make altinstall

3.2 备份旧版本,链接新版本

由于centos系统的yum程序使用的是之前系统自带的python2.6.6版本,所以为了解决版本冲突问题,我们先将新旧版本的python做好区分和重新链接

#mv /usr/bin/python /usr/bin/python2.6.6
#ln -s /usr/local/bin/python2.7 /usr/bin/python

3.3 修改yum配置文件

重新指定其所使用的python程序路径:

#vi /usr/bin/yum

查找到:

#!/usr/bin/python

修改为:

#!/usr/bin/python2.6.6

修改完毕后,可是使用"python"命令进入python2.7.9的环境。

退出环境使用"exit()"命令。

4、安装Python包管理工具

相关网站:

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

#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
#python setup.py install
#distribute --version

5、安装pip包管理工具

相关网站:

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

#easy_install pip
#pip --version

6、安装uWSGI

相关网站:

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

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

#pip install uwsgi
#uwsgi --version

7、安装virtualenv

相关网站:

virtualenv https://pypi.python.org/pypi/virtualenv

#pip install virtualenv
#virtualenv --version

8、安装flask

相关网站:

flask http://flask.pocoo.org/

复制代码
#mkdir /usr/local/nginx-1.6/www                         //新建项目库文件夹
#mkdir /usr/local/nginx-1.6/www/www.a.com               //新建项目根目录文件夹
#cd /usr/local/nginx-1.6/www/www.a.com
#virtualenv env //创建虚拟环境,env是定义的目录
#. env/bin/activate //进入虚拟环境,.后有个空格
#pip install flask //安装flask
#deactivate //退出虚拟环境
复制代码

9、环境配置

9.1 配置声明

首先我要声明几个固定目录的绝对位置。(本节严格按照以下位置进行配置&如需修改路径请同时修改相关配置文件)

nginx配置文件: /usr/local/nginx-1.6/conf/vhosts/www.a.com.conf

#mkdir /usr/local/nginx-1.6/conf/vhosts/www.a.com.conf   //新建nginx配置文件夹

项目根目录: /usr/local/nginx-1.6/www/www.a.com

日志文件目录: /usr/local/nginx-1.6/logs

//#mkdir /usr/share/nginx/log                            //新建日志文件夹

uWSGI配置文件:/etc/uwsgi/www.a.com.ini

#mkdir /etc/uwsgi                                       //新建uwsgi配置文件夹

9.2 配置uWSGI

9.2.1 新建uWSGI配置文件

复制代码
#vi /etc/uwsgi/uwsgi_www.a.com.ini                      //新建uwsgi配置文件

//将下面标注蓝色的内容录入wusgi_www.a.com.ini文件

[uwsgi]
master = true
vhost = true
workers = 2
reload-mercy = 10
vacuum = true
max-requests = 1000
limit-as = 256
chmod-socket = 666
socket = /tmp/uwsgi_www.a.com.sock
venv = /usr/local/nginx-1.6/www/www.a.com/env
chdir = /usr/local/nginx-1.6/www/www.a.com
module = myapp
callable = app
touch-reload = /usr/local/nginx-1.6/www/www.a.com
pidfile = /var/run/uwsgi_www.a.com.pid
daemonize = /usr/local/nginx-1.6/logs/uwsgi_www.a.com.log
复制代码

/var/run/uwsgi_www.a.com.pid可能存在权限问题,使uwsgi服务开启失败。

uwsgi_www.a.com.log可能需要自己创建。

9.2.2 为uwsgi添加开机脚本

复制代码
#vi /etc/init.d/uwsgi_www.a.com

//将下面标注蓝色的内容录入uwsgi_www.a.com文件
#! /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 PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin DESC="uwsgi daemon" NAME=uwsgi_www.a.com DAEMON=/usr/local/bin/uwsgi CONFIGFILE=/etc/uwsgi/$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

#chkconfig --add uwsgi_www.a.com //添加服务
#chkconfig uwsgi_www.a.com on //设置开机启动
复制代码

9.2.3 修改nginx配置文件

首先打开nginx配置文件:

#vi /url/local/nginx-1.6/conf/nginx.conf

查找到:

include /etc/nginx/conf.d/*.conf;

修改为:

#include /etc/nginx/conf.d/*.conf;                         //在行首添加#号注释掉此句
include /url/local/nginx-1.6/conf/vhosts/*.conf; //引用应用配置文件

9.2.4 新建项目配置文件

复制代码
#vi /url/local/nginx-1.6/conf/vhosts/www.a.com.conf

//将下面标注蓝色的内容录入uwsgi_www.a.com文件

server {       listen 80;       server_name www.a.com;
      index index.htm index.html;   location / {           include uwsgi_params;           uwsgi_pass unix:///tmp/uwsgi_www.a.com.sock;
  } }
复制代码

10、测试

10.1 开启服务

#service nginx start
#service uwsgi_www.a.com start

10.2 新建入口组件文件

复制代码
#vi /usr/local/nginx-1.6/www/www.a.com/myapp.py

//将下面标注蓝色的内容录入uwsgi_www.a.com文件

from flask import Flask
app = Flask(__name__)

@app.route('/')
def hello_world():
return 'Hello World!'

if __name__ == '__main__':
app.run()
复制代码

运行浏览器访问www.a.com 此时出现"Hello World!"表示环境已经搭建成功。

PS:

1、码字太多难免出错,如发现错误或有疑问请留言,我第一时间回复大家。



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值