安装django 是为了做网站
装uwsgi是为了启动django web 服务
装 supervisor 是为了守护 uwsgi 后台服务线程,在意外线程结束时,能自动重启 uwsgi
一、 在安装好 python3之后, 开始安装 Django
执行命令完成安装:
pip3 install Django==2.1.11 -i https://pypi.tuna.tsinghua.edu.cn/simple
这里是强迫安装指定的 django 版本。 -i 是配置安装源是清华的,实测过,清华大学是 最全的。
安装完成后 可以用 pip3 list 命令查看安装的 python package 列表
二、安装 uWSGI
执行命令安装
yum install uwsgi
三、测试 Django + uwsgi
1, 执行命令创建 django的 helloworld
django-admin startproject helloworld
2, 进入目录创建 uwsgi.ini 文件,文件的内容如下:
[uwsgi]
socket = 127.0.0.1:1234 # 1234是端口号
chdir = /www/helloworld # helloworld 是django 网站项目的根目录
module = helloworld.wsgi:application
env = DJANGO_SETTINGS_MODULE = helloworld.settings
master = true # 是否主守护线程
processes = 4 # 最大进程数
vacuum = true
logto = /www/helloworld/logs/uwsgi.log # log 文件位置
pidfile = /www/helloworld/uwsgi.pid # pid文件位置
enable-threads = true
3,修改 helloword/helloworld/settings.py 文件,修改
ALLOWED_HOSTS = ["*"]
4,开始测试
uwsgi --ini /www/helloword/uwsgi.ini
打开浏览器浏览 http://127.0.0.1:1234 即可看到 django helloworld
四、安装 Supervisor
https://www.jianshu.com/p/8b4a0e35ebe3 一个不错的中文讲 Supervisor 的帖子
http://supervisord.org/ 官方文档位置
1,centos 安装命令
yum install supervisor
2, 其 配置文件 /etc/supervisord.conf
vhost 目录是 /etc/supervisord.d/
创建的文件要以 ini来创建,比如 helloworld.ini 文件内容
[program:helloworld]
## 请记住上面这个程序名,一会启动该程序需要用到
## 指定uwsgi和指定ini启动指定django项目
command=/usr/local/bin/uwsgi --ini /www/helloworld/uwsgi.ini
## 项目路径
directory=/www/helloworld
startsecs=0
stopwaitsecs=0
autostart=true
autorestart=true
user=root
## django的虚拟环境, 这个需要的是 python 的 package 根目录
environment=PATH="/usr/local/lib/python3.6"
## 标准错误日志重定向到标准输出日志文件
redirect_stderr=true
## 标准输出日志文件路径
stdout_logfile=/www/helloworld/logs/cinema_log.log
3,运行测试
结束之前的 django uwsgi 后台进程
netstat -ntlp
上一句查看 web 后台服务的列表,然后 kill -9 来结束进程,2345 是通过 上一句命令看到的 pid
kill -9 2345
执行命令启动 Supervisor 服务
systemctl restart supervisord
打开浏览器浏览 http://127.0.0.1:1234 即可看到 django helloworld
五、Supervisor 常用命令
systemctl设置 supervisord 开机启动
systemctl enable supervisord
supervisorctl start mywebsite #启动
supervisorctl stop mywebsite #停止
supervisorctl update #更新配置
supervisorctl reload #重启
supervisorctl start helloworld # 启动程序
supervisorctl stop helloworld # 停止程序