Python 应用 uWSGI + Nginx 部署
生活随笔
收集整理的這篇文章主要介紹了
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.pidDjango 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 reloadHTTPS 加密部署
使用 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 部署的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: IT项目管理需要注意的细节
- 下一篇: Mysql悲观锁乐观锁区别使用区别