centos+nginx+uwsgi+virtualenv+flask 多網站環境搭建

來源:互聯網
上載者:User

標籤:

原文連結:http://localhost:8090/search/142294.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

相關網站 

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

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()"命令。

4、安裝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

5、安裝pip包管理工具

相關網站:

pip 

#easy_install pip
#pip --version

6、安裝uWSGI

相關網站:

uwsgi 

uwsgi參數詳解 

#pip install uwsgi#uwsgi --version

7、安裝virtualenv

相關網站:

virtualenv 

#pip install virtualenv#virtualenv --version

8、安裝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、環境配置

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 INFOPATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/binDESC="uwsgi daemon"NAME=uwsgi_www.a.comDAEMON=/usr/local/bin/uwsgiCONFIGFILE=/etc/uwsgi/$NAME.iniPIDFILE=/var/run/$NAME.pidSCRIPTNAME=/etc/init.d/$NAMEset -e[ -x "$DAEMON" ] || exit 0do_start() {$DAEMON $CONFIGFILE || echo -n "uwsgi already running"}do_stop() {$DAEMON --stop $PIDFILE || echo -n "uwsgi not running"rm -f $PIDFILEecho "$DAEMON STOPED."}do_reload() {$DAEMON --reload $PIDFILE || echo -n "uwsgi can‘t reload"}do_status() {ps aux|grep $DAEMON}case "$1" instatus)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}" >&2exit 3;;esacexit 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 Flaskapp = 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:     Date: 2015年1月14日

原文連結:http://localhost:8090/search/142294.html

centos+nginx+uwsgi+virtualenv+flask 多網站環境搭建

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.