日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

Python 应用 uWSGI + Nginx 部署

發布時間:2025/3/19 python 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Python 应用 uWSGI + Nginx 部署 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

新建一個 Django 應用部署

?使用 py 文件啟動 uWSGI

def application(env, start_response): start_response('200 ok', [('Content-type', 'text/html')]) return [b'Hello uWSGI.']

啟動:

uwsgi --http-socket :8000 --plugin python3 --wsgi-file uwsgi_test.py

可能需要安裝

sudo apt install uwsgi-plugin-commonsudo apt install uwsgi-plugin-python3

視情況也可能是apt install uwsgi-plugin-python,看自己使用的 Python 版本和軟鏈接情況。

這樣,在對應的 IP 下的 8000 端口可以訪問到文件。

?新建 Django 項目

django-admin startproject django_deployment

?通過修改配置允許外部訪問

setting.py:ALLOWED_HOSTS = ["*"]

?啟動

python3 manage.py runserver 0.0.0.0:8000

打通 Django 與 uWSGI 的鏈路

先停止應用

?啟動 uwsgi 打通關系

在項目根目錄啟動

uwsgi --http-socket :8000 --plugin python3 --module django_deployment.wsgi

?將命令行配置改為文件配置

新建django-uwsgi.ini:

touch django-uwsgi.inivim django-uwsgi.ini # 配置域[uwsgi]# 工作目錄chdir = /source/python/deployment/django_deployment# 模塊module = django_deployment.wsgi# 請求端口http-socket = :8000# mastermaster = True# 進程processes = 4# 線程threads = 1# 是否退出是清理相關內容vacuum = true

?啟動

uwsgi --ini django-uwsgi-ini

?后臺啟動

后臺運行程序并打印日志

# 配置域[uwsgi]# 工作目錄chdir = /source/python/deployment/django_deployment# 模塊module = django_deployment.wsgi# 請求端口http-socket = :8000# mastermaster = True# 進程processes = 4# 線程threads = 1# 是否退出是清理相關內容vacuum = true# backend run uwsgidaemonize = %(chdir)/log/uwsgi-8000.loglog-maxsize = 1024*1024*1024pidfile = %(chdir)/pid/uwsgi-8000.pid

創建文件夾 log 和 pid

?啟動

uwsgi --ini django-uwsgi.ini

?停止

uwsgi --stop pid/uwsgi-8000.pid

Django Nginx + uWSGI 部署

uWSGI 啟動 Django 服務器

?啟動并查看 pid

uwsgi --ini django-uwsgi-inicat pid/uwsgi-8000.pidps -aux | grep xxx

修改 Nginx 配置文件,完成反向代理

復制備份配置文件 nginx.conf 為 nginx.conf.back

修改 nginx.conf

在 63 行處:

upstream uwsgi { server 122.51.1.19:8000;}server { listen 80; server_name 122.51.1.19; charset utf-8; location / { proxy_pass http://uwsgi; }}

?啟動

nginx

查看啟動進程情況

ps -aux | grep nginx

?添加 log 配置文件

upstream uwsgi { server 122.51.1.19:8000;}server { listen 80; server_name 122.51.1.19; charset utf-8; access_log /var/log/nginx/nginx.log; location / { proxy_pass http://uwsgi; }}

?重啟

nginx -s reload

查看日志:

cd /var/log/ngxin/lstail -f nginx.log

收集靜態文件,完成靜態文件尋址配置

?收集靜態文件

vim django_deployment/settings.pySTATIC_ROOT = os.path.join(BASE_DIR, 'static')python manage.py collectstatic

?配置靜態文件路由

upstream uwsgi { server 122.51.1.19:8000;}server { listen 80; server_name 122.51.1.19; charset utf-8; access_log /var/log/nginx/nginx.log; location / { proxy_pass http://uwsgi; } location /static { allas /source/python/deployment/django_deployment/static; }}

重新加載

nginx -s reload

HTTPS 加密部署

使用 443 端口,協議加密傳輸報文。

?申請 SSL 證書

在 Nginx 中新建文件夾 ssl,存放 crt 和 key

?遠程拷貝到服務器

scp 2_topic.akashi.org.cn.crt root@122.51.1.19:/etc/nginx/ssl/scp 3_topic.akashi.org.cn.key root@122.51.1.19:/etc/nginx/ssl

?配置 Nginx 支持 HTTPS

nginx.conf:listen 443 ssl;ssl_certificate /etc/nginx/ssl/domain.com.crt;ssl_certificate_key /etc/nginx/ssl/domain.com.key;ssl_protocols TLSv1 TLSv1.1 TLSv1.2;定義 80 端口的返回server { listen 80; server_name topic.akashi.org.cn; rewrite ^(.*)$ https://$host$1 permanent;}

或者

server { listen 80; server_name topic.akashi.org.cn; return 301 https://topic.akashi.org.cn;$request_rui;}

?重啟生效

nginx -s reload

部署高可用服務

添加配置的啟動端口,開啟多個服務,并且轉發到 nginx 上,還可以通過權重分配達到負載均衡

upstream uwsgi { server 127.0.0.1:8000 weight=3; server 127.0.0.1:8001 weight=1;}

?注意事項

1.不要使用 root 權限啟動 uwsgi 服務2.關閉 uwsgi 外網訪問

http-scoket = 127.0.0.1:8000http-scoket = 127.0.0.1:8001 與50位技術專家面對面20年技術見證,附贈技術全景圖

總結

以上是生活随笔為你收集整理的Python 应用 uWSGI + Nginx 部署的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。