To begin, it’s important to note that PHP-FPM (FastCGI Process Manager) is an alternative PHP FastCGI implementation with some additional features useful for sites of any size, especially busier sites. PHP-FPM allows for more efficient use of system resources, and can help improve the overall performance of your PHP-based website.
In this tutorial, we will show you how to install and configure PHP-FPM with Apache2 on Rocky Linux. This process should also work on other Linux distributions with some slight modifications.
Before we begin, make sure you have a working Apache2 installation on your system. If not, you can refer to the Apache2 documentation for installation instructions.
Install PHP-FPM
The first step is to install PHP-FPM on your system. This can be done using your system’s package manager. For example, on Rocky Linux you can use the dnf
command to install PHP-FPM:
dnf install php-fpm
Once the installation is complete, you can check the version of PHP-FPM installed on your system using the following command:
php-fpm -v
Step 2: Configure PHP-FPM
Once PHP-FPM is installed, you will need to configure it to work with Apache2. The main configuration file for PHP-FPM is typically located at /etc/php-fpm.conf
. Open this file in a text editor and make the following changes:
- Set the
listen
parameter to/var/run/php-fpm.sock
. This will configure PHP-FPM to use a Unix socket file for communication with Apache2.
listen = /var/run/php-fpm.sock
- Set the
listen.owner
andlisten.group
parameters to the user and group that Apache2 runs as. This is typicallyapache
for both the user and group.
listen.owner = apache
listen.group = apache
- Set the
listen.mode
parameter to0666
. This will set the correct permissions on the Unix socket file so that Apache2 can access it.
listen.mode = 0666
- Save the file and exit the text editor.
Create PHP-FPM Pools
Next, you will need to create one or more PHP-FPM pools for your websites. A pool is a separate process that is used to run PHP scripts for a specific website. This allows you to have separate settings and configurations for each website, and also allows you to easily manage and monitor the resource usage of each website.
To create a PHP-FPM pool, create a new file in the /etc/php-fpm.d
directory. The name of the file should match the name of the pool you are creating. For example, if you are creating a pool for the website www.example.com
, the name of the file should be www.example.com.conf
.
In the new pool configuration file, add the following settings:
[www.example.com]
user = apache
group = apache
listen = /var/run/php-fpm-www.example.com.sock
listen.owner = apache
listen.group = apache
listen.mode = 0660
pm = dynamic
pm.max_children = 50
pm.start_servers = 5
pm.min_spare_servers = 5
pm.max_spare_servers = 35
These settings configure the pool to run PHP scripts as the apache
user and group, to use a Unix socket file for communication with Apache2, and to use the dynamic
process manager with a maximum of 50 child processes. The pm.start_servers
, pm.min_spare_servers
, and pm.max_spare_servers
settings control the number of child processes that are started and kept spare to handle incoming requests.
You can create as many pool configuration files as you need for your websites, each with its own unique settings and configurations.
Now that PHP-FPM is installed and configured, you will need to configure Apache2 to work with PHP-FPM. This can be done by adding the following lines to your Apache2 configuration file (usually located at /etc/httpd/conf/httpd.conf
):
<FilesMatch \\.php$>
SetHandler "proxy:unix:/var/run/php-fpm.sock|fcgi://localhost"
</FilesMatch>
This configuration tells Apache2 to use PHP-FPM to handle any PHP files that are requested by a client. You will also need to enable the proxy_fcgi
and proxy
modules in Apache2 by adding the following lines to your configuration file:
LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so
LoadModule proxy_module modules/mod_proxy.so
Once you have made these changes, save the configuration file and restart Apache2 to apply the changes:
systemctl restart httpd
Step 5: Test the Configuration
To test your PHP-FPM configuration, create a new PHP file in the document root of your website (usually /var/www/html
) with the following contents:
<?php
phpinfo();
Save the file as phpinfo.php
, and then request it in your web browser by visiting http://www.example.com/phpinfo.php
. This should display the PHP information page, which shows the PHP version and configuration details.
If you see this page, then your PHP-FPM and Apache2 configuration is working correctly. If not, check the PHP-FPM and Apache2 logs for any errors or issues.
Conclusion
In this tutorial, we have shown you how to install and configure PHP-FPM with Apache2 on Rocky Linux. By using PHP-FPM, you can improve the performance and scalability of your PHP-based website, and easily manage and monitor the resource usage of your website.