likes
comments
collection
share

Nginx+uWSGI部署Django项目

作者站长头像
站长
· 阅读数 13

部署Django

Django作为一个 Web 框架,需要一个 Web 服务器才能运行。由于大多数 Web 服务器不会以 Python 为母语,因此需要一个接口来实现这种通信。

Django目前支持两种接口:WSGI和ASGI。

WSGI是 Web 服务器和应用程序之间通信的主要 Python 标准,但它只支持同步代码

ASGI是一种新的、异步友好的标准,它将允许您的 Django 站点使用异步 Python 功能和开发的异步 Django 功能

使用 WSGI 进行部署

Django 的主要部署平台是WSGI,它是 Web 服务器和应用程序的 Python 标准。

将本地开发的项目打包

导出依赖模块列表

pip3 freeze > requirements.txt

关闭debug模式

DEBUG = False   # 调试模式

关闭白名单

ALLOWED_HOSTS = ['*' ]  # 白名单,只允许列表中的ip访问,*代表所有

将项目打包

服务器环境准备

安装Python3

yum install zlib-devel libffi-devel mysql-devel -y
tar zxvf Python-3.8.6.tgz
cd Python-3.8.6
./configure --enable-optimizations 
make && make install

安装依赖模块列表

pip3 install -r requirements.txt -i http://mirrors.aliyun.com/pypi/simple --trusted-host mirrors.aliyun.com
(env) [root@administrator django_project]# python3 manage.py runserver 0.0.0.0:8083
Performing system checks...

System check identified no issues (0 silenced).

You have 19 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions, user.
Run 'python manage.py migrate' to apply them.
May 10, 2023 - 02:56:22
Django version 3.2.19, using settings 'django_project.settings'
Starting development server at http://0.0.0.0:8083/
Quit the server with CONTROL-C.
[10/May/2023 02:56:39] "GET /user/test/ HTTP/1.1" 200 13
[10/May/2023 02:56:48] "GET /user/test/ HTTP/1.1" 200 13

安装与配置uwsgi

uwsgi文档:https://uwsgi-docs-zh.readthedocs.io/zh_CN/latest/index.html

安装uWSGI

uWSGI是一个Web服务器,也是Python的一个模块,可以直接使用pip命令安装:

pip3 install uwsgi

pip3 install uwsgi -i http://mirrors.aliyun.com/pypi/simple --trusted-host mirrors.aliyun.com

查看uwsgi版本

uwsgi --version

uWSGI测试

新建 test.py 文件


def application(env, start_response):
    start_response('200 OK', [('Content-Type','text/html')])
    return [b"Hello World"] # python3     
    #return ["Hello World"] # python2
uwsgi --http :8001 --wsgi-file test.py

在浏览器内输入:http://127.0.0.1:8001 Nginx+uWSGI部署Django项目

配置uwsgi

创建一个uwsgi配置文件,路径任意,文件名也可以是任何名称,但通常被称为uwsgi.ini

项目主要结构如下:

django_project
├── django_project
│   └── __pycache__
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
├── templates
└── user
    ├── migrations
    │   └── __pycache__
    └── __pycache__

在uwsgi配置文件中,指定应用程序的位置和uwsgi服务器的运行参数

[uwsgi]
# 项目目录
chdir = /root/workspace/django_project

# 指定sock的文件路径
socket =/root/workspace/django_project/uwsgi.sock

# 指定监听端口
http = 0.0.0.0:8083

# 静态资源
static-map =/static=/root/workspace/django_project/static

# 项目中wsgi.py文件的目录,相对于uwsgi.ini文件所在目录
wsgi-file=django_project/wsgi.py

# 进程个数
processes = 4 

# 指定项目的应用
module = django_project.user

# 进程pid
pidfile =/root/workspace/django_project/uwsgi.pid

# 退出时清理环境
vacuum = true

# 日志路径
daemonize = /root/workspace/django_project/uwsgi.log

其他参数配置

# HTTP超时时间(以秒为单位)
http-timeout = 86400

# 启用HTTP保持活动连接
http-keepalive = true

# 套接字超时时间(以秒为单位)
socket-timeout = 300

# 启动的进程数
processes = 4

# 每个进程中的线程数
threads = 2

# 绑定到的HTTP端口
http = :8000

# WSGI应用程序的入口点
module = app:application

# 启用主进程(默认值为true)
master = true

# 退出时清理环境
vacuum = true

# 当接收到TERM信号时立即关闭uWSGI
die-on-term = true

# 默认的uwsgi分配一个小的buffer(4k)来接收每个请求的头信息
# 如果在日志中看见"invalid request block size",它意味着你需要一个大一点的buffer
buffer-size  = 8192 

启动uwsgi服务器

这将根据配置文件中指定的参数启动uwsgi服务器

uwsgi --ini uwsgi.ini
(env) [root@administrator django_project]# uwsgi --ini uwsgi.ini
[uWSGI] getting INI configuration from uwsgi.ini
[uwsgi-static] added mapping for /static => /root/workspace/django_project/static

测试

访问uwsgi服务测试:http://IP:8083/user/tets Nginx+uWSGI部署Django项目 访问静态资源:http://IP:8083/static/1.jpg Nginx+uWSGI部署Django项目

停止uwsgi服务器

1.使用uwsgi自带的命令行选项

通过命令行启动uwsgi服务器的,可以使用以下命令来停止uwsgi服务器

uwsgi --stop /tmp/uwsgi.pid

注意:这里的/tmp/uwsgi.pid应该替换为实际使用的pid文件的路径

2.使用系统级别的命令

获取uwsgi进程ID

列出所有正在运行的uwsgi进程及其进程ID

ps aux | grep uwsgi

使用系统级别的命令来停止uwsgi服务器

这将向uwsgi进程发送SIGINT信号,强制其停止运行

kill -s SIGINT <uwsgi进程id>

Nginx 配置

将Web服务器与uwsgi服务器配合使用,通常使用Nginx

include /www/server/panel/vhost/nginx/*.conf;

修改uwsgi.ini配置文件,这里掉坑去了

# 直接做web服务器使用,指定监听端口
# http = 0.0.0.0:8083

# # 使用nginx链接时使用
socket = 127.0.0.1:8083

nginx配置转发请求给 uwsgi

server {
        listen 8085;
        server_name  uwsgi;

        location / {
           include     uwsgi_params;  # 导入模块用于与uwsgi通信
           uwsgi_pass 127.0.0.1:8083; 
        }
        
        # 静态文件目录
        location /static {
           alias /root/workspace/django_project/static;
        }
    }

Nginx+uWSGI部署Django项目