背景
centos7.3
python 2.7.5
django 1.11.27
uwsgi 2.0.18
nginx 1.16.1
supervisor 4.2.1
check
python --version
进入python import django django.VERSION
uwsgi --version
nginx -v
supervisord -v
uwsgi
安装
yum install python-devel
pip install uwsgi
uwsgi --version # 查看 uwsgi 版本
配置
需新建xx.ini
[uwsgi]
socket = /home/sock/[xxx].sock
chdir = /home/www/[xxx]
wsgi-file = [xxx]/wsgi.py
touch-reload = /home/www/[xxx]/reload #新建reload文件,每次touch reload时可以重启uwsgi
processes = 1
threads = 2
chmod-socket = 777
chown-socket = www:www
vacuum = true
使用配置启动
uwsgi --ini /etc/uwsgi9090.ini
nginx
安装
yum install nginx -y
rpm -qa | grep nginx
配置目录
/etc/nginx
nginx+sock配置
nginx的cfg中的user需与uwsgi配置中的匹配
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
root /usr/share/nginx/html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
uwsgi_pass unix:///home/sock/[xxx].sock; #此为nginx 使用 sock 的重点
include /etc/nginx/uwsgi_params;
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
启动
service nginx start
supervisor守护进程
安装
pip install supervisor
生成默认配置文件
echo_supervisord_conf > /etc/supervisord.conf
打开 supervisor.conf 在最底部添加
[program:xxx]
command=/usr/bin/uwsgi --ini /home/www/xxx/uwsgi.ini
directory=/home/www/xxx
startsecs=0
stopwaitsecs=0
autostart=true
autorestart=true
启动
supervisord -c /etc/supervisord.conf
重启 xxx项目
supervisorctl -c /etc/supervisord.conf restart xxx
其他命令
supervisorctl -c /etc/supervisord.conf [start|stop|restart] [program-name|all]
开机自启动
nginx
systemcel enable nginx.service
supervisor
vim /lib/systemd/system/supervisord.service
添加以下内容
[Unit]
Description=Process Monitoring and Control Daemon
After=rc-local.service nss-user-lookup.target
[Service]
Type=forking
ExecStart=/usr/bin/supervisord -c /etc/supervisord.conf // 开机启动时执行
ExecStop=/usr/bin/supervisord shutdown // 暂时不知道用处
ExecReload=/usr/bin/supervisord reload // 暂时不知道用处
killMode=process
Restart=on-failure
RestartSec=42s
[Install]
WantedBy=multi-user.target
保存后执行自启动配置
systemctl enable supervisord
验证
systemctl is-enabled supervisord nginx
均为enabled即可