How to Properly Restart PHP-FPM to Reload php.ini Configuration


2 views

When modifying PHP configuration files like php.ini, you'll often need to restart PHP-FPM for changes to take effect. Unlike Apache's graceful reload, PHP-FPM requires specific approaches depending on your system configuration.

# Systemd-based systems (most modern Linux distributions)
sudo systemctl restart php-fpm

# Older SysVinit systems
sudo service php-fpm restart

# Direct process signaling (when service management isn't available)
sudo kill -USR2 `cat /var/run/php-fpm.pid`

After restarting, verify your changes with:

php -i | grep "loaded configuration file"
php-fpm -v

For production environments, consider these alternatives:

# Graceful reload (maintains existing connections)
sudo systemctl reload php-fpm

# Alternative graceful method
sudo kill -USR2 $(pgrep php-fpm)

Check logs when facing issues:

# Common log locations
tail -f /var/log/php-fpm.log
journalctl -u php-fpm -f

Ensure your php-fpm.conf has proper settings:

; Sample configuration snippet
[global]
pid = /run/php-fpm.pid
error_log = /var/log/php-fpm.log
emergency_restart_threshold = 10
emergency_restart_interval = 1m

PHP-FPM (FastCGI Process Manager) is a popular alternative PHP FastCGI implementation with additional features useful for heavy-loaded sites. When you modify php.ini or pool configuration files, you need to restart or reload PHP-FPM for changes to take effect.

Before restarting, it's good practice to check the current status:

sudo systemctl status php-fpm
# or for older systems:
sudo service php-fpm status

There are two main approaches to apply configuration changes:

1. Graceful Reload (Preferred)

This reloads configuration without dropping active connections:

sudo systemctl reload php-fpm
# Alternative commands:
sudo kill -USR2 $(cat /var/run/php-fpm.pid)
sudo service php-fpm reload

2. Full Restart

Completely stops and starts the service (breaks active connections):

sudo systemctl restart php-fpm
# Alternative commands:
sudo service php-fpm restart
sudo /etc/init.d/php-fpm restart

After restarting, verify your changes took effect:

php -i | grep "Loaded Configuration File"
php-fpm -tt # Tests configuration syntax
sudo systemctl status php-fpm

If restart fails, check these:

# Check error logs:
sudo tail -f /var/log/php-fpm.log

# Test configuration syntax:
sudo php-fpm -t

# If using custom pools:
sudo php-fpm7.4 -t # Replace with your PHP version

For production environments, consider automating reloads when config files change. Here's an example using inotifywait:

#!/bin/bash
while inotifywait -e modify /etc/php/*/fpm/php.ini; do
    systemctl reload php-fpm
    echo "Reloaded PHP-FPM at $(date)"
done

When using containers, the restart approach differs:

# For single container:
docker exec -it php-fpm-container kill -USR2 1

# For docker-compose:
docker-compose exec php-fpm kill -USR2 1