1. 程式人生 > >Django+Nginx+uwsgi 安裝配置

Django+Nginx+uwsgi 安裝配置

python manage.py runserver 來執行伺服器,但這隻適用測試環境中使用。

正式釋出的服務,我們需要一個可以穩定而持續的伺服器,比如apache, Nginx, lighttpd等,本文將以 Nginx 為例。

安裝基礎開發包

Centos 下安裝步驟如下:

yum groupinstall "Development tools"
yum install zlib-devel bzip2-devel pcre-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel"Development tools"
yum install zlib-devel bzip2-devel pcre-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel

CentOS 自帶 Python 2.4.3,但我們可以再安裝Python2.7.5:

cd ~
wget http://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~
wget http://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

安裝Python包管理

安裝步驟:

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
python2.7 setup.py install
easy_install --version~
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
easy_install --version

安裝 pip 的好處是可以用 pip list、pip uninstall 管理 Python 包, easy_install 沒有這個功能,只有 uninstall。

安裝 uwsgi

pip install uwsgi
uwsgi --version    # 檢視 uwsgi 版本--version    # 檢視 uwsgi 版本

測試 uwsgi 是否正常:

新建 test.py 檔案,內容如下:

def application(env, start_response):
    start_response('200 OK', [('Content-Type','text/html')])
    return "Hello World" application(env, start_response):
    start_response('200 OK', [('Content-Type','text/html')])
    return "Hello World"

然後在終端執行:

uwsgi --http :8001 --wsgi-file test.py--http :8001 --wsgi-file test.py

在瀏覽器內輸入:http://127.0.0.1:8001,檢視是否有"Hello World"輸出,若沒有輸出,請檢查你的安裝過程。

安裝 Django

pip install django

測試 django 是否正常,執行:

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

在瀏覽器內輸入:http://127.0.0.1:8002,檢查django是否執行正常。

安裝 Nginx

安裝命令如下:

cd ~
wget http://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~
wget http://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

uwsgi 配置

uwsgi支援ini、xml等多種配置方式,本文以 ini 為例,在/ect/目錄下新建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    //pid檔案,用於下面的指令碼啟動、停止該程序
daemonize = /website/uwsgi9090.loguwsgi]
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    //pid檔案,用於下面的指令碼啟動、停止該程序
daemonize = /website/uwsgi9090.log

Nginx 配置

找到nginx的安裝目錄(如:/usr/local/nginx/),開啟conf/nginx.conf檔案,修改server配置:

server {
        listen       80;
        server_name  localhost;
        
        location / {            
            include  uwsgi_params;
            uwsgi_pass  127.0.0.1:9090;              //必須和uwsgi中的設定一致
            uwsgi_param UWSGI_SCRIPT demosite.wsgi;  //入口檔案,即wsgi.py相對於專案根目錄的位置,“.”相當於一層目錄
            uwsgi_param UWSGI_CHDIR /demosite;       //專案根目錄
            index  index.html index.htm;
            client_max_body_size 35m;
        }
    }{
        listen       80;
        server_name  localhost;
        
        location / {            
            include  uwsgi_params;
            uwsgi_pass  127.0.0.1:9090;              //必須和uwsgi中的設定一致
            uwsgi_param UWSGI_SCRIPT demosite.wsgi;  //入口檔案,即wsgi.py相對於專案根目錄的位置,“.”相當於一層目錄
            uwsgi_param UWSGI_CHDIR /demosite;       //專案根目錄
            index  index.html index.htm;
            client_max_body_size 35m;
        }
    }

設定完成後,在終端執行:

uwsgi --ini /etc/uwsgi9090.ini &
/usr/local/nginx/sbin/nginx--ini /etc/uwsgi9090.ini &
/usr/local/nginx/sbin/nginx

在瀏覽器輸入:http://127.0.0.1,你就可以看到 django 的 "It work" 了。