原文連接:http://faceye.net/search/142294.html
html
環境:python
centos x64 6.6nginx
nginx 1.6.2web
python 2.7.9sql
uwsgi 2.0.9flask
virtualenv 12.0.5centos
flask 0.10.1瀏覽器
正文:app
一、安裝nginxpython2.7
相關網站
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
二、安裝類庫
#cd ~
#yum groupinstall "Development tools"
#yum install zlib-devel bzip2-devel pcre-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel
三、安裝Python
相關網站:
python
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.tgz
#./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()"命令。
四、安裝Python包管理工具
相關網站:
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
五、安裝pip包管理工具
相關網站:
pip
#easy_install pip
#pip --version
六、安裝uWSGI
相關網站:
uwsgi
uwsgi參數詳解
#pip install uwsgi
#uwsgi --version
七、安裝virtualenv
相關網站:
virtualenv
#pip install virtualenv
#virtualenv --version
八、安裝flask
相關網站:
flask
#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.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.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:
一、碼字太多不免出錯,如發現錯誤或有疑問請留言,我第一時間回覆你們。
二、本博客歡迎轉發,但請保留如下信息:
By: oYY Url: Date: 2015年1月14日