目錄javascript
咱們在本地環境開發好咱們的Django項目,使用runserver很容易的就能在本地運行起來並訪問,可是隻能在本地局域網內訪問,若是須要生產環境部署咱們的web項目就須要多考慮一些問題:php
帶着這些疑問,咱們接着往下看。css
前提的準備工做:html
ALLOWED_HOSTS = ['*']
須要修改成容許任何人訪問或者限制IP訪問,多個ip逗號隔開。uwsgi是python的一個模塊,安裝uwsgi只需簡單的pip命令就能夠了java
pip3 install uwsgi
node
安裝完成後,它提供了兩種方式來啓動咱們的django項目,你任選一種均可以。python
進入咱們的django項目中:linux
[root@192.168.32.130]$ cd /data/app/Library_Management_System/ # 命令啓動 [root@192.168.32.130 /data/app/Library_Management_System]$ uwsgi --http 192.168.32.130:8080 --file Library_Management_System/wsgi.py --static-map=/static=static
參數說明:nginx
注意:有可能會啓動失敗,檢查指定的路徑是否存在,端口是否衝突,web
可使用命令檢查端口號:netstat -tunlp|grep xxx
若是須要關閉項目:kullall -9 uwsgi
或者 kill -9 進程號
啓動完成以後會夯住不動,提示相似信息,書名啓動成功,此時用url訪問咱們的django項目就能夠了。
[uwsgi-static] added mapping for /static => static *** Starting uWSGI 2.0.18 (64bit) on [Fri Nov 29 21:17:15 2019] *** compiled with version: 4.4.7 20120313 (Red Hat 4.4.7-23) on 30 November 2019 05:13:53 os: Linux-2.6.32-754.23.1.el6.x86_64 #1 SMP Thu Sep 26 12:05:41 UTC 2019 nodename: localhost.localdomain machine: x86_64 clock source: unix detected number of CPU cores: 1 current working directory: /data/app/Library_Management_System detected binary path: /usr/local/bin/uwsgi
首先在django項目manage.py 同級目錄下建立script目錄,用於存放配置文件腳本等。
# 建立script文件夾 [root@192.168.32.130 /data/app/Library_Management_System]$ mkdir script # 切換到script文件夾內 [root@192.168.32.130 /data/app/Library_Management_System]$ cd script/
新建立一個uwsgi.ini文件:
[root@192.168.32.130 /data/app/Library_Management_System/script]$ vim uwsgi.ini # uwsig使用配置文件啓動 [uwsgi] # 項目目錄 chdir=/data/app/Library_Management_System # 指定項目的application module=Library_Management_System.wsgi:application # 指定sock的文件路徑 socket=/data/app/Library_Management_System/script/uwsgi.sock # 進程個數 workers=5 pidfile=/data/app/Library_Management_System/script/uwsgi.pid # 指定IP端口 http=192.168.32.130:8080 # 指定靜態文件 static-map=/static=/data/app/Library_Management_System/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=/data/app/Library_Management_System/script/uwsgi.log
而後能夠啓動咱們的項目,出現如下提示信息,表示啓動成功。
[root@192.168.32.130 /data/app/Library_Management_System/script]$ uwsgi --ini uwsgi.ini [uWSGI] getting INI configuration from uwsgi.ini [uwsgi-static] added mapping for /static => /data/app/Library_Management_System/static [root@192.168.32.130 /data/app/Library_Management_System/script]$ [root@192.168.32.130 /data/app/Library_Management_System/script]$ netstat -tunlp|grep 8080 tcp 0 0 192.168.32.130:8080 0.0.0.0:* LISTEN 84518/uwsgi * 啓動:uwsgi --ini uwsgi.ini * 中止:uwsgi --stop uwsgi.pid * 重啓:uwsgi --reload uwsgi.pid
到如今uwsgi + django 已經完美結合了,可是光有uwsgi還不夠,uwsgi處理動態能力高,對於靜態文件請求,如css,js文件處理能力就差了,此時就須要結合靜態web服務器nginx一塊兒使用
略。。。。。
本身去網上找去
我已經在機器上配置好了,目錄:
[root@192.168.32.130 /usr/local/webserver/nginx/conf/vhosts]$
[root@192.168.32.130 ~]$ cd /usr/local/webserver/nginx/conf/vhosts/ vim django.conf server { listen 8000; # 訪問的端口 server_name 192.168.32.130; # 訪問的域名,能夠寫IP,也能夠寫ip。 charset utf-8; access_log logs/access.log main; access_log off; # 支持壓縮的類型 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同樣 include uwsgi_params; # 用來和uWSGI進行通信 uwsgi_connect_timeout 30; # 設置鏈接uWSGI超時時間 uwsgi_pass unix:/data/app/Library_Management_System/script/uwsgi.sock; # 指定uwsgi的sock文件全部動態請求就會直接丟給他 } # 指定靜態文件路徑 location /static/ { alias /data/app/Library_Management_System/static/; index index.html index.htm; } } 測試nginx配置是否正確: [root@192.168.32.130 /usr/local/webserver/nginx/conf/vhosts]$ /usr/local/webserver/nginx/sbin/nginx -t nginx: the configuration file /usr/local/webserver/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/webserver/nginx/conf/nginx.conf test is successful 啓動nginx: [root@192.168.32.130 /usr/local/webserver/nginx/conf/vhosts]$ /etc/init.d/nginx reload Reloading nginx: [ OK ] # 查看啓動的端口: [root@192.168.32.130 /usr/local/webserver/nginx/conf/vhosts]$ netstat -tunlp|grep 8080
至此,一個django項目已經部署上線了。
能夠訪問到url :http://192.168.32.130:8000/app01/home/
[root@192.168.32.130 /usr/local/webserver/nginx/conf/vhosts/siege-2.67]$ curl -I http://192.168.32.130:8000/app01/home/ HTTP/1.1 200 OK Server: CWS/3.0 Date: Sat, 30 Nov 2019 06:15:55 GMT Content-Type: text/html; charset=utf-8 Content-Length: 2398 Connection: keep-alive Vary: Accept-Encoding X-Frame-Options: SAMEORIGIN
若是說咱們的項目是先後端分離的狀況,這時候靜態文件的目錄確定是不同的,這時候可讓python自動幫咱們收集一些靜態文件,而後幫咱們放到指定的目錄裏面去,
STATIC_ROOT='/var/www/static/' STATIC_URL='/static/'
收集全部靜態文件到static_root指定目錄:
python3 manage.py collectstatic
重啓nginx、uwsgi
http://192.168.32.130:8000/app01/home/
安裝siege:
wget http://soft.vpser.net/test/siege/siege-2.67.tar.gz tar zxf siege-2.67.tar.gz cd siege-2.67 ./configure && make && make install siege -c 10000 -r 10 http://127.0.0.1/ -c是併發量,-r是重複次數。
[root@192.168.32.130 /usr/local/webserver/nginx/conf/vhosts/siege-2.67]$ siege -c 2000 -r 10 http://192.168.32.130:8000/app01/home/ done. siege aborted due to excessive socket failure; you can change the failure threshold in $HOME/.siegerc Transactions: 3722 hits Availability: 69.69 % Elapsed time: 21.02 secs Data transferred: 0.86 MB Response time: 1.94 secs Transaction rate: 177.07 trans/sec Throughput: 0.04 MB/sec Concurrency: 344.26 Successful transactions: 338 Failed transactions: 1619 Longest transaction: 14.83 Shortest transaction: 0.00 最高併發只有 344。。。
參數介紹: transactions:處理的請求數 Availability: 成功率 Elapsed Time:須要多少時間 Data transferred :傳輸了多少字節 Response time: 響應時間 Transaction rate:平均每秒完成多少次處理 (表示後臺的處理速度) Throughput:平均每秒傳輸速度 Concurrency:最高併發量 Successful transactions :成功的處理數 Failed transactions:失敗的處理數 Longest transactions:傳輸最長的時間 Shortest transactions:傳輸最短的時間