參考文章
參使用uWSGI和nginx來設定Django和你的web服務器
python3 + Django + uwsgi + nginx 配置部署筆記
不輕松,服務器部署nginx+uwsgi+djangorestfremework+react
http升級為https全程序(通過nginx安裝SSL證書)
Nginx操作 | Nginx配置SSL證書
如何用 uWSGI 托管 Django
我的環境
Ubuntu16.04、conda 4.7.10、python 3.6.7、Django 3.0.3
安裝uWSGI
pip install uwsgi
這里如果出現了問題,可以考慮如下解決辦法:參考鏈接
apt-get install python3-dev
apt-get install gcc-4.7 ##卸載你的gcc版本,安裝為4.7:
rm /usr/bin/gcc
ln -s /usr/bin/gcc-4.7 /usr/bin/gcc
最后重新運行安裝命令pip install uwsgi
測驗uWSGI是否正常作業
- 新建一個test.py檔案:
touch test.py若之后要洗掉則運行rm 檔案名
輸入如下內容:
def application(env, start_response):
start_response('200 OK', [('Content-Type','text/html')])
return [b"Hello World"] # python3
#return ["Hello World"] # python2
- 運行uWSGI:
uwsgi --http :8080 --wsgi-file test.py
可能會報錯:
uwsgi: error while loading shared libraries: libpcre.so.1: cannot open shared object file: No such file or directory
解決方法:參考鏈接
find / -name libpcre.so.* ##找到所有的系統中libpcre
ln -s /root/anaconda3/lib/libpcre.so.1 /lib ##創建libpcre.so.1軟鏈到/lib下
which uwsgi ##測驗一下是否好用了
這里有個坑,如果你的服務器是云服務器,例如我的是阿里云,一定要注意設定運行的埠要和云服務器控制臺的安全組埠對應,否則可能無法正確地用http訪問埠,

- 瀏覽器鍵入
http://example.com:8080出現Hello World說明運行成功,
表示以下路徑正常:the web client <-> uWSGI <-> Python
PS.Ubuntu殺死指定行程的命令:kill -9 $(lsof -i tcp:8080 -t)
從簡單的test.py到Django專案
- 新建一個Django專案并確保其能正確運行:如何新建參考這里
PS.云服務器上記得要把Django專案的settings.py中的ALLOWED_HOSTS = ['']改為ALLOWED_HOSTS = ['*'],方便其他地址正常訪問, - 改用uWSGI來運行:
uwsgi --http :8080 --module mysite.wsgi
如果能正常運行說明以下路徑正常:the web client <-> uWSGI <-> Django
通常我們不會讓瀏覽器直接與uWSGI通信,那是web服務器的作業,
安裝Nginx
sudo apt-get install nginx
sudo /etc/init.d/nginx start ##啟動nginx服務
通過瀏覽器訪問80埠,你應該會從Nginx獲得一個訊息:”Welcome to nginx!”,
這說明以下路徑正常:the web client <-> the web server
配置Nginx靜態路徑
- 在Django目錄下新建名為
uwsgi_params的檔案,檔案內容從這里復制 - 在Django目錄下新建名為
mysite_nginx.conf的檔案,內容為:
# mysite_nginx.conf
# the upstream component nginx needs to connect to
upstream django {
#server 0.0.0.0:8081; # for a web port socket (we'll use this first)
}
# configuration of the server
server {
# the port your site will be served on
listen 8080;
# the domain name it will serve for
server_name xx.xx.xx.xx # substitute your machine's IP address or FQDN
charset utf-8;
# max upload size
client_max_body_size 75M; # adjust to taste
# Django media
location /media {
alias /home/mysite/media; # your Django project's media files - amend as required
}
location /static {
alias /home/mysite/static; # your Django project's static files - amend as required
}
# Finally, send all non-media requests to the Django server.
location / {
uwsgi_pass django;
include /home/mysite/uwsgi_params; # the uwsgi_params file you installed,示使用uwsgi代理
}
}
- 將上述檔案鏈接到/etc/nginx/sites-enabled中以便Nginx識別
sudo ln -s ~/path/to/your/mysite/mysite_nginx.conf /etc/nginx/sites-enabled/ - 修改Django的settings.py檔案,添加下述陳述句:
STATIC_ROOT = os.path.join(BASE_DIR, "static/") - 運行如下命令:
python manage.py collectstatic這樣Django就會收集靜態檔案,放到指定目錄(static目錄)內 - 重啟Nginx
sudo /etc/init.d/nginx restart - 測驗
將測驗圖片test.jpg放入media檔案夾中,訪問xx.xx.xx.xx:8080/media/test.jpg如果能訪問,說明nginx提供了正確的檔案服務
配置Nginx動態請求
讓Nginx對test.py應用說句”hello world”吧,
uwsgi --socket :8081 --wsgi-file test.py
socket :8081:使用uWSGI協議,埠為8081,同時,已經配置了Nginx在那個埠與uWSGI通信,而對外使用8000埠,訪問:xx.xx.xx.xx:8080/
出現“hello world”說明以下路徑正常:the web client <-> the web server <-> the socket <-> uWSGI <-> Python
使用Unix socket而不是埠
目前我們使用了簡單的TCP socket,換成Unix socket所用的開銷更小,
- 編輯
mysite_nginx.conf檔案
將第一句的server 0.0.0.0:8081; #web socket改為server unix:///path/to/your/mysite/mysite.sock;這里的/path/to/your/.....改成你自己的路徑,mysite.sock是系統自動生成的,不用理會它, - 重啟Nginx:
sudo /etc/init.d/nginx restart - 再次運行uWSGI:
uwsgi --socket mysite.sock --wsgi-file test.py - 在瀏覽器訪問:
xx.xx.xx.xx:8000/
這里可能會出現502 Bad Gateway,原因是Nginx沒有進入該目錄的權限,故無法訪問socket檔案,其中一個解決方法是改變運行Nginx的用戶身份,把/etc/nginx/nginx.conf 第一行的user www-data; 中的www-data 改成權限足夠高的用戶,重啟Nginx,我直接改成root了,可以正常運行,
使用uWSGI和Nginx運行Django應用
- 輸入以下命令運行Django應用
uwsgi --socket mysite.sock --module mysite.wsgi --chmod-socket=664
通過組態檔啟動uWSGI
到這一步時,如果關閉云服務器的ssh遠程連接,網站又會出現502 Bad Gateway了,這是因為當你關閉這個ssh行程時,uWSGI行程也被終止了,所以我們需要ini組態檔啟動,
- 在專案目錄下新建
mysite_uwsgi.ini檔案,內容如下:
# mysite_uwsgi.ini file
[uwsgi]
# Django-related settings
# the base directory (full path)
chdir = /home/mysite
# Django's wsgi file
module = mysite.wsgi
# the virtualenv (full path)
virtualenv = /root/anaconda3/envs/chineseocr
python-autoreload=1
# process-related settings
# master
master = true
# maximum number of worker processes
processes = 10
# the socket (use the full path to be safe
socket = /home/mysite/mysite.sock
# ... with appropriate permissions - may be needed
# chmod-socket = 664
# clear environment on exit
vacuum = true
- 然后使用這個檔案運行uWSGI,再次測驗Django專案是否運行正常
uwsgi --ini mysite_uwsgi.ini # the --ini option is used to specify a file
Nginx靜態資源位置
訪問網址80埠默認指向/usr/share/nginx/html目錄下的index.html
在mysite_nginx.conf檔案中,我們在server{}下添加root /home/mysite/wwwroot/build;可以改變主頁的默認位置,
Nginx添加SSL證書
PS.這一節配置在我機器上有點問題,大家可以參考下其他人的文章,我用了下一節在其他埠配置的SSL可以正常使用,如果你只需要某些埠的使用,可以直接參考下一節,
- 首先在阿里云申請證書:http升級為https全程序(通過nginx安裝SSL證書)
- 檢查Nginx有沒有安裝SSL模塊:
進入Nginx安裝目錄,我的是/etc/,輸入nginx -V查看已經安裝的模塊,SSL的模塊名是--with-http_ssl_module - 在/etc/nginx/conf.d目錄里添加一個檔案,檔案名以.conf結尾,輸入下面兩個server:
server {
listen 443 ssl; #SSL協議訪問埠號為443,此處如未添加ssl,可能會造成Nginx無法啟動,
server_name localhost; #將localhost修改為您證書系結的域名,例如:www.example.com,
root html;
index index.html index.htm;
ssl_certificate cert/domain name.pem; #將domain name.pem替換成您證書的檔案名,
ssl_certificate_key cert/domain name.key; #將domain name.key替換成您證書的密鑰檔案名,
ssl_session_timeout 5m;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4; #使用此加密套件,
ssl_protocols TLSv1 TLSv1.1 TLSv1.2; #使用該協議進行配置,
ssl_prefer_server_ciphers on;
location / {
root html; #站點目錄,
index index.html index.htm;
}
}
server {
listen 80;
server_name localhost; #將localhost修改為您證書系結的域名,例如:www.example.com,
rewrite ^(.*)$ https://$host$1 permanent; #將所有http請求通過rewrite重定向到https,
location / {
index index.html index.htm;
}
}
- 重新啟動服務器
sudo /etc/init.d/nginx restart - 一些輔助命令
檢查.conf檔案有無語法錯誤:nginx -t -c /etc/nginx/nginx.conf
查看服務器無法啟動的原因:systemctl status nginx.service - 注意要在云服務器安全組(防火墻)開放HTTPS的443埠,不然無法訪問
給其他埠開啟SSL服務
- 在/etc/nginx/site-enabled下新建
test.conf檔案
假設該埠可以處理靜態資源請求,動態請求轉接給Django,檔案內容如下:
server {
listen 8083 ssl;
server_name www.xxx.com;
charset utf-8;
ssl_certificate /etc/nginx/cert/xxx.pem;
ssl_certificate_key /etc/nginx/cert/xxx.key;
ssl_session_timeout 5m;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4; #阿里云證書,使用此加密套件,
ssl_protocols TLSv1 TLSv1.1 TLSv1.2; #使用該協議進行配置,
ssl_prefer_server_ciphers on;
# max upload size
client_max_body_size 75M; # adjust to taste
# Django media
location /media {
alias /home/mysite/media; # your Django project's media files - amend as required
}
location /static {
alias /home/mysite/static; # your Django project's static files - amend as required
}
# Finally, send all non-media requests to the Django server.
location / {
uwsgi_pass django;
include /home/mysite/uwsgi_params; # the uwsgi_params file you installed
}
}
- 輸入
https://www.xxx.com:8083即可正常訪問
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/184544.html
標籤:Python
上一篇:Tornado
下一篇:python 入門基礎知識點總結
