环境:
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
相关网站 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/share/nginx/www //新建项目库文件夹
#mkdir /usr/share/nginx/www.a.com //新建项目根目录文件夹
#cd /usr/share/nginx/www/www.a.com
#vietualenv env //创建虚拟环境
#. env/bin/activate //进入虚拟环境
#pip install flask //安装flask
#deactivate //退出虚拟环境
9、环境配置
9.1 配置声明
首先我要声明几个固定目录的绝对位置。(本节严格按照以下位置进行配置&如需修改路径请同时修改相关配置文件)
nginx配置文件: /etc/nginx/vhosts/www.a.com.conf
#mkdir /etc/nginx/vhosts //新建nginx配置文件夹
项目根目录: /usr/share/nginx/www/www.a.com
日志文件目录: /usr/share/nginx/log
#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/share/nginx/www/www.a.com/env
chdir = /usr/share/nginx/www/www.a.com
module = myapp
callable = app
touch-reload = /usr/share/nginx/www/www.a.com
pidfile = /var/run/uwsgi_www.a.com.pid
daemonize = /usr/share/nginx/log/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 /etc/nginx/nginx.conf
查找到:
include /etc/nginx/conf.d/*.conf;
修改为:
#include /etc/nginx/conf.d/*.conf; //在行首添加#号注释掉此句
include /etc/nginx/vhosts/*.conf; //引用应用配置文件
9.2.4 新建项目配置文件
#vi /etc/nginx/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/share/nginx/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、码字太多难免出错,如发现错误或有疑问请留言,我第一时间回复大家。
2、本博客欢迎转发,但请保留以下信息:
By: oYY Url: http://www.cnblogs.com/echoyy Date: 2015年1月14日