【Django】Django web项目部署(Nginx+uwsgi)
一、安裝uwsgi
?通過pip安裝uwsgi。
| pip install uwsgi |
測試uwsgi,創建test.py文件:
| def application(env, start_response): ????start_response('200 OK', [('Content-Type','text/html')]) ????return [b"Hello World"] |
通過uwsgi運行該文件。
| uwsgi --http :8001 --wsgi-file test.py |
常用選項:
http?:?協議類型和端口號
processes?:?開啟的進程數量
workers?:?開啟的進程數量,等同于processes(官網的說法是spawn?the?specified?number?ofworkers?/?processes)
chdir?:?指定運行目錄(chdir?to?specified?directory?before?apps?loading)
wsgi-file?:?載入wsgi-file(load?.wsgi?file)
stats?:?在指定的地址上,開啟狀態服務(enable?the?stats?server?on?the?specified?address)
threads?:?運行線程。由于GIL的存在,我覺得這個真心沒啥用。(run?each?worker?in?prethreaded?mode?with?the?specified?number?of?threads)
master?:?允許主進程存在(enable?master?process)
daemonize?:?使進程在后臺運行,并將日志打到指定的日志文件或者udp服務器(daemonize?uWSGI)。實際上最常用的,還是把運行記錄輸出到一個本地文件上。
pidfile?:?指定pid文件的位置,記錄主進程的pid號。
vacuum?:?當服務器退出的時候自動清理環境,刪除unix?socket文件和pid文件(try?to?remove?all?of?the?generated?file/sockets)
二、安裝nginx
| sudo apt-get install nginx? #安裝 |
啟動Nginx:
| /etc/init.d/nginx start? #啟動 /etc/init.d/nginx stop? #關閉 /etc/init.d/nginx restart? #重啟 |
三、Django部署
在我們用python manager.py startproject myproject創建項目時,會自動為我們生成wsgi文件,所以,我們現在之需要在項目目錄下創建uwsgi的配置文件即可,我們采用ini格式:
| # myweb_uwsgi.ini file [uwsgi] # Django-related settings socket = :8000 # the base directory (full path) chdir?????????? = /mnt/myproject # Django s wsgi file module????????? = myproject.wsgi # process-related settings # master master????????? = true # maximum number of worker processes processes?????? = 4 # ... with appropriate permissions - may be needed # chmod-socket??? = 664 # clear environment on exit vacuum????????? = true daemonize?????? = /mnt/myproject/uwsgi_log.log pidfile = /mnt/myproject/uwsgi_pid.log |
再接下來要做的就是修改nginx.conf配置文件。打開/etc/nginx/nginx.conf文件,http中添加如下內容。
| server { ????listen???????? 8099; ????server_name??? 127.0.0.1 ????charset UTF-8; ????access_log????? /var/log/nginx/myweb_access.log; ????error_log?????? /var/log/nginx/myweb_error.log; ????client_max_body_size 75M; ????location / { ????????include uwsgi_params; ????????uwsgi_pass 127.0.0.1:8000; ????????uwsgi_read_timeout 2; ????}?? ????location /static { ????????expires 30d; ????????autoindex on; ????????add_header Cache-Control private; ????????alias /mnt/myproject/static/; ?????} ?} |
server_name? 設置為域名或指定的到本機ip。
nginx通過下面兩行配置uwsgi產生關聯:
| include uwsgi_params; uwsgi_pass 127.0.0.1:8000; //必須與uwsgi中配置的端口一致 |
最后我們在項目目錄下執行下面的命令來啟動關閉我們的項目:
1 #啟動 2 uwsgi --ini uwsgi.ini 3 /etc/init.d/nginx start 4 5 #停止 6 uwsgi --stop uwsgi_pid.log 7 /etc/init.d/nginx stop好了 ,現在我們可以訪問127.0.0.1:8099即可看到我們自己的項目了
轉載于:https://www.cnblogs.com/perfe/p/6196854.html
總結
以上是生活随笔為你收集整理的【Django】Django web项目部署(Nginx+uwsgi)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Android 活体人脸实时采集,百度、
- 下一篇: Nginx upstream 配置