在網上搜了不少篇Django+uwsgi+Nginx的部署文章,忙了一下午頭昏腦脹,最終完成了部署。部署文章流程講解都很好,但在細節上或許缺少一些注意力,致使我多篇文章來回切換在字裏行間尋找蛛絲馬跡。既然我完成了部署,那我何不寫下本身的流程和心得,讓後來人有一些參考?javascript
原理php
Django+uwsgi+Nginx的工做模式以下圖所示,三個服務各司其職,分別爲:css
部署流程html
1、完成的Django項目java
首先,要有一個完成的Django項目,經過瀏覽器能打開。node
在個人服務器上有一個tutotial的Django項目,是個博客的學習項目。python
使用python manager.py runserver 0.0.0.0:8000可以運行。經過瀏覽器訪問是正常的。nginx
2、安裝uwsgi並配置web
2.1 安裝uswgi。
數據庫
使用pip工具安裝uswgi,pip install uswgi
使用一個測試程序來測試uswgi可否正常運行。新建一個test.py,內容以下。
test.py
使用命令:uwsgi --http :8002 --wsgi-file test.py,開啓一個uswgi的服務。--http指訪問端口爲8002,--wsgi-file 指uwsgi的請求處理函數爲test.py。以下圖所示,爲開啓以後正常的輸出項
經過瀏覽器可以訪問到該服務。當訪問端口8002時,處理函數爲test.py中的application,返回一個text,格式爲html,內容是「hello world」。
2.2 配置uswgi文件
使用uswgi做爲nginx和django之間的搬運工,要將服務長久化就必須寫一個配置文件。以下配置文件,路徑在django主目錄,是對上一步驟的命令行中命令的文件化。其中咱們熟悉的配置項:
除此以外還有:
uwsgi起到一個橋樑的做用,具體體如今:wsgi-file鏈接django項目,socket鏈接Nginx。
uwsgi.ini
[uwsgi] # 對外提供 http 服務的端口 http = :9000 #the local unix socket file than commnuincate to Nginx 用於和 nginx 進行數據交互的端口 socket = 127.0.0.1:8001 # the base directory (full path) django 程序的主目錄 chdir = /home/ccf/code/django_learn/tutorial # Django's wsgi file
wsgi-file = tutorial/wsgi.py # maximum number of worker processes processes = 4 #thread numbers startched in each worker process threads = 2 #monitor uwsgi status 經過該端口能夠監控 uwsgi 的負載狀況 stats = 127.0.0.1:9191 # clear environment on exit vacuum = true
# 後臺運行,並輸出日誌 daemonize = /var/log/uwsgi.log
2.3 運行uwsgi
使用uwsgi可執行文件,載入uwsgi.ini配置文件。
當程序運行以後,有兩個端口能夠訪問到:
以下使用9000端口,訪問到django的項目。
3、安裝Nginx服務器
3.1 安裝Nginx
使用apt-get install nginx,一鍵安裝nginx。
3.2 配置nginx
上面的配置中,uswgi已經鏈接到django,大橋的一端已經通車,剩下的就nginx和uwsgi合龍。nginx的配置項路徑爲:/etc/nginx/nginx.conf。以下是nginx的完整配置文件,修改以下藍色字體。在http的配置項中增長了一個server配置項,該server就是鏈接uwsgi服務的配置。
/etc/nginx/nginx.conf
user root; worker_processes auto; pid /run/nginx.pid; events { worker_connections 768; # multi_accept on; } http { ## # Basic Settings ## sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 65; types_hash_max_size 2048; # server_tokens off; # server_names_hash_bucket_size 64; # server_name_in_redirect off; include /etc/nginx/mime.types; default_type application/octet-stream; ## # SSL Settings ## ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE ssl_prefer_server_ciphers on; ## # Logging Settings ## access_log /var/log/nginx/access.log; error_log /var/log/nginx/error.log; ## # Gzip Settings ## gzip on; gzip_disable "msie6"; # gzip_vary on; # gzip_proxied any; # gzip_comp_level 6; # gzip_buffers 16 8k; # gzip_http_version 1.1; # gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript; ## # Virtual Host Configs ## include /etc/nginx/conf.d/*.conf; include /etc/nginx/sites-enabled/*; server { #這裏是訪問時用到的端口 listen 8000; server_name 127.0.0.1; charset UTF-8; #這塊存讓日誌文件 access_log /var/log/nginx/SchoolBuy_access.log; error_log /var/log/nginx/SchoolBuy_error.log; client_max_body_size 75M; location / {
#必定要有該配置項 include uwsgi_params; #同uwsgi內容,鏈接uwsgi的socket。 uwsgi_pass 127.0.0.1:8001; #連接超時時間 uwsgi_read_timeout 30; } location /static/ { autoindex on; #這裏的路徑必定要到達靜態文件的文件夾。即collectedstatic/,重點在最後的橫槓。否則會報錯 alias /home/ccf/code/django_learn/tutorial/collectedstatic/; } location /media/ { autoindex on; alias /home/ccf/code/django_learn/tutorial/media/; } } } #mail { # # See sample authentication script at: # #http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript # # # auth_http localhost/auth.php; # # pop3_capabilities "TOP" "USER"; # # imap_capabilities "IMAP4rev1" "UIDPLUS"; # # server { # listen 8088; # protocol pop3; # proxy on; # } # # server { # listen localhost:143; # protocol imap; # proxy on; # } #}
listen nginx監聽的端口
include uwsgi_params 必填項
uwsgi_pass 127.0.0.1:8001 鏈接uwsgi的socket的端口。
其中稍難理解的是location/staic 和 location/media。簡單來說,在一個完整的django項目中,爲了便於django發佈應用,就是部署到web服務器,會將全部的靜態文件和圖形圖像文件放在一個指定的文件夾中,這樣Nginx收到請求以後就不用再去詢問django,而是直接去指定的靜態文件夾中獲取。該方法極大的增長了併發的數量。static和media就是django的兩個靜態文件夾,在個人環境中以下:
media文件根據django項目而定是否有,若是沒有collectedstatic文件夾要執行以下兩步:
一、配置setting.py文件,添加STATIC_ROOT配置項,指定靜態文件的收集文件夾當前路徑下的collectedstatic/,當前路徑指django主項目路徑。
二、執行 python manage.py collectstatic,收集全部的靜態文件。
三、能夠查看在django主目錄下出現了collectedstatic,內容以下,爲admin的後臺的css,js等文件和blog項目的靜態文件。
3.3 使配置生效
Nginx檢查配置文件
nginx -t 是nginx檢查配置文件是否有錯誤的命令,若是正常,輸出以下:
使配置文件生效
nginx -s reload 是nginx從新載入配置文件的命令,可讓配置項生效。
3.4 瀏覽器訪問
瀏覽器可以正常訪問,部署成功。
在部署中每一個人都會遇到本身的問題,個人經驗也不適用於全部人。參考個人過程,若是遇到問題,歡迎留言交流。