程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Under the Ubuntu Nginx Uwsgi - Django project deployment process in detail

編輯:Python

Ubuntu下的Nginx+Uwsgi+DjangoProject deployment detailed process

版本

  • Ubuntu 18.04
  • Python 3.6
  • Django 3.2
  • Nginx 1.14.0

Python 虛擬環境

  • virtualenvIs the virtual environment installation package,Creating a virtual environment provides better control over package versions,The stability of the project is guaranteed
  • The operating environments between different virtual environments are independent of each other,互不干擾

1.安裝virtualenv

pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple virtualenv

2.創建虛擬環境

virtualenv myVenv

3.進入虛擬環境

source myVenv/bin/activate

Django項目代碼上傳

1.通過pycharm進行代碼部署

  • 選擇:工具-Deployment-Configuration
  • 點擊 + 號,選擇SFTP連接
  • select remote host
  • Select Local Directory and Upload Directory
  • After selecting the file, upload it

2.項目試運行

python3 manage.py runserver 0.0.0.0:8000

uWSGI 安裝配置

1.Python 安裝 uWSGI

在虛擬環境下安裝uWSGI

pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple uwsgi

2.測試uWSGI

從一個簡單的 “Hello World” 開始,創建文件 test_uwsgi.py,代碼如下:

def application(env, start_response):
start_response('200 OK', [('Content-Type','text/html')])
return [b"Hello World"]

uWSGI Python 加載器將會搜索的默認函數 application .
接下來我們啟動 uWSGI 來運行一個 HTTP 服務器,將程序部署在HTTP端口 8001 上:

uwsgi --http :8001 --wsgi-file test_uwsgi.py

uWSGI部署Django

在項目根目錄下(與manage.py同級)新建一個uwsgi.ini文件,內容如下:

[uwsgi]
socket = 0.0.0.0:8001
chdir = /data/myobject/
wsgi-file = myobject/wsgi.py
processes = 4
threads = 2
buffer-size = 65536
# .sock文件目錄需與Nginx文件內的配置相同
# socket = /data/myobject/my_sock.sock
# chmod-socket = 666
# 存儲pid進程
pidfile=uwsgi.pid
# 存儲log日志
daemonize=uwsgi.log

啟動uWSGI,並指定配置文件

uwsgi --ini uwsgi.ini

  • 可以看到,文字正常顯示,圖片無法顯示,這是正常現象
  • The above steps show that the following three links are the same

    web client <-> uWSGI <-> Django

Nginx

1.安裝Nginx

sudo apt-get install nginx

2.Nginx命令

/usr/sbin/nginx # 啟動Nginx
/usr/sbin/nginx -s stop # 停止Nginx
/usr/sbin/nginx -s reload # 重新啟動Nginx
vi /etc/nginx/nginx.conf # 編輯Nginx配置文件

3.測試Nginx

啟動Nginx後,浏覽器訪問ip地址:

看到如上頁面,說明Nginx啟動成功.

4.配置Nginx

將/etc/nginx/目錄下的uwsgi_params復制到項目文件夾,No changes are made to this file

cp /etc/nginx/uwsgi_params /data/myobject

在項目根目錄創建文件my_nginx.conf,並寫入以下內容

upstream django {
server 0.0.0.0:8001;
# server unix:///data/myobject/my_sock.sock;
}
server {
listen 8000; # 端口號
server_name 127.0.0.1; # 服務器 ip 或是域名
charset utf-8; # 字符集
# 最大上傳限制
client_max_body_size 75M;
location /static {
alias /data/myobject/static; # 靜態文件所在文件夾
}
# Forward all non-media requests toDjango服務器上
location / {
uwsgi_pass django; # Defined at the top
# Turn all parameters to uwsgi下
include /data/myobject/uwsgi_params; # uwsgi_params的路徑
}
}

This configuration file indicates that static files and media files will be served by Nginx處理,And other requests go inuWSGI處理
與NginxThe configuration directory establishes a soft link

sudo ln -s /data/myobject/my_nginx.conf /etc/nginx/sites-enabled/

Nginx & uWSGI & Django

1.測試Nginx

Put a test picturetest.jpg放入static文件夾中,在浏覽器中輸入<YOUR_SERVER_IP>:8000/static/test.jpg,如果出現403Change the permissions of the image to 666,成功顯示圖片.

2.測試uWSGI

回到項目根目錄,輸入以下命令

uwsgi --socket :8001 --wsgi-file test_uwsgi.py

打開浏覽器,地址欄輸入<YOUR_SERVBER_IP>,看是否能正常顯示’Hello World’.

3.用UNIX socket取代TCP port

修改my_nginx.conf,最終版如下:

upstream django {
# server 0.0.0.0:8001;
server unix:///data/myobject/my_sock.sock;
}
server {
listen 8000; # 端口號
server_name 127.0.0.1; # 服務器 ip 或是域名
charset utf-8; # 字符集
# 最大上傳限制
client_max_body_size 75M;
location /static {
alias /data/myobject/static; # 靜態文件所在文件夾
}
# Forward all non-media requests toDjango服務器上
location / {
uwsgi_pass django; # Defined at the top
# Turn all parameters to uwsgi下
include /data/myobject/uwsgi_params; # uwsgi_params的路徑
}
}

修改uwsgi.ini,最終版如下:

# socket = 0.0.0.0:8001
chdir = /data/myobject/
wsgi-file = myobject/wsgi.py
processes = 4
threads = 2
buffer-size = 65536
# .sock文件目錄需與Nginx文件內的配置相同
socket = /data/myobject/my_sock.sock
chmod-socket = 666
# 存儲pid進程
pidfile=uwsgi.pid
# 存儲log日志
daemonize=uwsgi.log

重啟Nginx和uWSGI

/usr/sbin/nginx -s reload
uwsgi --stop uwsgi.pid
uwsgi --ini uwsgi.ini

打開浏覽器,地址欄輸入網址<YOUR_SERVER_IP>:8000,Check if the pictures and text are displayed properly
At this point, the following links have all been opened

web client <-> web server(nginx) <-> the socket <-> uwsgi <-> Django


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved