Test your skills on our all Hosting services and get 15% off!

Use code at checkout:

Skills
09.10.2024

Different ways to Restart PHP-FPM

PHP-FPM (PHP FastCGI Process Manager) is a powerful and widely used process manager for PHP, often favored for its speed and ability to handle high-traffic websites. Knowing how to restart PHP-FPM is crucial for developers and system administrators, especially when applying changes to PHP configurations, resolving issues, or performing routine maintenance. In this article, we will explore several methods to restart PHP-FPM on different systems and in different environments.

Why Restart PHP-FPM?

Before diving into the methods, let’s briefly discuss why restarting PHP-FPM might be necessary:

  1. Configuration Changes: If you modify PHP settings in the php.ini or php-fpm.conf files, a restart is required for the changes to take effect.
  2. Performance Optimization: Restarting PHP-FPM can help free up memory and resources, especially on high-traffic sites.
  3. Error Resolution: If PHP-FPM encounters issues or becomes unresponsive, restarting can help resolve temporary glitches.
  4. Log Rotation: After rotating log files for PHP-FPM, a restart may be necessary to ensure new logs are written properly.

Prerequisites

Make sure you have root access or the necessary privileges to restart services on your server. Depending on your Linux distribution, you might use different commands like systemctl or service.

Method 1: Using systemctl

On modern Linux distributions such as Ubuntu 16.04+ and CentOS 7+, systemctl is the standard method for managing services, including PHP-FPM. Here’s how you can restart PHP-FPM using systemctl:

Restart PHP-FPM

sudo systemctl restart php-fpm

This command will restart the PHP-FPM service, applying any new configuration changes.

Stop and Start PHP-FPM

If you want more control, you can stop and start PHP-FPM manually:

sudo systemctl stop php-fpm
sudo systemctl start php-fpm

This can be helpful if you need to ensure that the service is completely stopped before starting it again.

Check Status

To ensure that PHP-FPM has restarted successfully, use:

sudo systemctl status php-fpm

This will display the status of the PHP-FPM service, including whether it is active and any recent log messages.

Method 2: Using service Command

The service command is commonly used on older Linux distributions or for backward compatibility. Here’s how to restart PHP-FPM using service:

sudo service php-fpm restart

Or, to stop and start the service separately:

sudo service php-fpm stop
sudo service php-fpm start

Method 3: Restarting Specific PHP-FPM Versions

If you have multiple PHP versions installed, you need to specify the version of PHP-FPM you want to restart. For example, if you have PHP 7.4 installed, use the following command:

sudo systemctl restart php7.4-fpm

Or with service:

sudo service php7.4-fpm restart

This method is particularly useful for environments where different PHP versions are needed for different applications or websites.

Method 4: Sending Signals Directly to PHP-FPM

You can directly send signals to PHP-FPM to reload configurations without fully restarting the service. This is a more subtle approach that can be less disruptive to your web traffic:

Reload Configuration

To reload the PHP-FPM configuration without terminating existing processes, use:

sudo kill -USR2 $(cat /run/php/php-fpm.pid)

Replace /run/php/php-fpm.pid with the path to your actual PID file if it differs. This method is useful when you want to reload configuration changes without restarting all PHP-FPM processes.

Graceful Restart

A graceful restart allows PHP-FPM to finish serving ongoing requests before restarting:

sudo kill -QUIT $(cat /run/php/php-fpm.pid)

This is especially helpful in production environments where you want to avoid interrupting active user sessions.

Method 5: Using pkill or killall

If you’re facing issues with PHP-FPM and need to forcefully stop all processes, you can use pkill or killall. This is a more aggressive method and should be used with caution:

sudo pkill php-fpm

Or:

sudo killall php-fpm

After killing the processes, you need to start PHP-FPM manually:

sudo systemctl start php-fpm

Method 6: Using Nginx or Apache Restart

In some cases, restarting your web server can indirectly restart PHP-FPM if it’s tightly coupled with the web server’s process. Here’s how you can do that:

Nginx

sudo systemctl restart nginx

Apache

sudo systemctl restart apache2

While this method is not recommended for PHP-FPM-specific issues, it can be useful if you are performing maintenance on your entire web stack.

Best Practices for Restarting PHP-FPM

  • Check Logs: Before restarting, check PHP-FPM logs for any errors or warnings. This can help identify underlying issues that may be resolved without a restart.
  • Graceful Restarts in Production: Always prefer a graceful restart in a production environment to avoid disrupting users.
  • Monitor Resource Usage: Use tools like top, htop, or ps to monitor PHP-FPM processes before and after a restart to ensure it’s operating as expected.

Conclusion

Restarting PHP-FPM is a common task that every server administrator or developer should be familiar with. Whether you are using systemctl, service, sending signals, or performing a graceful restart, knowing the right method for your situation can ensure smooth performance and minimal disruption to your services. By following best practices, you can keep your PHP-FPM running efficiently and handle any issues that may arise with confidence.

Test your skills on our all Hosting services and get 15% off!

Use code at checkout:

Skills