ubuntu supervisor manages uwsgi+nginx

I. Overview

superviosr is a process monitoring tool on Linux/Unix systems. His/her supervisor is a general process management program developed by Python, which can manage and monitor processes on Linux, and can turn a common command line process into a background daemon. And monitor the process status, it can automatically restart when it exits abnormally. But like daemontools, it cannot monitor the daemon process (that is, the background process)

Two, installation

apt-get install -y supervisor

After the installation is successful, the supervisord.conf configuration file will be generated in the /etc/supervisor directory.

You can also use the echo_supervisord_conf> supervisord.conf command to generate the default configuration file (not recommended, there is more content).

Example configuration of supervisord.conf:

; supervisor config file

[ unix_http_server]
file=/var/run/supervisor.sock   ;(the path to the socket file)
chmod=0700; sockef file mode(default0700)[supervisord]
logfile=/var/log/supervisor/supervisord.log ;(main log file;default $CWD/supervisord.log)
pidfile=/var/run/supervisord.pid ;(supervisord pidfile;default supervisord.pid)
childlogdir=/var/log/supervisor            ;('AUTO' child log dir,default $TEMP); the below section must remain in the config file for RPC
;( supervisorctl/web interface) to work, additional interfaces may be
; added by defining them in separate rpcinterface: sections
[ rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface

[ supervisorctl]
serverurl=unix:///var/run/supervisor.sock ; use a unix:// URL  for a unix socket; The [include] section can just contain the "files" setting.  This
; setting can list multiple files(separated by whitespace or
; newlines).  It can also contain wildcards.  The filenames are
; interpreted as relative to this file.  Included files *cannot*; include files themselves.[include]
files =/etc/supervisor/conf.d/*.conf

The process configuration will read the *.conf configuration file in the /etc/supervisor/conf.d directory

After the installation is complete, the supervisor is started by default

Three, manage uwsgi

In the previous article, the link is as follows:

https://www.cnblogs.com/xiao987334176/p/11329906.html

Uwsgi and nginx have been configured. These are two more critical processes. If any one of them dies, the web page cannot be accessed.

Modify uwsgi configuration

Turn off background operation, why? Because supervisord cannot manage background processes

cd /www/mysite1/uwsgi
vim uwsgi.ini

Comment out daemonize

[ uwsgi]

# Django-related settings
# the base directory(full path)
chdir           =/www/mysite1
# Django's wsgi file
module          = mysite1.wsgi
# the virtualenv(full path)
home            =/virtualenvs/venv
# process-related settings
# master
master          =true
# maximum number of worker processes
processes       =1
# pid file
pidfile         =/www/mysite1/uwsgi/uwsgi.pid
# socket file path(full path)
socket          =/www/mysite1/uwsgi/mysite1.sock
# clear environment on exit
vacuum          =true
# The process runs in the background and types the log to the specified log file
# daemonize       =/www/mysite1/uwsgi/uwsgi.log

Close uwsgi

/virtualenvs/venv/bin/uwsgi --stop uwsgi.pid

Added uwsgi process configuration file

cd /etc/supervisor/conf.d
vim uwsgi.conf

The content is as follows:

[ program:uwsgi]
directory =/www/mysite1 ;Start directory of the program
command=/virtualenvs/venv/bin/uwsgi --ini uwsgi/uwsgi.ini ;Start command
autostart =true;Automatically start when supervisord starts
startsecs =5;If there is no abnormal exit after 5 seconds of startup, it is deemed to have started normally
autorestart =true;Automatically restart after the program exits abnormally
startretries =3;The number of automatic retries on startup failure, the default is 3
user = root          ;Which user to start with
redirect_stderr =true;Redirect stderr to stdout, default false
stdout_logfile_maxbytes = 20MB  ;stdout log file size, default 50MB
stdout_logfile_backups =20;stdout log file backup number

; stdout log file, you need to pay attention that it cannot start normally when the specified directory does not exist, so you need to create the directory manually(supervisord will automatically create log files)
stdout_logfile =/www/mysite1/logs/stdout.log
; Output error file
stderr_logfile =/www/mysite1/logs/stderr.log
; Add environment variables needed for operation,Virtual environment
; environment=PYTHONPATH=$PYTHONPATH:/virtualenvs/venv/bin/;Then make sure that after killing the main process, the child process can also be stopped
stopasgroup=true
killasgroup=true

Create log directory

mkdir /www/mysite1/logs/

**Note: supervisord will not automatically create the directory for you, so you need to create it manually. **

Load configuration

supervisorctl reload

If it appears:

error:<class'socket.error'>,[Errno 2] No such file or directory: file:/usr/lib/python2.7/socket.py line:228

First confirm whether the process exists: ps -ef | grep supervisord and then use the command supervisord -c /etc/supervisor/supervisord.conf to start it.

View status

The first view, the state is STARTING, the second view, the state is RUNNING

root@ubuntu:/etc/supervisor/conf.d# supervisorctl status
uwsgi                            STARTING  
root@ubuntu:/etc/supervisor/conf.d# supervisorctl status
uwsgi                            RUNNING   pid 20367, uptime 0:00:12

kill process

root@ubuntu:/etc/supervisor/conf.d# ps -aux|grep uwsgi
root     203670.30.810268034832?        S    13:500:00/virtualenvs/venv/bin/uwsgi --ini uwsgi/uwsgi.ini
root     203690.00.710268028768?        S    13:500:00/virtualenvs/venv/bin/uwsgi --ini uwsgi/uwsgi.ini
root     203770.00.015984976 pts/1    S+13:520:00 grep --color=auto uwsgi
root@ubuntu:/etc/supervisor/conf.d# killall -9 uwsgi
root@ubuntu:/etc/supervisor/conf.d# ps -aux|grep uwsgi
root     203790.00.810267634844?        S    13:520:00/virtualenvs/venv/bin/uwsgi --ini uwsgi/uwsgi.ini
root     203810.00.710267628488?        S    13:520:00/virtualenvs/venv/bin/uwsgi --ini uwsgi/uwsgi.ini
root     203830.00.015984968 pts/1    S+13:520:00 grep --color=auto uwsgi

The above information can be found. After the process is forcibly killed, the supervisor will start uwsgi.

Fourth, manage Nginx

Since supervisor cannot monitor background programs,

command = /usr/local/bin/nginx This command is started in the background by default.
Add -g'daemon off;' this parameter can solve this problem, this parameter means to run in the foreground.

command = /usr/local/bin/nginx -g ‘daemon off;’

Add nginx process configuration file

cd /etc/supervisor/conf.d
vim nginx.conf

The content is as follows:

[ program:nginx]
command =/usr/sbin/nginx -g 'daemon off;'
startsecs=0
autostart=true
autorestart=true
stdout_logfile=/var/log/nginx/stdout.log
stopasgroup=true
killasgroup=true

Load configuration

supervisorctl reload

Check status

root@ubuntu:/etc/supervisor/conf.d# supervisorctl status
nginx                            RUNNING   pid 20409, uptime 0:00:00uwsgi                            RUNNING   pid 20404, uptime 0:00:07

kill the nginx process

root@ubuntu:/etc/supervisor/conf.d# ps -aux|grep nginx
root     204410.20.21251169832?        S    13:580:00 nginx: master process /usr/sbin/nginx -g daemon off;
www-data 204420.00.01254403228?        S    13:580:00 nginx: worker process
root     204460.00.0159841084 pts/1    S+13:580:00 grep --color=auto nginx
root@ubuntu:/etc/supervisor/conf.d# killall -9 nginx
root@ubuntu:/etc/supervisor/conf.d# ps -aux|grep nginx
root     204480.00.21251169792?        S    13:580:00 nginx: master process /usr/sbin/nginx -g daemon off;
www-data 204490.00.01254403132?        S    13:580:00 nginx: worker process
root     204510.00.015984936 pts/1    S+13:580:00 grep --color=auto nginx

This article references:

https://www.cnblogs.com/xishuai/p/ubuntu-install-supervisor.html

https://blog.csdn.net/qq_32402917/article/details/80169366

https://blog.csdn.net/genglei1022/article/details/81239900

Recommended Posts

ubuntu supervisor manages uwsgi+nginx
Use supervisor in ubuntu