1. 程式人生 > >生產環境使用Nginx+uwsgi部署Diango項目

生產環境使用Nginx+uwsgi部署Diango項目

download 不用 true 項目部署 web服務器 world edi 目錄 require

環境:CentOS6.5 + Nginx1.11.5 + Python3.5.2

1. 安裝基礎軟件包

yum install -y zlib-devel bzip2-devel pcre-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel

2. 安裝Python3.5.2版本

源碼包下載,戳我

wget https://www.python.org/ftp/python/3.5.2/Python-3.5.2.tar.xz
tar xf Python-3.5.2.tar.xz 
cd Python-3.5.2
./configure
make -j 2
make altinstall

PS:Python3.x默認已經安裝pip等包管理工具,如果沒有需要手動安裝`pip`包管理工具

3. uWSGI的安裝與使用

什麽是uwsgi,what?

uWSGI是一個Web服務器,它實現了WSGI協議、uwsgi、http等協議。Nginx中HttpUwsgiModule的作用是與uWSGI服務器進行交換。

要註意 WSGI / uwsgi / uWSGI 這三個概念的區分。

  1. WSGI是一種Web服務器網關接口。它是一個Web服務器(如nginx,uWSGI等服務器)與web應用(如用Flask框架寫的程序)通信的一種規範。
  2. uwsgi是一種線路協議而不是通信協議,在此常用於在uWSGI服務器與其他網絡服務器的數據通信。
  3. 而uWSGI是實現了uwsgi和WSGI兩種協議的Web服務器。
  4. uwsgi協議是一個uWSGI服務器自有的協議,它用於定義傳輸信息的類型(type of information),每一個uwsgi packet前4byte為傳輸信息類型描述,它與WSGI相比是兩樣東西。

uWSGI的主要特點如下

  1. 超快的性能
  2. 低內存占用(實測為apache2的mod_wsgi的一半左右)
  3. 多app管理(終於不用冥思苦想下個app用哪個端口比較好了-.-)
  4. 詳盡的日誌功能(可以用來分析app性能和瓶頸)
  5. 高度可定制(內存大小限制,服務一定次數後重啟等)

安裝uWSGI

# Install the latest stable release:
pip install uwsgi
# ... or if you want to install the latest LTS (long term support) release,
pip install https://projects.unbit.it/downloads/uwsgi-lts.tar.gz

基本測試

創建測試文件

# test.py
def application(env, start_response):
    start_response(‘200 OK‘, [(‘Content-Type‘,‘text/html‘)])
    return [b"Hello World"] # python3
    #return ["Hello World"] # python2

運行

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

使用瀏覽器訪問`http://ip:8000`驗證

使用uWSGI運行Django

uwsgi --http :8000 --module mysite.wsgi

使用瀏覽器訪問`http://ip:8000`驗證

可以將參數寫到一個配置文件中

[root@localhost Django_test]# vim mysite_uwsgi.ini
[uwsgi]
http = :9000
#the local unix socket file than commnuincate to Nginx
socket = 127.0.0.1:8001
# the base directory (full path)
chdir = /usr/local/Django_test
# Django‘s wsgi file
wsgi-file = Django_test/wsgi.py
# maximum number of worker processes
processes = 4
#thread numbers startched in each worker process
threads = 2

#monitor uwsgi status
stats = 127.0.0.1:9191
# clear environment on exit or restart
vacuum          = true

啟動

uwsgi --ini mysite_uwsgi.ini

使用瀏覽器訪問`http://ip:9000`驗證

3. 安裝並配置Nginx

useradd -M -s /sbin/nologin nginx
tar zxf nginx-1.11.5.tar.gz 
cd nginx-1.11.5
./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module --with-http_gzip_static_module
make && make install
ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/

修改nginx配置文件

#修改主配置文件,引入下面要寫的uwsgi的配置文件,也可以在當前配置文件中寫
# vim /usr/local/nginx/conf/nginx.conf
#在http區段中
include mysite_uwsgi_nginx.conf;

創建一個uwsgi的配置文件

# vim /usr/local/nginx/conf/mysite_uwsgi_nginx.conf

# the upstream component nginx needs to connect to
upstream django {
    # server unix:///path/to/your/mysite/mysite.sock; # for a file socket
    server 127.0.0.1:8001; # for a web port socket (we‘ll use this first)
}

# configuration of the server
server {
    # the port your site will be served on
    listen      8000;
    # the domain name it will serve for
    server_name .example.com; # substitute your machine‘s IP address or FQDN
    charset     utf-8;

    # max upload size
    client_max_body_size 75M;   # adjust to taste

    location /static {
        alias /usr/local/Django_test/static; # your Django project‘s static files - amend as required
    }

    # Finally, send all non-media requests to the Django server.
    location / {
        uwsgi_pass  django;
        include     uwsgi_params; # the uwsgi_params file you installed
    }
}

到此Django項目部署已經部署完成,你可以訪問我們寫的頁面,但是訪問`http://ip:8000/admin`卻發現沒有樣式,waht?

技術分享圖片

技術分享圖片

這個問題是由於admin的樣式文件都在django內部,而不是在我們的項目的靜態文件的目錄中,到此我們需要把所有的靜態文件匯聚到一個目錄中

1. 修改項目目錄中的`setting.py`文件,添加 (all_statics可以自己指定目錄)

STATIC_ROOT = os.path.join(BASE_DIR, "all_statics/")

2.run (提示輸入yes)

python manage.py collectstatic

3.現在我們需要去修改nginx的配置文件,將靜態文件的目錄修改為匯聚後的靜態文件的目錄

location /static {
        alias /usr/local/Django_test/all_statics;
    }

現在去啟動nginx和uwsgi

[root@localhost Django_test]# # nginx 
[root@localhost Django_test]# uwsgi mysite_uwsgi.ini &

現在admin頁面總算可以看了

技術分享圖片

生產環境使用Nginx+uwsgi部署Diango項目