Automatically Restart Apache & PHP-FPM When Reaching Max Process Limit on AWS EC2

If Apache (httpd) or PHP-FPM on AWS EC2 reaches the maximum process limit, it can cause performance issues or even system failure. To prevent this, you can implement an automatic restart mechanism. Below are four methods to achieve this:

  1. Using systemd (Basic Auto-Restart)
  2. Using monit (Process Count-Based Auto-Restart)
  3. Using AWS CloudWatch + Systems Manager (AWS-Native Solution)
  4. Using Datadog + AWS Lambda (Advanced Monitoring & Auto-Restart)

1. Using systemd for Automatic Restart

If Apache or PHP-FPM crashes or stops unexpectedly, systemd can automatically restart them.

Steps to Configure systemd Auto-Restart

  1. Edit Apache (httpd) systemd configuration
   sudo systemctl edit httpd

Add the following lines:

   [Service]
   Restart=always
   RestartSec=5s
  1. Edit PHP-FPM systemd configuration
   sudo systemctl edit php-fpm

Add the same configuration:

   [Service]
   Restart=always
   RestartSec=5s
  1. Reload systemd and restart services
   sudo systemctl daemon-reexec
   sudo systemctl restart httpd
   sudo systemctl restart php-fpm

Pros: Simple and effective for unexpected crashes.
Cons: Does not restart services when process count exceeds the threshold.


2. Using monit to Monitor Process Count and Restart Services

monit is a lightweight monitoring tool that can track the number of Apache and PHP-FPM processes and restart them when exceeding a defined limit.

Steps to Configure monit

  1. Install monit
   sudo yum install monit -y  # For Amazon Linux 2 / RHEL-based systems
  1. Configure monit to monitor Apache and PHP-FPM
   sudo vi /etc/monit.d/apache_php

Add the following rules:

   check process httpd with pidfile /var/run/httpd/httpd.pid
       start program = "/bin/systemctl start httpd"
       stop program  = "/bin/systemctl stop httpd"
       if children > 50 then restart
       if 5 restarts within 5 cycles then alert

   check process php-fpm with pidfile /run/php-fpm/php-fpm.pid
       start program = "/bin/systemctl start php-fpm"
       stop program  = "/bin/systemctl stop php-fpm"
       if children > 50 then restart
       if 5 restarts within 5 cycles then alert
  1. Enable and start monit
   sudo systemctl enable monit
   sudo systemctl restart monit

Pros: Monitors process count and restarts services proactively.
Cons: Requires additional software setup.


3. Using AWS CloudWatch + Systems Manager for Auto-Restart

AWS provides a native way to monitor process count and restart services automatically using CloudWatch and AWS Systems Manager (SSM).

Steps to Configure AWS CloudWatch Monitoring

  1. Install CloudWatch Agent
   sudo yum install -y amazon-cloudwatch-agent
  1. Configure CloudWatch Agent to monitor Apache & PHP-FPM processes
   sudo vi /opt/aws/amazon-cloudwatch-agent/etc/amazon-cloudwatch-agent.json

Add the following configuration:

   {
       "metrics": {
           "metrics_collected": {
               "procstat": [
                   {
                       "pattern": "httpd",
                       "measurement": ["pid_count"],
                       "metrics_collection_interval": 60
                   },
                   {
                       "pattern": "php-fpm",
                       "measurement": ["pid_count"],
                       "metrics_collection_interval": 60
                   }
               ]
           }
       }
   }
  1. Restart CloudWatch Agent
   sudo systemctl enable amazon-cloudwatch-agent
   sudo systemctl restart amazon-cloudwatch-agent
  1. Create a CloudWatch Alarm
  • In the AWS Console, go to CloudWatch → Metrics → EC2 → procstat
  • Select pid_count for httpd and php-fpm
  • Set a threshold: If process count > 50, trigger an alarm
  • Configure the alarm to send a notification to SNS (Simple Notification Service).
  1. Trigger AWS Systems Manager to Restart Services
  • Use AWS Lambda to invoke AWS Systems Manager Run Command:
   import boto3

   ssm_client = boto3.client('ssm')

   def lambda_handler(event, context):
       response = ssm_client.send_command(
           InstanceIds=['your-ec2-instance-id'],
           DocumentName='AWS-RunShellScript',
           Parameters={'commands': ['sudo systemctl restart httpd', 'sudo systemctl restart php-fpm']}
       )
       return response

Pros: Fully AWS-native solution with monitoring and automated recovery.
Cons: Requires CloudWatch setup and AWS Lambda integration.


4. Using Datadog + AWS Lambda

Datadog provides detailed process monitoring and can automatically trigger AWS Lambda to restart services when the process count exceeds a threshold.

Steps to Configure Datadog Monitoring

  1. Install Datadog Agent
   DD_API_KEY=your_api_key bash -c "$(curl -L https://s3.amazonaws.com/dd-agent/scripts/install_script.sh)"
  1. Enable Process Monitoring
  • Edit the Datadog configuration:
    sh sudo vi /etc/datadog-agent/datadog.yaml
    Add:
    yaml process_config: enabled: "true"
  1. Restart Datadog Agent
   sudo systemctl restart datadog-agent
  1. Set an Alert in Datadog
  • Go to Datadog Dashboard → Monitors → Create Monitor
  • Select Process Monitoring
  • Configure the condition: If Apache or PHP-FPM process count > 50, trigger an alert
  • Set a notification to Datadog Webhook
  1. Create AWS Lambda Function
  • The Webhook triggers AWS Lambda to execute:
   import boto3

   ssm_client = boto3.client('ssm')

   def lambda_handler(event, context):
       response = ssm_client.send_command(
           InstanceIds=['your-ec2-instance-id'],
           DocumentName='AWS-RunShellScript',
           Parameters={'commands': ['sudo systemctl restart httpd', 'sudo systemctl restart php-fpm']}
       )
       return response

Pros: Advanced process monitoring and cloud-based visualization.
Cons: Requires Datadog setup and Webhook integration.


Conclusion: Which Method to Choose?

MethodProcess Count MonitoringAuto-RestartAWS NativeDifficulty
systemdNoYes (only if process crashes)NoEasy
monitYesYesNoMedium
CloudWatch + Systems ManagerYesYesYesHard
Datadog + AWS LambdaYesYesYesMedium
  • For simple crash recovery → Use systemd
  • For process count-based restarts → Use monit
  • For a fully AWS-integrated solution → Use CloudWatch + Systems Manager
  • For cloud-based monitoring with visualization → Use Datadog + AWS Lambda