yum groupinstall "Development tools"
yum install zlib-devel bzip2-devel pcre-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel
cd ~
wget https://python.org/ftp/python/2.7.5/Python-2.7.5.tar.bz2
tar xvf Python-2.7.5.tar.bz2
cd Python-2.7.5
./configure --prefix=/usr/local
make && make altinstall # altinstall 避免覆蓋系統 python 指令
使用 python2.7 指令進入 2.7 環境。
# distribute (setuptools 前身)
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
python2.7 setup.py install
# pip
easy_install pip
pip --version
關鍵:所有安裝步驟必須明確使用 python2.7,否則裝入系統 2.6 環境。
pip install uwsgi
uwsgi --version
# 測試
cat > test.py <<'EOF'
def application(env, start_response):
start_response('200 OK', [('Content-Type','text/html')])
return "Hello World"
EOF
uwsgi --http :8001 --wsgi-file test.py
# 瀏覽器訪問 https://127.0.0.1:8001 應顯示 Hello World
pip install django
django-admin.py startproject demosite
cd demosite
python2.7 manage.py runserver 0.0.0.0:8002
wget https://nginx.org/download/nginx-1.5.6.tar.gz
tar xf nginx-1.5.6.tar.gz
cd nginx-1.5.6
./configure --prefix=/usr/local/nginx-1.5.6 \
--with-http_stub_status_module \
--with-http_gzip_static_module
make && make install
/etc/uwsgi9090.ini[uwsgi]
socket = 127.0.0.1:9090
master = true
vhost = true # 多站模式
no-site = true # 不指定單一入口
workers = 2
reload-mercy = 10
vacuum = true
max-requests = 1000
limit-as = 512
buffer-size = 30000
pidfile = /var/run/uwsgi9090.pid
daemonize = /website/uwsgi9090.log
/etc/init.d/uwsgi9090#!/bin/sh
# chkconfig: 2345 55 25
### 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
### END INIT INFO
DAEMON=/usr/local/bin/uwsgi
CONFIGFILE=/etc/uwsgi9090.ini
PIDFILE=/var/run/uwsgi9090.pid
NAME=uwsgi9090
case "$1" in
start) $DAEMON $CONFIGFILE ;;
stop) $DAEMON --stop $PIDFILE ;;
reload) $DAEMON --reload $PIDFILE ;;
status) ps aux | grep $DAEMON ;;
*) echo "Usage: $0 {start|stop|reload|status}"; exit 3 ;;
esac
chmod +x /etc/init.d/uwsgi9090
chkconfig --add uwsgi9090
chkconfig uwsgi9090 on
server {
listen 80;
server_name localhost;
location / {
include uwsgi_params;
uwsgi_pass 127.0.0.1:9090;
uwsgi_param UWSGI_SCRIPT demosite.wsgi; # 相對專案根目錄
uwsgi_param UWSGI_CHDIR /demosite; # 專案根目錄
client_max_body_size 35m;
}
}
同理建立 /etc/init.d/nginx、chkconfig --add nginx、chkconfig nginx on。
完成後,多個 Python 專案僅需在 Nginx 新增 server 區塊指向同一 uWSGI socket,即可實現輕量級多站點託管。