django生產環境部署


測試環境:linux centos7下

1、安裝uwsgi

python3下安裝:

pip3 install uwsgi

python2下安裝:

pip install uwsgi

如果是系統自帶的python2.7環境下安裝的話,有可能會出錯:

Command "/usr/bin/python2 -u -c "import setuptools, tokenize;__file__='/tmp/pip-build-
jjOBXy/uwsgi/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install --record /tmp/pip-3cX7u0-record/install-record.txt --single-version-externally-managed --compile" failed with error code 1 in /tmp/pip-build-jjOBXy/uwsgi/

這時候需要我們先安裝一個python開發包:

yum install -y python-devel

然后就可以安裝了

2、測試 uwsgi

#python 3.x
def application(env, start_response):
    start_response('200 OK', [('Content-Type','text/html')])
    return [b"Hello World"]   #Python 3.x 需要 返回字節字符串

#python 2.x
#def application(env, start_response):
#    start_response('200 OK', [('Content-Type','text/html')])
#    return ["Hello World"]
test.py

運行:

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

3、添加並發和監控

並發:

uWSGI 默認啟動一個單獨的進程和一個單獨的線程

我們可以通過 --processes 選項或者 --threads (或者兩個選項都使用)來增加更多的進程或者線程

uwsgi --http :8000 --wsgi-file test.py --master --processes 4 --threads 2

將會產生 4 個進程(每個進程 2 個線程),一個主進程(當你的進程死掉時會重新 spawn 一個新的)以及 HTTP 路由器

 監控:

 知道發生了什么在生產環境中是極其重要的。stats 子系統允許你 用 JSON 輸出 uWSGI 的內部數據:

uwsgi --http :8000 --wsgi-file test.py  --master --processes 4 --threads 2 --stats 127.0.0.1:8181

用 “uwsgitop”監控應用實例

pip isntall uwsgitop

uwsgitop--uwsgi服務器性能查看工具,用法:

uwsgitop 127.0.0.1:8181

3、連接Django和uwsgi

先測試Django項目本身是可運行的:

python manage.py runserver 0.0.0.0:8001

倘若django 項目沒問題,我們就可以把uwsgi與Django連接起來

進入到django項目中,以uwsgi啟動django

 uwsgi --http :8001 --module WebSite.wsgi

4、編寫uwsgi配置文件[uwsgi]

demo

[uwsgi]

socket = 127.0.0.1:8000 chdir = /home/zhi_hu_04 wsgi-file = zhi_hu/wsgi.py home = /home/mysite_venv master = true processes = 4 threads = 2 vacum = true

  daemonize=/var/log/uwsgi.log #使進程在后台運行,並將日志打到指定的日志文件

然后放到django項目中

啟動uwsgi:

uwsgi --ini path/to/project/uwsgi.ini

然后成功訪問到我們的django項目

39.108.132.2xx:9000

5、安裝Nginx

yum -y install nginx

啟動Nginx

systemctl start nginx.service

關閉Nginx

systemctl stop nginx.service

重啟

systemctl restart nginx.service

 設置開機啟動

systemctl enable nginx

查看nginx 啟動狀態

systemctl status nginx

查看是否監聽

 ss -tnl | grep 80 

查看nginx進程命令

 ps -ef | grep  nginx

pkill -9 nginx

nginx配置

方法一:直接修改nginx.conf:

 server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  _;         
        root         /usr/share/nginx/html;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;
     #添加
        location / {
        include      uwsgi_params;
        uwsgi_pass   127.0.0.1:8001; #要跟uwsgi中的綁定的socket一致
        }
#靜態文件

        location /static {

            alias /home/www/library_system/static/;

        }

        #日志

        access_log /var/log/nginx_access.log;

        error_log /var/log/nginx_error.log;

       #添加

        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }

 

方法二:添加站點配置

在項目中添加website_nginx.conf

demo

server {
    #偵聽80端口
    listen      80;
    #網站域名
    server_name  www.xxx.com  xxx.com  www.xxx.xyz    xxx.xyz;
    #rewrite 規則   如可將www.zhihu086.com 請求轉發至 www.zhihu086.com/bbb/
     # rewrite ^/$ /bbb/ break; 
    charset     utf-8;
    #上傳最大限制75M
    client_max_body_size 75M;

    #你的Django項目的媒體文件路徑
    location /media  {
        alias /home/zhi_hu_04/media;
    }

    # 靜態文件路徑
    location /static {
        alias /home/zhi_hu_04/static;
    }

    # 將動態請求轉發到uwsgi
    location / {
        #一定要和uwsgi的對應
        uwsgi_pass 127.0.0.1:8000;
        # uwsgi_params文件要新建寫入配置
        include  uwsgi_params;
    }
}

然后將站點配置軟鏈接到 /etc/nginx/conf.d 目錄下

或者,直接 放到/etc/nginx/conf.d

ln -s /root/workplace/WebSite/website_nginx.conf /etc/nginx/conf.d/website_nginx.conf

最后:

 copy  /etc/nginx下的uwsgi_params 文件 到項目中 

重啟nginx  

啟動uwsgi.ini

6、Django靜態文件收集

 把Django所有靜態文件收集到同一個static中,不然訪問Django的admin頁面會找不到靜態文件。在django的setting文件中添加:

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

然后運行:

python manage.py collectstatic

 


免責聲明!

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



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