简体   繁体   中英

Is there an easy way to automatically do a php-fpm restart after a 502 gateway timeout on server?

Do you have any useful links, tips or scripts about installing a heartbeat-tool for a bigger site that uses Wordpress and nginx. If too many people visit that site at the same time server shuts down. I need something to automatically restart the site immediately after that happens.

Regards

Your question is how to restart PHP on a 502. My first answer is an attempt at preventing the 502 from happening in the first place.

  1. It's possible that PHP is consuming too much memory. My guess is that your number of php FCGI children is set too high. In your init script you should have an entry like PHP_FCGI_CHILDREN=20 or similar that controls the amount of PHP processes that will start. I would try reducing the number. If you can identify the average memory per PHP process (using top perhaps) then you can establish the max number of PHP processes that should run. For example, if you have a 2,000MB server and your PHP processes consume a max of 100MB each then you'll want to limit them to 20.

  2. You can create another location and start the name with the @ symbol. The @ symbol is used for "internal" locations. I like to use the http://openresty.org distribution of nginx. It includes the ngx_lua http://wiki.nginx.org/HttpLuaModule module. Lua is a scripting language that can (among other things) execute shell commands. For example:

     location / { error_page 502 = @php502error; } location @php502error { content_by_lua 'os.execute("/bin/restart-my-php-processes.sh")'; }

    os.execute is blocking, so you'll want to keep that in mind... I've heard of people setting up a thttpd to run scripts. So you'd proxy_pass in your @php502error location.

Although kaicurry is correct, you should be editing your php.ini file to hopefully tackle the source of the problem; to actually answer your question:

  1. Edit your php-fpm.service file. Eg, type nano /lib/systemd/system/php-fpm.service .

  2. Add the following 2 lines at the bottom of [Service] :

    Restart=on-failure

    RestartSec=5s

  3. Restart systemctl: systemctl daemon-reload .

Php will now automatically restart whenever it fails. You should ocassional check the logs to make sure it's not failing often.

You may also wish to do the same for nginx: /lib/systemd/system/nginx.service

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM