Jan 20, 2020 8:30 AM

Configuring Apache to support PHP-FPM on Debian/Ubuntu (apache2 + php-fpm)

In this article I will explain how to configure the Apache server to support PHP-FPM on the Debian/Ubuntu operating system. PHP-FPM is a FastCGI (FCGI) process manager in PHP, commonly used for heavily loaded sites. This PHP integration method uses Nginx. But we can also use FastCGI in Apache to improve performance. PHP-FPM 7.4 is the latest stable version available for installation. You can also install PHP-FPM 7.3, PHP-FPM 7.2, PHP-FPM 7.1, PHP-FPM 7.0 with this instruction.

This instruction was executed on operating systems: Debian 10, Debian 9, Ubuntu 20.10, Ubuntu 20.04, Ubuntu 19.10.

Installing PHP-FPM

Before any software installation, it is recommended to update the list of repository packages by running the command in the terminal:

sudo apt-get update

Install PHP-FPM by running the command in the terminal:

sudo apt-get install php7.4-fpm

or

sudo apt-get install php-fpm

You can learn more about how to install PHP 7.x on Debian and how to install PHP 7.x on Ubuntu.

Installing PHP-FPM

Configuring Apache to support PHP-FPM

Before setting up Apache to work with the PHP handler, we find out its version by running the command in the terminal:

php -v

The Apache configuration file /etc/apache2/conf-available/php7.4-fpm.conf must be created after installing PHP-FPM. You can also create this file manually by running the command in the terminal:

sudo nano /etc/apache2/conf-available/php7.4-fpm.conf
# Redirect to local php-fpm if mod_php is not available
<IfModule !mod_php7.c>
<IfModule proxy_fcgi_module>
    # Enable http authorization headers
    <IfModule setenvif_module>
    SetEnvIfNoCase ^Authorization$ "(.+)" HTTP_AUTHORIZATION=$1
    </IfModule>

    <FilesMatch ".+\.ph(ar|p|tml)$">
        SetHandler "proxy:unix:/run/php/php7.4-fpm.sock|fcgi://localhost" # PHP version (php -v command)
    </FilesMatch>
    <FilesMatch ".+\.phps$">
        # Deny access to raw php sources by default
        # To re-enable it's recommended to enable access to the files
        # only in specific virtual host or directory
        Require all denied
    </FilesMatch>
    # Deny access to files without filename (e.g. '.php')
    <FilesMatch "^\.ph(ar|p|ps|tml)$">
        Require all denied
    </FilesMatch>
</IfModule>
</IfModule>

Save the changes and close the file.

You must activate the proxy_fcgi module by running the command in the terminal:

sudo a2enmod proxy_fcgi

Do not forget to activate the configuration file by running the command in the terminal:

sudo a2enconf php7.4-fpm

You need to disable the mod_php module so that requests are sent to FCGI by running a command in the terminal:

sudo a2dismod php7.4

Check the configuration for validity by running the command in the terminal:

sudo apache2 -t

Now apply the configuration changes by running the command in the terminal:

sudo service apache2 reload

Testing Apache with PHP-FPM support

Let's verify that Apache correctly transfers files with the .php extension to our PHP handler. Create a test file that will return information about our server by running the command in the terminal:

echo "<?php phpinfo(); ?>" | sudo tee /var/www/html/info.php

Let's make sure that the Apache server correctly displays the content generated by the PHP script by opening this page in the browser:

http://192.168.0.101/info.php

or for localhost

http://localhost/info.php

If you correctly configured Apache for PHP-FPM support, a similar page will be displayed:

Testing Apache with PHP-FPM support

After checking for security reasons, you need to delete this file by running the command in the terminal:

sudo rm /var/www/html/info.php

Other articles