django(三)django配置支持nginx访问

1,202 阅读5分钟

我正在参加「掘金·启航计划」

VUE3.0版本发布,按照之前的计划,博客前端的页面打算使用VUE3.0重新编写。目前后端使用的是PHP的web框架thinkphp5.0,新版的博客后端打算使用python的web框架django。

目前我的服务器使用的环境是lnmp(centos+nginx+mariadb+php),因此运行django框架的时候,我选择使用nginx + uwsgi

我的服务器使用的是阿里云的centos7.8

运行原理如下图所示:

一:技术扩展WSGI

WSGI 是 Web Server Gateway Interface 的缩写。以层的角度来看,WSGI 所在层的位置低于 CGI。但与 CGI 不同的是 WSGI 具有很强的伸缩性且能运行于多线程或多进程的环境下,这是因为 WSGI 只是一份标准并没有定义如何去实现。实际上 WSGI 并非 CGI,因为其位于 web 应用程序与 web 服务器之间,而 web 服务器可以是 CGI。可以理解为是 Python 内置的一个测试 web 服务器。

uWSGI 是一个Web服务器,它实现了 WSGI 协议、uwsgi、http 等协议。Nginx 中HttpUwsgiModule 的作用是与 uWSGI 服务器进行交换。WSGI 是一种 Web 服务器网关接口。比如把 HTTP 协议转化成 WSGI 协议,让 Python 可以直接使用。

二、uWSGI 安装配置

安装

sudo pip3 install uwsgi

测试

创建 test.py 文件

#!/usr/bin/env python3
# -*- coding: UTF-8 -*-
 
def application(env, start_response):
    start_response('200 OK', [('Content-Type','text/html')])
return [b'Hello World']

通过 uWSGI 运行该文件

uwsgi --http :8000 --wsgi-file test.py # 这里需要注意确保你的8000端口是对外开放的

在浏览器中输入  你的服务器ip:8000,出现 “Hello World”表示安装成功。

三:Nginx、uWSGI、Django 之间的通信

安装Django框架请移步《django(一)ubuntu18.04安装框架

1 :配置 Django 和 uWSGI

我的项目 目录是:

/usr/local/download/django-master/helloworld

在当前目录下创建文件uwsgi.ini

[uwsgi]
#套接字方式--使用nginx连接时使用,后面是Django程序所在服务器地址
socket = :8000

#http通信方式--直接做web服务器使用,后面是Django程序所在服务器地址
# http=192.168.0.214:8000

#配置当前工作的项目目录
chdir = /usr/share/nginx/html/moolsPython

#项目中wsgi.py文件的目录,相对于项目目录
wsgi-file = moolsPython/wsgi.py

# 进程数
processes = 8

# 线程数
threads = 2

# 开启主进程模式--uwsgi服务器的角色   uwsgi做主进程,关闭该进程其他相关进程也会被杀掉
master = True

# 存放进程编号的文件
pidfile = uwsgi.pid

# 后台运行,并输出日志,后面是日志文件位置,后台运行时看不见终端所以只能查看日志
daemonize = uwsgi.log

module = moolsPython.wsgi

这个配置,其实就相当于在上一小节中通过 wsgi 命令,后面跟一堆参数的方式,给文件化了。

socket: 指定项目执行的端口号。

chdir: 指定项目的目录。

module: module = hello.wsgi 可以这么来理解。对于 uwsgi.ini 文件而言,与它同级目录有一个 helloworld目录,这个目录下有一个 wsgi.py 文件。

此时 Django 项目的目录文件结构如下:

helloworld/
├── manage.py
├── helloworld
│   ├── __init__.py
│   ├── __pycache__
│   │   ├── __init__.cpython-36.pyc
│   │   └── settings.cpython-36.pyc
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
└── uwsgi.ini

接下来,通过 uwsgi 命令读取 uwsgi.ini 文件启动项目(在项目根目录下运行)

uwsgi --ini uwsgi.ini

运行结果:

setup@labideas-data:~/myweb$ uwsgi --ini uwsgi.ini
[uWSGI] getting INI configuration from uwsgi.ini
*** Starting uWSGI 2.0.17 (64bit) on [Tue Mar 20 11:11:30 2018] ***
compiled with version5.4.0 20160609 on 19 March 2018 09:13:12
os: Linux-4.4.0-105-generic #128-Ubuntu SMP Thu Dec 14 12:42:11 UTC 2017
nodename: labideas-data
machine: x86_64
clock source: unix
detected number of CPU cores4
current working directory: /home/setup/hello
detected binary path: /usr/local/bin/uwsgi
!!! no internal routing support, rebuild with pcre support !!!
chdir() to /home/setup/hello
your processes number limit is 64049
your memory page size is 4096 bytes
detected max file descriptor number65535
lock engine: pthread robust mutexes
thunder lock: disabled (you can enable it with --thunder-lock)
uwsgi socket 0 bound to TCP address :8888 fd 3
Python version3.5.2 (default, Nov 23 201716:37:01)  [GCC 5.4.0 20160609]
*** Python threads support is disabled. You can enable it with --enable-threads ***
Python main interpreter initialized at 0x1f73aa0
your server socket listen backlog is limited to 100 connections
your mercy for graceful operations on workers is 60 seconds
mapped 364520 bytes (355 KB) for 4 cores
*** Operational MODE: preforking ***
WSGI app 0 (mountpoint='') ready in 0 seconds on interpreter 0x1f73aa0 pid7097 (default app)
*** uWSGI is running in multiple interpreter mode ***
spawned uWSGI master process (pid7097)
spawned uWSGI worker 1 (pid7099cores1)
spawned uWSGI worker 2 (pid7100cores1)
spawned uWSGI worker 3 (pid7101cores1)
spawned uWSGI worker 4 (pid7102cores1)

注意查看uwsgi的启动信息,如果有错,就要检查配置文件的参数是否设置有误。

2 : 配置 Nginx

我的nginx配置文件在/usr/local/nginx/conf目录下。

server {
    listen          80; #  指定的是 nginx 代理 uwsgi 对外的端口号。
    server_name     moolspy.mools.net  # 网站域名
    charset         UTF-8;
    # access_log      /var/log/nginx/myweb_access.log;
    # error_log       /var/log/nginx/myweb_error.log;
 
    client_max_body_size 75M;
 
    location / {
        include uwsgi_params; # 配置nginx支持uwsgi
        uwsgi_pass 127.0.0.1:8000; # 指的是本机 IP 和端口号,并且要与 helloworld目录下uwsgi.ini 配置文件中的 IP 和端口号必须保持一致。
        uwsgi_connect_timeout 60s; # 链接时间  最大不能超过75s
        uwsgi_read_timeout 120s; # nginx等待uwsgi进程发送响应数据的超时时间 默认 60s
        uwsgi_send_timeout 120s; # nginx向uwsgi进程发送请求的超时时间 默认 60s
    }  
}

重新启动nginx。

在django根目录下运行:

uwsgi --ini uwsgi.ini

在浏览器中访问你刚刚在nginx中配置的域名,出现如下图所示:说明配置成功:

33.png

3 :设置 uWSGI 开机自启

打开 sudo vi /etc/rc.local 将

uwsgi --ini /home/setup/myweb/uwsgi.ini &

添加到文件中

注意要添加到 exit 0 之前,& 表示该服务是在后台执行。

4 :更新代码没有效果

Django + uwsgi + nginx 这个组合在正常访问是没有问题的,但是在你更新完代码之后他并不会立即生效。你需要kill掉uwsgi所有的进程,再重新启动uwsgi才可以。

killall -9 uwsgi # 杀死uwsgi进程
uwsgi -i uwsgi.ini & # 重启uwsgi

有好的建议,请在下方输入你的评论。