標籤:top double lock style 類型 cli expires related wiki
一、安裝uwsgi
通過pip安裝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 filemodule = myproject.wsgi # process-related settings# mastermaster = true # maximum number of worker processesprocesses = 4 # ... with appropriate permissions - may be needed# chmod-socket = 664# clear environment on exitvacuum = true daemonize = /mnt/myproject/uwsgi_log.log pidfile = /mnt/myproject/uwsgi_pid.log |
再接下來要做的就是修改nginx.conf設定檔。開啟/etc/nginx/nginx.conf檔案,添加如下內容。
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/; } } |
listen 指定的是nginx 對外的連接埠號碼。
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.log7 /etc/init.d/nginx stop
好了 ,現在我們可以訪問127.0.0.1:8099即可看到我們自己的項目了
【python】Django web項目部署(Nginx+uwsgi)