一、 gunicorn
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
部署后静态文件的调整
gunicorn
在项目根路由
urls.py
中添加如下两行代码:
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
.
.
.
.
urlpatterns += staticfiles_urlpatterns()
三、 Gunicorn
启动服务
Gunicorn
在/项目名/
wsgi.py
同级路径下,执行如下命令:
# conf.wsgi:application中的conf表示wsgi.py文件所在的目录名
gunicorn --config=conf.py conf.wsgi:application
四、利用 supervisor
做服务进程管理
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
配置文件
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
supervisor
supervisord -c /etc/supervisord.conf
七、查看 supervisor
服务状态
supervisor
-
supervisorctl
--> 进入控制台
进入目录下
,输入命令:/etc/supervisord.d/
:supervisorctl
- 通过
查看服务状态,通过status
对服务重启,关闭和开启。restart/stop/start muke_video
八、 Nginx
反向代理 gunicorn
服务
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
django
- 每个路径可能都不一样,找到
配置路径,nginx
修改其中的/usr/local/nginx/conf/nignx.conf
下的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
如果是自动安装,
nginx
会被自动启动,当书写好
conf
文件之后,进行重启:
./nginx -s reload
如果
nginx
初始并没有启动,找到
nginx
执行命令后,执行下面命令,即可启动。:
./nginx
查看
nginx
是否启动:
ps aux | grep nginx