本文采用uwsgi+nginx來部署Django,環境是ubuntu16.04javascript
這種方式是將nginx做爲服務器前端,將接受web全部的請求,統一管理。Nginx把全部的靜態請求本身處理(靜態文件處理是ngInx強項),而後把全部非靜態請求經過uwsgi傳遞給Django,由Django來處理,從而完成一次web請求。php
1、uWSGIcss
pip install uwsgi
$ uwsgi --version
2.0.15
# test.py def application(env, start_response): start_response('200 OK', [('Content-Type','text/html')]) return [b"Hello World"] # python3 #return ["Hello World"] # python2
uwsgi --http :8000 --wsgi-file test.py
參數解釋:html
http :8000表示使用http協議,端口號爲8000,前端
wigi-file則表示要運行的wsgi應用程序文件。java
uwsgi運行後打開瀏覽器,訪問http://127.0.0.1:8000/ ,或者是相應服務器地址的8000端口,就能夠看到hello world 頁面了
若是想要運行項目來測試python
# uwsgi --http :8000 --chdir 項目路徑 -w 項目.wsg --static-map=/static=static uwsgi --http :8000 --chdir /home/teacher/ -w teacher.wsgi --static-map=/static=static
uWSGI經常使用命令:nginx
uwsgi --chdir=/path/to/your/project \ --module=mysite.wsgi:application \ --env DJANGO_SETTINGS_MODULE=mysite.settings \ --master --pidfile=/tmp/project-master.pid \ --socket=127.0.0.1:49152 \ # 能夠ip地址,也能夠是文件 --processes=5 \ # 進程數量 --uid=1000 --gid=2000 \ # 若是是root用戶,uwsgi能夠有刪除權限 --harakiri=20 \ # 一個請求超時時間 --max-requests=5000 \ # 一個工做進程最大請求數 --vacuum \ # 退出時清楚環境 --home=/path/to/virtual/env \ # virtualenv的路徑 -- static # 作一個映射,指定靜態文件 --http # 這個就和runserver同樣指定IP 端口 --daemonize=/var/log/uwsgi/yourproject.log # 日誌
[uwsgi] # 項目目錄 chdir=/opt/project_teacher/teacher/ # 指定項目的application module=teacher.wsgi:application # 進程個數 workers=5 pidfile=/opt/project_teacher/script/uwsgi.pid # 指定IP端口 http=192.168.31.123:8080 # 指定靜態文件 static-map=/static=/opt/test_project/teacher/static # 啓動uwsgi的用戶名和用戶組 uid=root gid=root # 啓用主進程 master=true # 自動移除unix Socket和pid文件當服務中止的時候 vacuum=true # 序列化接受的內容,若是可能的話 thunder-lock=true # 啓用線程 enable-threads=true # 設置自中斷時間 harakiri=30 # 設置緩衝 post-buffering=4096 # 設置日誌目錄 daemonize=/opt/project_teacher/script/uwsgi.log # 指定sock的文件路徑 socket=/opt/project_teacher/script/uwsgi.sock
$ uwsgi --ini uwsgi.ini # 啓動uwsgi配置 [uwsgi-static] added mapping for /static => /home/trunk/static # 啓動成功 $ uwsgi --stop uwsgi.pid # 關閉uwsgi signal_pidfile()/kill(): Operation not permitted [core/uwsgi.c line 1659]
2、Nginxweb
$ sudo apt-get install nginx #安裝
$ /etc/init.d/nginx start
[ ok ] Starting nginx (via systemctl): nginx.service.
檢查nginx是否啓動成功json
$ ps -ef |grep -i nginx root 6961 1 0 03:56 ? 00:00:00 nginx: master process /usr/sbin/nginx -g daemon on; master_process on; www-data 6962 6961 0 03:56 ? 00:00:00 nginx: worker process pala 6985 2090 0 03:57 pts/0 00:00:00 grep --color=auto -i nginx
而後打開瀏覽器,訪問ip地址,出現以下頁面即表明nginx安裝完成且能夠正常啓動。
Nginx經常使用命令
$ /etc/init.d/nginx start #啓動 $ /etc/init.d/nginx stop #關閉 $ /etc/init.d/nginx restart #重啓
3、Django + uWSGI +Nginx
$ vim /etc/nginx/conf.d/xxx.conf
server { # 這個server標識我要配置了 listen 80; # 我要監聽那個端口 server_name 10.129.205.183 ; # 你訪問的路徑前面的url名稱 access_log /var/log/nginx/access.log main; # Nginx日誌配置 charset utf-8; # Nginx編碼 gzip on; # 啓用壓縮,這個的做用就是給用戶一個網頁,好比3M壓縮後1M這樣傳輸速度就會提升不少 gzip_types text/plain application/x-javascript text/css text/javascript application/x-httpd-php application/json text/json image/jpeg image/gif image/png application/octet-stream; # 支持壓縮的類型 error_page 404 /404.html; # 錯誤頁面 error_page 500 502 503 504 /50x.html; # 錯誤頁面 # 指定項目路徑uwsgi location / { # 這個location就和我們Django的url(r'^admin/', admin.site.urls), include uwsgi_params; # 導入一個Nginx模塊他是用來和uWSGI進行通信的 uwsgi_connect_timeout 30; # 設置鏈接uWSGI超時時間 uwsgi_pass unix:/opt/project_teacher/script/uwsgi.sock; # 指定uwsgi的sock文件全部動態請求就會直接丟給他 } # 指定靜態文件路徑 location /static/ { alias /opt/project_teacher/teacher/static/; index index.html index.htm; }
uwsgi_params文件內容(放在項目根目錄下)
uwsgi_param QUERY_STRING $query_string;
uwsgi_param REQUEST_METHOD $request_method;
uwsgi_param CONTENT_TYPE $content_type;
uwsgi_param CONTENT_LENGTH $content_length;
uwsgi_param REQUEST_URI $request_uri;
uwsgi_param PATH_INFO $document_uri;
uwsgi_param DOCUMENT_ROOT $document_root;
uwsgi_param SERVER_PROTOCOL $server_protocol;
uwsgi_param REQUEST_SCHEME $scheme;
uwsgi_param HTTPS $https if_not_empty;
uwsgi_param REMOTE_ADDR $remote_addr;
uwsgi_param REMOTE_PORT $remote_port;
uwsgi_param SERVER_PORT $server_port;
uwsgi_param SERVER_NAME $server_name;
重啓nginx
$ /etc/init.d/nginx restart #重啓
來源一位知乎小姐姐:https://zhuanlan.zhihu.com/p/29083368
對了,插一嘴,在ubuntu中,普通用戶對計算機中的文件如 opt/ 、etc/等文件夾中內容修改時,沒有辦法直接修改,除了命令行執行命令加上sudo以外,能夠使用sudo nautilus來移動或刪除文件。有問題能夠直接將問題發送至1005819387@qq.com來討論,或者在評論裏直接說也能夠