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:
- Using systemd (Basic Auto-Restart)
- Using monit (Process Count-Based Auto-Restart)
- Using AWS CloudWatch + Systems Manager (AWS-Native Solution)
- 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
- Edit Apache (httpd) systemd configuration
sudo systemctl edit httpd
Add the following lines:
[Service]
Restart=always
RestartSec=5s
- Edit PHP-FPM systemd configuration
sudo systemctl edit php-fpm
Add the same configuration:
[Service]
Restart=always
RestartSec=5s
- 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
- Install monit
sudo yum install monit -y # For Amazon Linux 2 / RHEL-based systems
- 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
- 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
- Install CloudWatch Agent
sudo yum install -y amazon-cloudwatch-agent
- 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
}
]
}
}
}
- Restart CloudWatch Agent
sudo systemctl enable amazon-cloudwatch-agent
sudo systemctl restart amazon-cloudwatch-agent
- Create a CloudWatch Alarm
- In the AWS Console, go to CloudWatch → Metrics → EC2 → procstat
- Select
pid_count
forhttpd
andphp-fpm
- Set a threshold: If process count > 50, trigger an alarm
- Configure the alarm to send a notification to SNS (Simple Notification Service).
- 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
- Install Datadog Agent
DD_API_KEY=your_api_key bash -c "$(curl -L https://s3.amazonaws.com/dd-agent/scripts/install_script.sh)"
- Enable Process Monitoring
- Edit the Datadog configuration:
sh sudo vi /etc/datadog-agent/datadog.yaml
Add:yaml process_config: enabled: "true"
- Restart Datadog Agent
sudo systemctl restart datadog-agent
- 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
- 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?
Method | Process Count Monitoring | Auto-Restart | AWS Native | Difficulty |
---|---|---|---|---|
systemd | No | Yes (only if process crashes) | No | Easy |
monit | Yes | Yes | No | Medium |
CloudWatch + Systems Manager | Yes | Yes | Yes | Hard |
Datadog + AWS Lambda | Yes | Yes | Yes | Medium |
- 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