【Django】Django web项目部署(Nginx+uwsgi)
一、安裝uwsgi
?通過pip安裝uwsgi。
| pip install uwsgi |
測(cè)試uwsgi,創(chuàng)建test.py文件:
| def application(env, start_response): ????start_response('200 OK', [('Content-Type','text/html')]) ????return [b"Hello World"] |
通過uwsgi運(yùn)行該文件。
| uwsgi --http :8001 --wsgi-file test.py |
常用選項(xiàng):
http?:?協(xié)議類型和端口號(hào)
processes?:?開啟的進(jìn)程數(shù)量
workers?:?開啟的進(jìn)程數(shù)量,等同于processes(官網(wǎng)的說法是spawn?the?specified?number?ofworkers?/?processes)
chdir?:?指定運(yùn)行目錄(chdir?to?specified?directory?before?apps?loading)
wsgi-file?:?載入wsgi-file(load?.wsgi?file)
stats?:?在指定的地址上,開啟狀態(tài)服務(wù)(enable?the?stats?server?on?the?specified?address)
threads?:?運(yùn)行線程。由于GIL的存在,我覺得這個(gè)真心沒啥用。(run?each?worker?in?prethreaded?mode?with?the?specified?number?of?threads)
master?:?允許主進(jìn)程存在(enable?master?process)
daemonize?:?使進(jìn)程在后臺(tái)運(yùn)行,并將日志打到指定的日志文件或者udp服務(wù)器(daemonize?uWSGI)。實(shí)際上最常用的,還是把運(yùn)行記錄輸出到一個(gè)本地文件上。
pidfile?:?指定pid文件的位置,記錄主進(jìn)程的pid號(hào)。
vacuum?:?當(dāng)服務(wù)器退出的時(shí)候自動(dòng)清理環(huán)境,刪除unix?socket文件和pid文件(try?to?remove?all?of?the?generated?file/sockets)
二、安裝nginx
| sudo apt-get install nginx? #安裝 |
啟動(dòng)Nginx:
| /etc/init.d/nginx start? #啟動(dòng) /etc/init.d/nginx stop? #關(guān)閉 /etc/init.d/nginx restart? #重啟 |
三、Django部署
在我們用python manager.py startproject myproject創(chuàng)建項(xiàng)目時(shí),會(huì)自動(dòng)為我們生成wsgi文件,所以,我們現(xiàn)在之需要在項(xiàng)目目錄下創(chuàng)建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中添加如下內(nèi)容。
| 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? 設(shè)置為域名或指定的到本機(jī)ip。
nginx通過下面兩行配置uwsgi產(chǎn)生關(guān)聯(lián):
| include uwsgi_params; uwsgi_pass 127.0.0.1:8000; //必須與uwsgi中配置的端口一致 |
最后我們?cè)陧?xiàng)目目錄下執(zhí)行下面的命令來啟動(dòng)關(guān)閉我們的項(xiàng)目:
1 #啟動(dòng) 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好了 ,現(xiàn)在我們可以訪問127.0.0.1:8099即可看到我們自己的項(xiàng)目了
轉(zhuǎn)載于:https://www.cnblogs.com/perfe/p/6196854.html
總結(jié)
以上是生活随笔為你收集整理的【Django】Django web项目部署(Nginx+uwsgi)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Android 活体人脸实时采集,百度、
- 下一篇: 厉害了!20年【科比NBA】生涯|数据分