Blog / Linux/ Guide to Configuring an Automatic PHP-FPM Restart Script for 502 Bad Gateway Errors in LNMP

Guide to Configuring an Automatic PHP-FPM Restart Script for 502 Bad Gateway Errors in LNMP

LNMP 环境 502 Bad Gateway 错误自动重启 PHP-FPM 脚本配置指南

Problem Background

In an LNMP (Linux + Nginx + MySQL + PHP) environment, websites may occasionally encounter a "502 Bad Gateway" error. This is typically caused by PHP-FPM process pool exhaustion, process crashes, or response timeouts. While manually restarting the PHP-FPM service can temporarily resolve the issue, a better solution is to configure an automatic monitoring and restart script.

Automatic Monitoring and Restart Script

The following Bash script checks the HTTP status code of a specified URL to determine if the service is healthy. If a 502 error is detected, it automatically restarts the PHP-FPM service.

#!/bin/bash

# Replace with your website URL
CheckURL="https://your-website.com/"

# Get HTTP status code with a 10-second timeout
STATUS_CODE=$(curl -o /dev/null -m 10 --connect-timeout 10 -s -w "%{http_code}" "$CheckURL")

# Uncomment for debugging
# echo "$CheckURL Status Code:t$STATUS_CODE"

if [ "$STATUS_CODE" = "502" ]; then
    # Restart PHP-FPM service
    systemctl restart php-fpm  # For Systemd systems
    # /etc/init.d/php-fpm restart  # For SysVinit systems
fi

Script Usage Steps

  1. Create the script file: Save the script content to your server, e.g., /root/check_502.sh. Replace https://your-website.com/ with your actual URL.
  2. Grant execute permission:
    chmod +x /root/check_502.sh
  3. Test the script: Run it manually to ensure no syntax errors and correct commands/paths.
    bash /root/check_502.sh

Configure Cron Job

To run the script automatically at regular intervals, add it to the system's cron scheduler.

  1. Edit the current user's crontab:
    crontab -e
  2. Add a cron rule: Insert the following line to run the check every 5 minutes.
    */5 * * * * /bin/bash /root/check_502.sh > /dev/null 2>&1

    Note: > /dev/null 2>&1 discards all output to prevent cron emails. To log output for debugging, use >> /var/log/check_502.log 2>&1 instead.

  3. Restart Cron service (optional): Usually not required. If needed:
    # Systemd systems
    systemctl restart crond  # or cron
    
    # SysVinit systems
    service crond restart

Additional Notes and Considerations

  • LNMP One-Click Install Package: If you use the LNMP one-click package, a similar script is included at /root/lnmp1.x/tools/check502.sh (replace 1.x with your version). You can modify it instead of creating a new one.
  • Service Restart Command: Adjust the restart command based on your Linux distribution and init system. Modern systems (CentOS 7+, Ubuntu 16.04+) typically use systemctl restart php-fpm. Older systems may use /etc/init.d/php-fpm restart. Verify with systemctl status php-fpm or ps aux | grep php-fpm.
  • Root Cause Investigation: This script is a temporary fix. Frequent 502 errors indicate underlying issues such as:
    • Insufficient PHP-FPM pool settings (e.g., pm.max_children).
    • PHP script execution timeout (max_execution_time).
    • Slow or failed database connections.
    • Insufficient server memory or CPU resources.

    Check PHP-FPM and Nginx error logs (commonly in /usr/local/php/var/log/ and /usr/local/nginx/logs/) to identify and resolve the root cause.

  • Script Security: Store the script in a secure directory (e.g., /root/ or /usr/local/bin/) and restrict permissions to prevent unauthorized access or modification.

Post a Comment

Your email will not be published. Required fields are marked with *.