How to use Supervisor to guard PHP-FPM Master process for automatic restart of PHP-FPM

Source: Internet
Author: User
Tags fpm virtualenv
Recently, a colleague has a guardianship requirement for the PHP-FPM process, i.e. if the master process of PHP-FPM quits unexpectedly (possibly crash, it could be a false kill), then the master process is expected to be pulled automatically to avoid interruption of service.

We know that supervisor is a very powerful process monitoring (monitor & Control) tool that can theoretically implement the PHP-FPM master process's daemon requirements. Therefore, I help colleagues to test how to use supervisor to complete his needs, the results show that supervisor is really an artifact, just a reasonable configuration file, it can solve the problem.

Here is my research process and the final implementation of the PHP-FPM main process daemon function configuration file, here to make a record, but also hope to help others.

1. Installing Supervisor

Supervisor itself is a Python implementation, and is the research phase, so first create a new virtualenv environment, and then install the Supervisor package with PIP.

At this point, the basic research environment has been built. Of course, PHP-FPM and PHP environments and the front-end nginx are ready for the long time.

2. Analyzing php-fpm.sh Scripts

Usually after compiling and installing PHP, PHP-FPM this 2-binary C program will also be compiled and installed, typical path in the php_install_path/sbin/directory. The directory also has a script named php-fpm.sh to control actions such as start/stop/restart/reload of the PHP-FPM process.

In the./sbin/php-fpm.sh script, the "start" Operation initiates the PHP-FPM master process, and the rest is implemented by signal to the PHP-FPM master process.

# # code segment in Php-fpm.shcase "$" in        start)                echo-n "Starting php-fpm"                # # The following line is the key command                $php _fpm_bin--d Aemonize $php _opts                if ["$?"! = 0]; then                        echo "Failed"                        exit 1                fi                wait_for_pid created $php _fpm_pid< c9/>if [-N "$try"]; Then                        echo "Failed"                        exit 1                Else                        echo ' done '                fi        ;;

From the above is the terminal input "./sbin/php-fpm.sh start", the actual execution of the code, you can see that the PHP-FPM process startup parameters are? daemonize $php _opts, and the $php_opts value is "? Fpm-config $ php_fpm_conf pid $php _fpm_pid ".

Note : When php-fpm.sh starts the PHP-FPM master process, the Daemonize parameter is passed in, indicating that PHP-FPM master processes is started in Daemon (daemon) mode. According to the supervisor documentation, when using the Supervisor monitoring process, the guardianship process cannot be the daemon , because the daemon usually "ends the life" of the parent process after the child process is forked. That is, the parent process created by supervisor exits, at which point supervisor can no longer monitor the child processes that have been created by the exiting process. The behavior of Daemon process can be understood by referring to the Linux Daemon Writing howto article.

Based on the above analysis, we know that as long as supervisor starts the PHP-FPM process, it does not pass in the Daemonize parameter.

3. Supervisor configuration file that implements the PHP-FPM Master process daemon function

The above analysis has told us how to solve the problem, the following directly on the verification of the available configuration files. The file is located in the php-fpm.conf sibling directory (typical path is php_install_path/etc/).

[Inet_http_server]; inet (TCP) server disabled by defaultport=127.0.0.1:9015; (Ip_address:port specifier, *:p ort for all iface) [Supervisord]logfile=./var/log/supervisord.log; (Main log File;default $CWD/supervisord.log) logfile_maxbytes=50mb; (max main logfile bytes b4 rotation;default 50MB) logfile_backups=2; (Num of main logfile rotation backups;default) loglevel=info; (log level;default info; others:debug,warn,trace) pidfile=./var/run/supervisord.pid; (Supervisord pidfile;default supervisord.pid) Nodaemon=false; (Start in foreground if true;default false) minfds=1024; (min. avail startup file Descriptors;default 1024x768) minprocs=200; (min. avail process descriptors;default) identifier=sup.php-fpm; (supervisord identifier, default is ' supervisor ') [Rpcinterface:supervisor]supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface[ Supervisorctl]serverurl=http://127.0.0.1:9015; Use an http://URL to specify an inet socket[program:php-fpm]command=bash-c "Sleep 1 &&/home/slvher/tools/php/5. 6.11/SBIN/PHP-FPM--fpm-config/home/slvher/tools/php/5.6.11/etc/php-fpm.conf--pid/home/slvher/tools/php/5.6.11/ Var/run/php-fpm.pid "; The program (relative uses PATH, can take args) process_name=% (program_name) s; Process_name Expr (default% (program_name) s) autostart=true; Start at Supervisord start (default:true) autorestart=true; Whether/when to restart (default:unexpected) startretries=5; Max # of Serial start failures (default 3) exitcodes=0,2,70; ' Expected ' exit codes for process (default 0,2) Stopsignal=quit; Signal used to kill process (default term) stopwaitsecs=2; Max num secs to wait b4 SIGKILL (default 10)

The configuration file structure is easy to grasp by looking at the supervisor documentation, with two configuration items that require special attention :

1) command

It specifies the start command for the process to be monitored by the supervisor, as you can see, we have not passed the daemonize parameter to PHP-FPM, and the rest of the arguments simply expand the shell variable in the php-fpm.sh.

It has been noted that command is not a direct php-fpm, but rather the execution of two commands through Bash-c, and the first command is Sleep 1. This is due to the fact that the port that is occupied by PHP-FPM after stop is usually not released immediately, and when supervisor attempts to pull up the process at a very fast speed, it may fail several times due to the following error retry:

# # var/log/php-fpm.error.log[18-jul-2015 21:35:28] error:unable to bind listening sockets for address ' 127.0.0.1:9002 ': Ad Dress already in use (98) [18-jul-2015 21:35:28] ERROR:FPM initialization failed

And supervisor currently does not support the delay restart function, therefore, here can only be done by first sleep and then start a slightly tricky method to solve the problem, the results show that the curative effect is good and no side effects. -_-

2) AutoRestart

The documentation is described below:

May be one of false, unexpected, or true. If false, the process would never be autorestarted. If unexpected, the process would be restart when the program exits with a exit code that was not one of the exit codes Asso Ciated with this process ' configuration (see Exitcodes). If true, the process would be unconditionally restarted when it exits, without regard to its exit code.

The default value is unexpected, which indicates that supervisor will not pull the process again until the exit code exception for the monitored process. This is set to true to indicate that any time the process exits will be pulled again.

After this configuration, in the 1th step of this article set up a good virtualenv environment, run the following command to complete the Supervisor PHP-FPM Master process monitoring:

Shell> supervisord-c etc/sup.php-fpm.conf

Then, by PS x | Fgrep FPM can see that the PHP-FPM master process has been pulled up.
Then, kill the PHP-FPM main process, again PS x | Fgrep FPM You can see that a new PHP-FPM master process is created by supervisor.

At this point, the need to use Supervisor Daemon PHP-FPM master process to achieve PHP-FPM automatic restart has been resolved.

Resources

    1. Supervisor:a Process Control System
    2. Linux Daemon Writing HOWTO

========================= EOF ====================

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.