欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

django+nginx+supervisor+uwsgi+daphne部署linux各种环境配置以及注意事项

程序员文章站 2022-07-14 20:40:40
...

简介

本文主要讲述通过uwsgi处理http请求,daphne处理websocket请求,然后通过supervisor管理uwsgi与daphne,最后通过nginx作为代理的各种配置以及注意事项。

具体配置

首先你肯定得有个django的项目,然后就是具体的python环境,现在python一般都是3.6左右的版本,具体的环境就如下

pip install django uwsgi supervisor channels
# 其中channels包括了daphne的环境

然后就是下载nginx环境

1)安装 nginx 需要先将官网下载的源码进行编译,编译依赖 gcc 环境
yum install gcc-c++
2)PCRE是一个Perl库,包括 perl 兼容的正则表达式库。nginx 的 http 模块使用 pcre 来解析正则表达式,所以需要在 linux 上安装 pcre 库,pcre-devel 是使用 pcre 开发的一个二次开发库。nginx也需要此库。
yum install -y pcre pcre-devel
3)zlib 库提供了很多种压缩和解压缩的方式, nginx 使用 zlib 对 http 包的内容进行 gzip ,所以需要在 Centos 上安装 zlib 库。
yum install -y zlib zlib-devel
4)下载tar包
yum install wget	安装下载器(如果有可以不安装)
wget -c https://nginx.org/download/nginx-1.12.0.tar.gz		
tar -zxvf nginx-1.12.0.tar.gz			
2)解压安装
cd nginx-1.12.0		
make & make install		编译安装

然后就可以通过systemctl start nginx.service试试安装是否成功
以上基本环境就都搭建完成了,重头戏就在配置文件

创建asgi.py并开启asgi服务

首先要在settings.py同级目录下创建asgi.py文件,文件内容如下

import os
import django
from channels.routing import get_default_application

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "[你自己的项目名].settings")
django.setup()
application = get_default_application()

然后就可以通过daphne -b 0.0.0.0 -p 8002 [你自己的项目名].asgi:application进行启动了,路径是你项目所在的根目录
这时候控制就会显示开启信息
django+nginx+supervisor+uwsgi+daphne部署linux各种环境配置以及注意事项

开启uwsgi服务

然后就是开启uwsgi服务,一般都是通过配置文件进行启动,先创建uwsgi.ini文件,文件路径一定要记得,最好放在项目文件中。其配置如下:

[uwsgi]

# 指定项目执行的端口号
socket = 127.0.0.1:8001
socket-timeout = 10

# Django项目目录
chdir = /home/django-demo
wsgi-file = django-demo/wsgi.py

# 进程数
processes = 10

#线程数
threads = 2

#在项目生成两个文件uwsgi.status和uwsgi.pid 方便查看pid和运行状态
pidfile = /home/ybm-backup/configs/uwsgi_ybm.pid

stats = /home/ybm-backup/configs/uwsgi_ybm.stats


#保存日志文件,这个路径就是刚刚新建的myuwsgi.log文件的路径
daemonize = /home/web-system.log

# 表示不记录正常信息,只记录错误信息
disable-logging = true

然后通过uwsgi --ini /[配置文件所在的路径]/uwsgi.ini启动一般启动成功会出现一行[uWSGI] getting INI configuration from /[配置文件所在的路径]/uwsgi.ini这样的信息,也可以通过netstat -ntlpd查看端口情况判断服务是否启用。

配置nginx

然后就是配置nginx的配置文件了,到nginx安装目录下找到nginx.conf文件打开,配置如下:

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;

# http请求           
upstream django {
    server 127.0.0.1:8001;	
    }
# websocket请求
upstream channels-backend {
    server 127.0.0.1:8002;
}


server {
    listen      8000;
    server_name django-demo.com; 
    charset     utf-8;
    root /home/django-demo;

    # 请求静态文件路径
    location /static {		
        root /home/django-demo/;     
    }

    # /开头的请求
    location / {
        uwsgi_pass  django;
        include     /etc/nginx/uwsgi_params;
        uwsgi_read_timeout 3600;
        uwsgi_param UWSGI_CHDIR /home/django-demo;       
     }
    # /ws开头的请求
    location /ws {
        proxy_pass http://channels-backend;

        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_redirect off;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
    	proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Host $server_name;
     }
  }
}

配置完成后就可以通过systemctl restart nginx.servicel来重启服务,然后在浏览器中访问在nginx中配置的server_name也就是django-demo.com就可以访问了,websocket的问题也可以解决了。

通过supervisor统一管理

daphne是通过非守护进程启动的,而suoervisor就是将其通过守护程序开启
这里同时将uwsgi也一同进行管理。首先创建配置文件supervisor.confc,配置如下:

[program:asgi]
directry = /home/django-demo  # 项目根目录
command =  daphne -b 0.0.0.0 -p 8002 [你自己的项目名].asgi:application
autostart=true
autorestart=true
# 日志文件
stdout_logfile=/var/log/supervisor/%(program_name)s.log
stderr_logfile=/var/log/supervisor/%(program_name)s.log

[program:uwsgi]
directry = /home/django-demo
command = uwsgi --ini /[文件所在的路径]/uwsgi_ybm.ini
autostart=true
autorestart=true
# 日志
stdout_logfile=/var/log/supervisor/%(program_name)s.log
stderr_logfile=/var/log/supervisor/%(program_name)s.log


# 这个是必须的,不然启动服务会报错可以通过下面网站查看具体配置怎么写
#[http://supervisord.org/configuration.html#supervisord-section-settings]
[supervisord]
# supervisor日志,这里日志文件需要到路径下手动创建
logfile = /tmp/supervisord.log
pidfile = /tmp/supervisord.pid
directory = /tmp
childlogdir = /tmp

配置完成后将之前开启的daohne和uwsgi服务关了,就可以通过supervisord -c /[文件所在的路径]/supervisord.conf启动了,这时候daphne就是通过守护程序进行运行了,相应的输出都在之前配置的log文件中。