uWSGI + Nginx + Django 部署
來源:https://www.cnblogs.com/midworld/p/10992005.html
1. uWSGI 服務(wù)器
Django 默認(rèn)使用 WSGI(Python Web Server Gateway )
作為 Web 服務(wù)器,一般僅用來作為測(cè)試使用,實(shí)際生產(chǎn)環(huán)境而是使用 uWSGI 和 Nginx 作為服務(wù)器。
uWSGI 代碼完全用C編寫,效率高、性能穩(wěn)定,但是處理?靜態(tài)文件能力較弱,因此實(shí)際生產(chǎn)環(huán)境中,一般采用?uWSGI + Nginx?兩者搭配使用:
- uWSGI:處理動(dòng)態(tài)請(qǐng)求(css、js、圖片文件等)
- Nginx:處理靜態(tài)文件請(qǐng)求(提交表單、mysql、動(dòng)態(tài)渲染 html)
安裝:
pip3 install uWSGIsettings.py 配置
設(shè)置?ALLOWED_HOSTS:
ALLOWED_HOSTS = [# 加上本機(jī)的IP地址'192.168.xx.xxx','127.0.0.1', 'localhost' ]也可以這樣設(shè)置:
ALLOWED_HOSTS = ['*']1.1 通過參數(shù)啟動(dòng) uWSGI
# 請(qǐng)更換成你服務(wù)器 ip 和端口,其中 Celery_Test/wsgi.py 為Django 項(xiàng)目中自帶的 wsgi web 服務(wù) hj@hj:~/桌面/Celery_Test$ uwsgi --http 192.168.21.128:8080 --file Celery_Test/wsgi.py --static-map=/static=static出現(xiàn)如下效果便是運(yùn)行成功了:
?
查看啟動(dòng)端口:
hj@hj:~/桌面/Celery_Test/script$ ps -ef|grep uwsgihj 17176 5231 0 15:37 pts/1 00:00:00 uwsgi --http 192.168.xx.128:8080 --file Celery_Test/wsgi.py --static-map=/static=static hj 17177 17176 0 15:37 pts/1 00:00:00 uwsgi --http 192.168.xx.128:8080 --file Celery_Test/wsgi.py --static-map=/static=static hj 17206 6554 0 15:38 pts/2 00:00:00 grep --color=auto uwsgi訪問?192.168.21.128:8080?:
?
1.2 通過配置文件啟動(dòng) uWSGI
在項(xiàng)目根目錄創(chuàng)建一個(gè)名為?uwsgi.ini?的配置文件,配置如下:
[uwsgi] # 項(xiàng)目目錄 chdir=/home/hj/桌面/Celery_Test/ # 指定項(xiàng)目的application module=Celery_Test.wsgi:application # 指定sock的文件路徑 socket=/home/hj/桌面/Celery_Test/script/uwsgi.sock # 進(jìn)程個(gè)數(shù) workers=5 pidfile=/home/hj/桌面/Celery_Test/script/uwsgi.pid # 指定IP端口 http=192.168.21.128:8080 # 指定靜態(tài)文件 static-map=/static=/home/hj/桌面/Celery_Test/static # 啟動(dòng)uwsgi的用戶名和用戶組 uid=root gid=root # 啟用主進(jìn)程 master=true # 自動(dòng)移除unix Socket和pid文件當(dāng)服務(wù)停止的時(shí)候 vacuum=true # 序列化接受的內(nèi)容,如果可能的話 thunder-lock=true # 啟用線程 enable-threads=true # 設(shè)置自中斷時(shí)間 harakiri=30 # 設(shè)置緩沖 post-buffering=4096 # 設(shè)置日志目錄 daemonize=/home/hj/桌面/Celery_Test/script/uwsgi.log配置好之后,運(yùn)行?uwsgi --ini uwsgi.ini?啟動(dòng) uwsgi,出現(xiàn)如下信息即表示啟動(dòng)成功:
hj@hj:~/桌面/Celery_Test$ uwsgi --ini uwsgi.ini [uWSGI] getting INI configuration from uwsgi.ini [uwsgi-static] added mapping for /static => /home/hj/桌面/Celery_Test/static查看運(yùn)行情況:
ps ajx|grep uwsgi效果如下圖:
?
常用命令:
uwsgi --ini uwsgi.ini # 啟動(dòng)# 啟動(dòng)時(shí)會(huì)生成兩個(gè)文件,分別為:# PID文件 標(biāo)識(shí)這個(gè)程序所處的狀態(tài)# SOCK文件 用來和其他程序通信的 uwsgi --stop uwsgi.pid # 停止 uwsgi --reload uwsgi.ini # 重置Tips
停止時(shí)出現(xiàn)?signal_pidfile()/kill(): No such process [core/uwsgi.c line 1693]
原因:當(dāng)前端口進(jìn)程與?uwsgi.pid?不一致,查看當(dāng)前端口實(shí)際進(jìn)程 ID,并修改?uwsgi.pid:
# 根據(jù)端口,查看進(jìn)程 hj@hj:~/桌面/Celery_Test$ sudo netstat -nap | grep 8080 tcp 0 0 192.168.21.128:8080 0.0.0.0:* LISTEN 6943/uwsgi # 修改 uwsgi.pid 的值為 6943,并再重新停止 hj@hj:~/桌面/Celery_Test$ uwsgi --stop script/uwsgi.pid# 查看發(fā)現(xiàn)已經(jīng)成功停止 hj@hj:~/桌面/Celery_Test$ ps ajx|grep uwsgi5231 14550 14549 5231 pts/1 14549 S+ 1000 0:00 grep --color=auto uwsgiLinux 中怎么查看端口和進(jìn)程號(hào)
# 加上 sudo # 根據(jù)進(jìn)程 pid 查看端口 lsof -i | grep pid# 根據(jù)端口查看進(jìn)程 lsof -i:port# 根據(jù)進(jìn)程 pid 查看端口 netstat -nap | grep pid# 根據(jù)端口查看進(jìn)程號(hào) netstat -nap | grep port2. Nginx 服務(wù)器
我們知道 uWSGI 處理靜態(tài)文件請(qǐng)求能力比較弱,因此一般實(shí)際生產(chǎn)環(huán)境中以?動(dòng)靜分離?的方式處理動(dòng)靜請(qǐng)求,即 uWSGI + Nginx。
Nginx 作用還包括負(fù)載均衡、反向代理等。
2.1 Ubuntu 上安裝 Nginx
Nginx 的軟件包在 Ubuntu 默認(rèn)軟件倉庫中可用。 安裝非常簡(jiǎn)單,只需鍵入以下命令:
sudo apt update udo apt install nginx查看服務(wù)器版本信息:
sudo nginx -vnginx version: nginx/1.14.0 (Ubuntu)查看服務(wù)器狀態(tài):
# 兩個(gè)都可以 sudo systemctl status nginx ps -ef | grep nignx?
配置防火墻
打開 80 和 443 端口允許通過防火墻:
hj@hj:~$ sudo ufw allow 'Nginx Full' 防火墻規(guī)則已更新 規(guī)則已更新(v6)檢查是否更改:
hj@hj:~$ sudo ufw status 狀態(tài): 激活至 動(dòng)作 來自 - -- -- 22 ALLOW Anywhere 4200 ALLOW Anywhere Nginx Full ALLOW Anywhere 22 (v6) ALLOW Anywhere (v6) 4200 (v6) ALLOW Anywhere (v6) Nginx Full (v6) ALLOW Anywhere (v6)測(cè)試安裝
訪問:http://192.168.21.128/:
?
使用 systemctl 管理 Nginx 服務(wù)
您可以像任何其他 systemd 單位一樣管理 Nginx 服務(wù):
# 停止Nginx服務(wù) sudo systemctl stop nginx# 再次啟動(dòng) sudo systemctl start nginx# 重新啟動(dòng)Nginx服務(wù): sudo systemctl restart nginx# 在進(jìn)行一些配置更改后重新加載 Nginx 服務(wù): $sudo systemctl reload nginx# 如果你想禁用Nginx服務(wù)在啟動(dòng)時(shí)啟動(dòng): $sudo systemctl disable nginx# 并重新啟用它: $sudo systemctl enable nginx參考鏈接:如何在Ubuntu 18.04上安裝Nginx
2.2 CentOS 上安裝
以CentOS6.x 系統(tǒng)為例
2.3 與 uWSGI 結(jié)合使用部署 Django
為 Nginx 添加配置文件,Ngnix 默認(rèn)配置文件加載是在?/etc/nginx/conf.d/?目錄下,新建一個(gè)配置文件(名字隨意),編輯如下:
# 新建到配置文件 conf.d vim /etc/nginx/conf.d/# 編輯配置文件 server { # 開始配置了listen 80; # 監(jiān)聽端口server_name 10.129.xxx.183 ; # 你訪問的路徑前面的 url名稱access_log /var/log/nginx/access.log main; # Nginx日志配置charset utf-8; # Nginx編碼gzip on; # 啟用壓縮,這個(gè)的作用就是給用戶一個(gè)網(wǎng)頁,比如3M壓縮后1M這樣傳輸速度就會(huì)提高很多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; # 錯(cuò)誤頁面error_page 500 502 503 504 /50x.html; # 錯(cuò)誤頁面# 指定項(xiàng)目路徑 uwsgilocation / { # 類似于 Django的 url(r'^admin/', admin.site.urls),include uwsgi_params; # 導(dǎo)入一個(gè)Nginx模塊他是用來和uWSGI進(jìn)行通訊的uwsgi_connect_timeout 30; # 設(shè)置連接uWSGI超時(shí)時(shí)間uwsgi_pass unix:/opt/project_teacher/script/uwsgi.sock; # 指定uwsgi的sock文件所有動(dòng)態(tài)請(qǐng)求就會(huì)直接丟給他}# 指定靜態(tài)文件路徑location /static/ {alias /opt/project_teacher/teacher/static/;index index.html index.htm;}}參考:
server { listen 80; server_name 192.168.xx.128 ; access_log /var/log/nginx/access.log main; charset utf-8; gzip on; 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; location / { include uwsgi_params; uwsgi_connect_timeout 30; uwsgi_pass unix:/home/hj/桌面/Celery_Test/script/uwsgi.sock; }location /static/ {alias /home/hj/桌面/Celery_Test/static/;index index.html index.htm啟動(dòng) Nginx 服務(wù)?/etc/init.d/nginx start,訪問:http://192.168.21.128:8080/app/index/,效果如下圖:
?
常用命令:
/etc/init.d/nginx start # 啟動(dòng) /etc/init.d/nginx stop # 關(guān)閉# Nginx配置是重啟才生效,若修改配置不知道是否對(duì)不對(duì),可以用來測(cè)試 /etc/init.d/nginx configtest# 生產(chǎn)環(huán)境直接 reload就行了,不要 stop start 或者 restart /etc/init.d/nginx reload配置 Django 靜態(tài)文件
admin 所需的靜態(tài)文件都在 Django 的安裝內(nèi),我們沒有配置指向 Django 的配置文件。
解決辦法:
指定其他地方放置靜態(tài)文件
# 新建目錄 sudo mkdir -vp /var/www/test2/static/# 賦予權(quán)限 sudo chmod 777 /var/www/test2/static/# 修改項(xiàng)目中 settings.py,指定靜態(tài)文件路徑 STATIC_ROOT = '/var/www/test2/static/' STATIC_URL = '/static/'# 收集靜態(tài)文件到 /var/www/test2/static/ 中 python3 manage.py collectstatic# 輸入 yes,開始收集,重新加載 Nginx 服務(wù)總結(jié)
以上是生活随笔為你收集整理的uWSGI + Nginx + Django 部署的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 赵洪山前面的领导(赵洪山)
- 下一篇: 卡丁车是什么?