天天看点

nginx+supervisor+gunicorn部署django2

一、

gunicorn

  • gunicorn

    是一个

    python Wsgi http server

    ,只支持在

    Unix

    系统上运行
  • 官网:https://gunicorn.org/
  • 安装:
pip install gunicorn
           
  • 配置:

    manage.py

    同级目录下,创建文件

    conf.py

    ,写入如下代码:
import multipreocessing

bind = '127.0.0.1:8001'
worders = multiprocessing.cpu_count() * 2
worker_class = 'gevent'
           

二、

gunicorn

部署后静态文件的调整

在项目根路由

urls.py

中添加如下两行代码:

from django.contrib.staticfiles.urls import staticfiles_urlpatterns
.
.
.
.
urlpatterns += staticfiles_urlpatterns()
           

三、

Gunicorn

启动服务

在/项目名/

wsgi.py

同级路径下,执行如下命令:

# conf.wsgi:application中的conf表示wsgi.py文件所在的目录名
gunicorn --config=conf.py conf.wsgi:application
           

四、利用

supervisor

做服务进程管理

  • Supervisor

    是用

    Python

    开发的一套通用的进程管理程序,能将一个普通的命令行进程变为后台

    daemon

    ,并监控进程状态,异常退出时能自动重启
  • 官网:http://www.supervisord.org/
  • 安装:
pip install supervisor
           
  • 安装好后在

    /etc/

    会生成一个

    supervisord.conf

    文件及一个

    supervisord.d

    文件目录,如果没有生成,执行以下命令来生成配置文件和文件夹:
echo supervisord.conf > /etc/supervisord.conf
mkdir /etc/supervisord.d/
           

五、书写

supervisor

配置文件

  • 创建文件

    /etc/supervisord.d/muke_video.conf

    ,写入如下配置:
# 项目名称
[program:muke_video] 
# 执行目录地址,根据自己的项目路径而定
direcotry=/Users/zhangdewei/muke/python/lessoins/lession8/muke_video_test/video
# 执行命令,注意要找到虚拟环境下的 gunicorn。根据自己的虚拟环境路径而定
command=/Users/zhangdewei/muke/python/django/env/bin/gunicorn --config=conf.py config.wsgi:application
# 自动启动
autostart=true
# 自动重启
autorestart=true
# 启动时停滞 1 秒后才启动
startsecs=1
# 启动 supervisor 进程数量
numprocs=1
# 错误日志地址
stderr_logfile=/Users/zhangdewei/muke/python/django/lessions/lession8/err-web.log
# 输出日志地址
stdout_logfile=/Users/zhangdewei/muke/python/django/lessions/lession8/out-web.log
# 关闭服务时,如果有组的概念,可以一起关闭
stopasgroup=true
# 如果进程有很多子进程或线程,下面命令可以‘杀’掉
# 如果不设置,一些子进程会‘杀’不掉,变成‘僵尸’进程
killasgroup=true
           

六、启动

supervisor

supervisord -c /etc/supervisord.conf
           

七、查看

supervisor

服务状态

  • supervisorctl

    --> 进入控制台

    进入目录下

    /etc/supervisord.d/

    ,输入命令:

    supervisorctl

    :
    nginx+supervisor+gunicorn部署django2
  • 通过

    status

    查看服务状态,通过

    restart/stop/start muke_video

    对服务重启,关闭和开启。

八、

Nginx

反向代理

gunicorn

服务

  • Nginx

    是一个高性能的静态服务器,支持负载均衡,多进程,多线程,数据压缩,日志监控,限流等多种功能。是目前比较主流的服务器。
  • 官网:http://nginx.org/en/download.html
  • 各个平台下的安装:

mac

brew install nginx
           

ubuntu

apt-get install nginx
           

redhat & centos

yum install nginx
           

window

:看上边的官网地址

九、

Nginx

配置

django

的方法

  • 每个路径可能都不一样,找到

    nginx

    配置路径,

    /usr/local/nginx/conf/nignx.conf

    nginx+supervisor+gunicorn部署django2
    修改其中的

    http

    下的

    server

    里的内容:
server {
	listen	8080;
	location / {
		service_name www.yourdomain.com;
		client_max_body_size 3M;
		proxy_pass http://127.0.0.1:8001;
	}
}
           

十、

nginx

的启动

如果是自动安装,

nginx

会被自动启动,当书写好

conf

文件之后,进行重启:

./nginx -s reload
           

如果

nginx

初始并没有启动,找到

nginx

执行命令后,执行下面命令,即可启动。:

./nginx
           

查看

nginx

是否启动:

ps aux | grep nginx
           

继续阅读