Nginx+uWSGI+Django环境配置

时间:2021-02-04 05:59:32

通常项目会部署在虚拟环境,虚拟环境的使用可以参考这里,点击前往

当然你也可以直接部署,这里不多说。

一、安装uWSGI

1.通过pip安装

pip install uwsgi

这里只说明了一种安装方式,其他安装方式可以参考官网,点击跳转

二、第一个WSGI程序

# 创建一个名为test.py 的文件

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

# 在HTTP端口9090上进行部署

uwsgi --http:8000 --wsgi-file test.py

选项说明:

  • --http : 使用协议http,端口9090
  • --wsgi-file test.py : 加载指定的文件test.py

现在再去访问,我这里的地址为http://192.168.10.165:8000/就可以看到Hello World 信息

如果是这样,工作流程为      网络客户端 <--> uWSGI <--> Python

三、添加并发和查看uwsgi状态

每一个处于监听(Listen)状态的端口,都有自己的监听队列.监听队列的长度,与如下两方面有关:

somaxconn参数和使用该端口的程序中listen()函数

可以通过echo >/proc/sys/net/core/somaxconn 命令来修改系统的监听队列长度。并且在/etc/sysctl.conf文件中添加net.core.somaxconn = 1024 并sysctl -p生效。

通过 添加--listen num参数增加队列长度。

默认情况下,uWSGI以单进场和单线程开始的,你可以使用--processes添加更多的进程,--threads添加更多的线程

uwsgi --http :9090 --wsgi-file test.py --master --processes 4 --threads 2

查看uwsgi状态:

统计子系统允许将uWSGI的内部统计信息导出为JSON

uwsgi --http :9090 --wsgi-file test.py --master --processes 4 --threads 2 --stats 127.0.0.1:9191

请求您的应用程序,然后Telnet到端口9191,你会得到很多关系uwsgi的相关信息。

使用“uwsgitop”工具可以来监控实例

  需要使用pip install uwsgitop 来安装uwsgitop

注意:将统计套接字绑定到私人地址(除非你知道你在做什么),否则每个人都可以访问它!

uwsgi的使用还有其他很多的选项,查看更多点击这里

四、uwsgi + Django配置

现在我们希望uWSGI做同样的事情,但是运行Django站点而不是test.py 模块。

首先我们来创建一个django 站点,并启动测试该站点是否可以正常访问:

source bin/activate        # 进去python虚拟环境
pip install django # 安装django
django-admin startproject mysite # 新建站点
cd mysite/
vim mysite/settings.py # 配置ALLOWED_HOSTS为‘["*"]’,方便其他主机访问站点,也可以不修改
python manage.py runserver 0.0.0.0:8000 # 启动并测试可用性

在测试站点可以正常运行后,关闭站点,下面来使用uwsgi来运行django项目:

uwsgi --http :8000 --module mysite.wsgi
  • --module mysite.wsgi : 加载指定的wsgi模块
  • --http : add an http router/server on the specified address
  • --socket: bind to the specified UNIX/TCP socket using default protocol

通过浏览器访问页面,如果页面出现,意味着uWSGI能够从你的virtualenv服务于你的django项目,并且正常运行,此时的工作流程为:

网络客户端 <--> uWSGI <--> Django

注意:

  此时如果你的django需要访问静态文件,是访问不到的,需要利用uWSGI进行静态文件的配置,这里不作说明,因为一般也不需要uWSGI来处理站点的静态文件,

  一般使用网络服务器来处理这些静态文件会更好,更高效。

通常不会让浏览器直接与uWSIG会话,这个是网络服务器的工作(这里为Nginx),uWSGI将作为一个中间件。

五、Nginx环境和静态文件配置

1.首先搭建Nginx环境

yum install nginx
/etc/init.d/nginx start # start nginx

nginx的配置问题这里不过多说明,避免与默认配置冲突,我选择新建vhost文件夹,在vhost下添加配置,然后导入到主配置文件中,这里使用8000端口。

新建一个Nginx的配置(mysite_nginx.conf)放入vhost中:(记得在Nginx主配置文件中导入该配置)

# mysite_nginx.conf

# the upstream component nginx needs to connect to
upstream django {
# server unix:///path/to/your/mysite/mysite.sock; # for a file socket
server 127.0.0.1:8001; # for a web port socket (we'll use this first)
} # configuration of the server
server {
# the port your site will be served on
listen 8000;
# the domain name it will serve for
server_name .example.com; # substitute your machine's IP address or FQDN
charset utf-8; # max upload size
client_max_body_size 75M; # adjust to taste # Django media
location /media {
alias /path/to/your/mysite/media; # your Django project's media files - amend as required
} location /static {
alias /path/to/your/mysite/static; # your Django project's static files - amend as required
} # Finally, send all non-media requests to the Django server.
location / {
uwsgi_pass django;
include /path/to/your/mysite/uwsgi_params; # the uwsgi_params file you installed
}
}
# 这里的配置需要进行修改,路径为你项目路径

这个conf文件告诉nginx从文件系统提供媒体和静态文件,以及处理需要Django干预的请求。对于大型部署来说,让一台服务器处理静态/媒体文件以及另一个处理Django应用程序的

做法被认为是很好的做法,但是现在这样做会很好。

为了方便对文件的查看和修改,建议把配置文件建立一个软连接到项目目录下。

sudo ln -s  /etc/nginx/sites-enabled/mysite_nginx.conf /path/to/your/mysite/

2.部署静态文件

在运行Nginx之前,必须在静态文件夹中收集所有的django静态文件,需要编辑mysite/settings.py 添加:

STATIC_ROOT = os.path.join(BASE_DIR, "static/")
# and then run
python manage.py collectstatic

这里需要注意一点,配置的静态文件夹和django的静态文件夹不能为一个。

3.检查静态文件配置

在启动Nginx是会报错,那是因为配置文件在的  include /path/to/your/mysite/uwsgi_params;

需要导入这个文件, 这个文件一般在Nginx uwsgi发行版的目录中,或从这里获取https://github.com/nginx/nginx/blob/master/conf/uwsgi_params

启动后检查是否可以获取静态文件。

六、Nginx + uWSGI + test.py

1.使用uWSGI启动一个socket ,使nginx可以转发到uWSGI

uwsgi --socket:8001 --wsgi-file test.py
  • socket:8001 : 使用协议uwsgi,端口8001

nginx同时被配置为与该WSGI在该端口上通信,并与外界在8000端口进行通信

这是当前的堆栈:

web客户端 <--> web服务器 <--> 套接字 <--> uWSGI <--> pyhton(test.py)

2.使用Unix套接字实现

编辑mysite_nginx.conf,使用套接字来实现:

server unix:///path/to/your/mysite/mysite.sock; # for a file socket
# server 127.0.0.1:8001; # for a web port socket (we'll use this first)

这个socket选项告诉uWSGI使用哪个文件进行套接字连接。

如果不行:

  检查Nginx错误日志 nginx/error.log 如果你看到

connect() to unix:///path/to/your/mysite/mysite.sock failed (13: Permissiondenied)

那么可能你需要管理套接字的权限,以便运行nginx使用它。

uwsgi --socket mysite.sock --wsgi-file test.py --chmod-socket=666 # (verypermissive)
uwsgi --socket mysite.sock --wsgi-file test.py --chmod-socket=664 # (moresensible)

你可能还需要将用户添加到nginx的组里,以便nginx可以正确的读取和写入套接字

七、Nginx + uWSGI + Django

来运行我们的Django应用程序:

uwsgi --socket mysite.sock --module mysite.wsgi --chmod-socket=664  # 或者是666

现在uWSGI和nginx不仅仅是一个“Hello World ” ,而是个Django项目。

通过ini配置文件运行uWSGI

uwsgi支持通过各种的配置文件来启动服务,这里说一个ini配置文件

创建一个名为mysite_uwsgi.ini:

# mysite_uwsgi.ini file
[uwsgi] # Django-related settings
# the base directory (full path)
chdir = /path/to/your/project
# Django's wsgi file
module = project.wsgi
# the virtualenv (full path)
home = /path/to/virtualenv # process-related settings
# master
master = true
# maximum number of worker processes
processes = 10
# the socket (use the full path to be safe
socket = /path/to/your/project/mysite.sock
# ... with appropriate permissions - may be needed
# chmod-socket = 664 # 或者666
# clear environment on exit
vacuum = true

并运行uwsgi:

uwsgi --ini mysite_uwsgi.ini # the --ini option is used to specify a file

测试站点是否正常,成功。。

退出Python虚拟环境使用:

deactivate

八、皇帝模式来管理uWSGI

uWSGI可以运行在'皇帝'模式。在这种模式下,它会关注uWSGI配置文件的目录,并为每个配置文件生成一个实例('vassals')。

每当修改配置文件时,皇帝将自动重新启动附庸。

# create a directory for the vassals
sudo mkdir /etc/uwsgi
sudo mkdir /etc/uwsgi/vassals
# symlink from the default config directory to your config file
sudo ln -s /path/to/your/mysite/mysite_uwsgi.ini /etc/uwsgi/vassals/
# run the emperor
uwsgi --emperor /etc/uwsgi/vassals --uid nginx --gid nginx
  • emperor:在哪里寻找附庸(配置文件)
  • uid:进程一旦启动的用户ID
  • gid:进程一旦启动的组ID

九、其他

这里