萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 伺服器教學

CentOS 6.4 部署 Nginx + uWSGI + Python 2.7 多站點環境完整實戰

環境規劃

  • CentOS 6.4 x64
  • Nginx 1.5.6
  • Python 2.7.5(系統預設 2.6.6 共存)
  • uWSGI 多站模式(vhost)

1. 開發套件與 Python 2.7.5 編譯安裝

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 環境。

2. Python 套件管理工具鏈

# 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 環境。

3. uWSGI 安裝與測試

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

4. Django 安裝驗證(可選)

pip install django
django-admin.py startproject demosite
cd demosite
python2.7 manage.py runserver 0.0.0.0:8002

5. Nginx 編譯安裝

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

6. uWSGI 多站配置 /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

SystemV 啟動腳本 /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

7. Nginx 站點配置

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;
    }
}

8. Nginx 啟動腳本註冊

同理建立 /etc/init.d/nginx、chkconfig --add nginx、chkconfig nginx on。

完成後,多個 Python 專案僅需在 Nginx 新增 server 區塊指向同一 uWSGI socket,即可實現輕量級多站點託管。

copyright © 萬盛學電腦網 all rights reserved