提高Django高並發性的部署方案(Python)


方案: nginx + uWSGI 提高 Django的並發性
        1. uWSGI :  
               uWSGI是一個web服務器,實現了WSGI協議、uwsgi協議、http協議等。


           uWSGI的主要特點是:

               超快的性能
              低內存占用
              多app管理
              詳盡的日志功能(可以用來分析app的性能和瓶頸)
              高度可定制(內存大小限制,服務一定次數后重啟等)
             uWSGI服務器自己實現了基於uwsgi協議的server部分,我們只需要在uwsgi的配置文件中指定application的地址,uWSGI就                       能直接和應用框架中的WSGI application通信。

        2.  nginx :   
               Nginx 是一個高性能的負載均衡HTTP和反向代理服務器,Nginx是一款輕量級的Web 服務器/反向代理服務器及電子郵件代理服務器。

                  特點是占有內存少,並發能力強。


          結構與擴展:一個主進程和多個工作進程。工作進程是單線程的,且不需要特殊授權即可運行;
3. nginx和uWSGI的關系:
            nginx相當於是服務器,負責接收請求

            uwsgi是服務器和服務端應用程序的通信協議,規定了怎么把請求轉發給應用程序和返回

            2個基本概念: 
                服務器(接收請求),應用程序(處理請求並返回)

            通信過程: 

                客戶端發送一個http請求,被nginx服務器接收,nginx服務器將請求轉發給uwsgi,uwsgi將請求轉發給實現uwsgi

               協議的應用程序(flask,gunicorn等等)

        4.  uWSGI的安裝:
                  4.1:  [root@crazy-acong ~]# pip3 install uwsgi

4.2: 創建django項目並配置uWSGI配置文件:

cd 進入到 django 的主目錄

vi test-uwsgi.ini  # 添加以下配置文件,按需修改即可:

[uwsgi]
# 對外提供 http 服務的端口
http = :8888
#用於和 nginx 進行數據交互的端口
socket = 127.0.0.1:8899
# django 程序的主目錄。
chdir = /project/django
# Django's wsgi file
wsgi-file = /project/django_test/django_test/wsgi.py
# 最大的工作進程數
processes = 4
#在每個進程中的最大線程數
threads = 2
# 通過該端口可以監控 uwsgi 的負載情況
stats = 127.0.0.1:9999
# 清理環境出口
vacuum          = true
# 后台運行,並輸出日志
daemonize = /var/log/uwsgi.log
                 4.3  通過 uwsgi 配置文件啟動 django 程序


uwsgi test-uwsgi.ini # 在瀏覽器中 通過訪問 http://ip:9000 可以看到發布的 django 程序
4.4. 查看uwsgi的啟動進程狀態
netstat -lnpt | grep uwsgi

      4.4 .  安裝nginx : 

           apt-get install nginx

     

5、配置 nginx 的配置文件

在 django 的主目錄下創建下面的 nginx 配置文件,然后做軟連接到 nginx 的配置文件目錄,或者直接在 nginx 配置文件目錄中添加該文件也可以

5.1 創建 nginx 配置文件


[root@crazy-acong django_test]# cat /data/django_test/django-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

# Django media
location /media {
alias /path/to/your/mysite/media; # your Django project's media files - amend as required
}

location /static {
alias /data/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 /data/django_test/uwsgi_params; # the uwsgi_params file you installed
}
}

5.2 重啟nginx 服務


[root@crazy-acong django_test]# nginx -t
nginx: the configuration file /data/application/nginx-1.10.3/conf/nginx.conf syntax is ok
nginx: configuration file /data/application/nginx-1.10.3/conf/nginx.conf test is successful
[root@crazy-acong django_test]# nginx -s reload

[root@crazy-acong django_test]# netstat -lnpt | grep 8000
tcp 0 0 0.0.0.0:8000 0.0.0.0:* LISTEN 43492/nginx

這個時候就可以通過 http://ip:8000 訪問 django 程序了,不過目前還存在一個問題,訪問 http://ip:8000/admin 發現靜態文件貌似沒讀取到,需要通過下面的方法解決靜態文件的問題

 

6、解決 django 多 app 靜態文件的問題


# 在 django 程序的 settings.py 文件中添加以下內容

STATIC_ROOT = os.path.join(BASE_DIR, "static_all")


# 然后通過執行該命令,將靜態文件整合到一個目錄中
[root@crazy-acong django_test]# python3 manage.py collectstatic

[root@crazy-acong django_test]# ll
total 40
drwxr-xr-x 3 nginx games 4096 Mar 14 14:42 app01
-rw-r--r-- 1 root root 3072 Mar 14 14:51 db.sqlite3
-rw-r--r-- 1 root root 1026 Mar 14 15:18 django-nginx.conf
drwxr-xr-x 3 nginx games 4096 Mar 14 15:45 django_test
-rwxr-xr-x 1 nginx games 809 Mar 14 14:37 manage.py
drwxr-xr-x 2 nginx games 4096 Mar 14 14:42 static
drwxr-xr-x 3 root root 4096 Mar 14 15:47 static_all # 此時會發現多了一個該目錄,所有 app 的靜態文件都整合到這一個目錄中了
drwxr-xr-x 2 nginx games 4096 Mar 14 14:40 templates
-rw-r--r-- 1 root root 565 Mar 14 15:40 test-uwsgi.ini
-rw-r--r-- 1 root root 664 Mar 14 15:28 uwsgi_params

 

然后需要修改 nginx 配置文件中 指向 django 靜態目錄的配置文件


[root@crazy-acong django_test]# cat /data/django_test/django-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

# Django media
location /media {
alias /path/to/your/mysite/media; # your Django project's media files - amend as required
}

location /static {
     # 需要修改的地方在這里
alias /data/django_test/static_all; # your Django project's static files - amend as required
}

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

最后重啟 nginx 服務即可

[root@crazy-acong django_test]# nginx -t
nginx: the configuration file /data/application/nginx-1.10.3/conf/nginx.conf syntax is ok
nginx: configuration file /data/application/nginx-1.10.3/conf/nginx.conf test is successful
[root@crazy-acong django_test]# nginx -s reload
---------------------
作者:LIJZ_Python
來源:CSDN
原文:https://blog.csdn.net/yinhangxitong36/article/details/79821851
版權聲明:本文為博主原創文章,轉載請附上博文鏈接!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM