用python做web應用最麻煩的還是配置服務器了,此話不假,光中間件就有好幾種選擇,fastcgi、wsgi、uwsgi,難 免讓人眼花繚亂。 而聽說uwsgi的效率是fastcgi和wsgi的10倍,因此初學python的我就有點躍躍欲試了,打算在centos下搭建個 nginx+uwsgi+python玩玩。
下面是本人經(jīng)過google和親身實踐所得:
準備工作:
yum install python-devel libxml2-devel python-setuptools zlib-devel wget pcre-devel gcc make 編譯安裝nginx:
cd /tmp
wget http:///download/nginx-1.2.5.tar.gz
tar -zxvf nginx-1.2.5.tar.gz
./configure //此處可能會提示有些library未安裝,安裝完再運行此命令檢查即可
make;make install //確認無誤,編譯安裝 編譯安裝uwsgi:
wget http://projects./downloads/uwsgi-1.4.2.tar.gz
tar -zxvf uwsgi-1.4.2.tar.gz cd uwsgi-1.4.2
python setup.py build
make
mv uwsgi /usr/bin //將編譯好的文件移動到此處 配置nginx
vim /usr/local/nginx/conf/nginx.conf 在server下的location下增加以下2行
location / {
uwsgi_pass 127.0.0.1:9001;
include uwsgi_params;
} 編輯python測試文件
vim /var/www/index.py
def application(env, start_response): start_response('200 OK', [('Content-Type','text/html; charset=iso-8859-1')]) return 'Hello, world'
啟動nginx和uwsgi
/usr/local/nginx/sbin/nginx
uwsgi -s 127.0.0.1:9001 --wsgi-file /var/www/index.py Centos系統(tǒng)放在了虛擬機里,我用winxp母機,firefox17.0查看的頁面,結果是挺讓人吃驚的,單純的helloworld幾乎不耗時間,如圖

如果是使用django等python框架的,相關的配置信息會更容易找些。
|