Alibaba Cloud ECS deployment python and flask (2) use uwsgi emperor + Nginx deployment to automatically start multiple web projects

Prerequisite: This article is a continuation of " Alibaba Cloud ECS Deployment Python and Flask (1)" . Both uwsgi and nginx of a single python project have been successfully configured.
reference:

  1. Install uwsgi in the system environment ( 非python项目中的虚拟环境):

pip3 install uwsgi

  1. Create a new directory: /etc/uwsgi/vassals, copy the uwsgi configuration files of each item to this directory (this step is said to be a soft link in many blogs on the Internet ln -s, but I don’t know why I used the soft link unsuccessfully, and finally I have no choice but to use it directly. copy):

mkdir /etc/uwsgi && mkdir /etc/uwsgi/vassals
cp ~/first_project/f_uwsgi.ini /etc/uwsgi/vassals/
cp ~/second_project/s_uwsgi.ini /etc/uwsgi/vassals/

  1. Create a new /etc/uwsgi/emperor.ini file, the code is as follows:
[uwsgi]
emperor = /etc/uwsgi/vassals
# uid = www
# gid = www

Note (the permission problem has been solved, please refer to: Alibaba Cloud ECS deployment python and flask (3) by setting uid and gid to change the problem that uwsgi+emperor usually uses root permissions
):

  • 由于我尚未搞清楚linux系统中用户和用户组权限问题,所以所有操作包括最后的自启动部署,都是在root下完成的。包括nginx.conf中的user也是root。
  • 我在这里给绕了好久,若真正部署起来全使用root权限还是不好,目前也是迫于无奈,待学习好用户权限问题后再做修改。
  • 权限问题会涉及:1、配置静态文件直接由Nginx处理;2、上边的emporer.ini。(由于我都是用root,所以uid和gid我都注释掉了)
  1. Create a new /etc/systemd/system/emperor.uwsgi.service file, the code is as follows:
[Unit]
Description=uWSGI Emperor
After=syslog.target

[Service]
#uwsgi 服务的路径,以及需要启动的 ini 文件路径,根据自己的实际情况进行修改
ExecStart=/usr/bin/uwsgi --ini /etc/uwsgi/emperor.ini
# Requires systemd version 211 or newer
RuntimeDirectory=uwsgi
Restart=always
KillSignal=SIGQUIT
Type=notify
StandardError=syslog
NotifyAccess=all

[Install]
WantedBy=multi-user.target
  1. Create /usr/lib/systemd/system/nginx.service, (Since I installed it with yum install nginx, this file already exists.) The code is as follows:
[Unit]
Description=The nginx HTTP and reverse proxy server
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/run/nginx.pid
# Nginx will fail to start if /run/nginx.pid already exists but has the wrong
# SELinux context. This might happen when running `nginx -t` from the cmdline.
# https://bugzilla.redhat.com/show_bug.cgi?id=1268621
ExecStartPre=/usr/bin/rm -f /run/nginx.pid
ExecStartPre=/usr/sbin/nginx -t
ExecStart=/usr/sbin/nginx
ExecReload=/bin/kill -s HUP $MAINPID
KillSignal=SIGQUIT
TimeoutStopSec=5
KillMode=process
PrivateTmp=true

[Install]
WantedBy=multi-user.target
  1. Set boot up:

systemctl enable nginx.service
systemctl enable emperor.uwsgi.service

  1. Run service, stop service, restart service, status query:

systemctl start emperor.uwsgi.service
systemctl stop emperor.uwsgi.service
systemctl restart emperor.uwsgi.service
systemctl status emperor.uwsgi.service

systemctl start nginx.service
systemctl stop nginx.service
systemctl restart nginx.service
systemctl status nginx.service

View all started services

systemctl list-units --type=service

Guess you like

Origin blog.csdn.net/qq_41090453/article/details/83959862