Apache and Nginx are free, open-source, and popular – the most widely used web servers worldwide. Apache and Nginx both run on all Unix-based operating systems. Apache is known for its power, while Nginx is known for its speed. Nginx is also used as a reverse proxy for HTTP, HTTPS, IMAP, SMTP, and POP3 and as a load balancer.

PHP-FPM (FastCGI Process Manager) is an alternative PHP FastCGI implementation specially designed for high-loaded websites. PHP-FPM allows you to run multiple versions of PHP at a time. PHP-FPM can be run differently than mod_PHP on a web server. If you want to host your web application with optimal performance, then PHP-FPM is the best choice.

This tutorial will explain how to enable PHP-FPM support on Apache and Nginx web servers on Ubuntu. This procedure is compatible with Ubuntu 18.04, Ubuntu 20.04, and Ubuntu 22.04.

Step 1 – Enable PHP-FPM Support on the Apache Web Server

This section will teach us how to install and enable PHP-FPM support on the Apache webserver.

First, install the Apache and PHP-FPM by running the following command:

apt-get update -y
apt-get install apache2 libapache2-mod-php libapache2-mod-fcgid php php-fpm php-cli -y

Once all the packages are installed, start Apache and PHP-FPM service with the following command:

systemctl start apache2
systemctl start php7.2-fpm

Note: Replaced php7.2-fpm with your installed PHP version.

Next, you will need to configure the Apache webserver with PHP-FPM support. To do so, create a new Apache virtual host configuration file:

nano /etc/apache2/sites-available/example.com.conf

Add the following lines:

<VirtualHost *:80>
     ServerAdmin [email protected]
     DocumentRoot /var/www/html/
     DirectoryIndex info.php
     ServerName example.com

     <Directory /var/www/html/>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        Order allow,deny
        allow from all
     </Directory>

    <FilesMatch \.php$>
        # 2.4.10+ can proxy to unix socket
         SetHandler "proxy:unix:/run/php/php7.2-fpm.sock|fcgi://localhost"
    </FilesMatch>

     ErrorLog ${APACHE_LOG_DIR}/example.com_error.log
     CustomLog ${APACHE_LOG_DIR}/example.com_access.log combined
</VirtualHost>

Note: Replaced php7.2-fpm with your installed PHP version.

Save and close the file. Then, enable the virtual host configuration file with the following command:

a2ensite example.com

Next, you will need to enable a few modules for apache2 to work with PHP-FPM:

a2enmod actions fcgid alias proxy_fcgi

Next, restart the Apache service using the following command:

systemctl restart apache2

Step 2 – Test Apache Web Server

Apache webserver is now configured with PHP-FPM support. It’s time to test whether PHP-FPM is loaded with Apache webserver or not.

To test it, create a sample info.php file inside the Apache document root directory:

nano /var/www/html/info.php

Add the following lines:

<?php
    phpinfo();
?>

Save and close the file, then change the ownership of the info.php file to www-data:

chown www-data:www-data /var/www/html/info.php

Next, open your web browser and type the URL http://example.com. You should see the following page:

The above page indicates that PHP-FPM is loaded with the Apache webserver.

Note: Don’t forget to remove the info.php file after testing.

Step 3 – Enable PHP-FPM Support on the Nginx Web Server

This section will teach us how to install and enable PHP-FPM support on the Nginx webserver.

Step 4 – Install Nginx and PHP-FPM

First, install Nginx and PHP-FPM by running the following command:

apt-get install nginx php php-fpm php-cli -y

Once all the packages are installed, start Nginx and PHP-FPM service with the following command:

systemctl start nginx
systemctl start php7.2-fpm

Step 5 – Configure Nginx with PHP-FPM Support

Next, you will need to configure the Nginx webserver with PHP-FPM support. To do so, create a new Nginx virtual host configuration file:

nano /etc/nginx/sites-available/example.com.conf

Add the following lines:

server {
        listen 80;
        root /var/www/html/;
        index info.php;
        server_name example.com;

        location ~ \.php$ {
          fastcgi_split_path_info ^(.+\.php)(/.+)$;
          fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
          fastcgi_index index.php;
          include fastcgi_params;
          fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
          fastcgi_intercept_errors off;
          fastcgi_buffer_size 16k;
          fastcgi_buffers 4 16k;
          fastcgi_connect_timeout 600;
          fastcgi_send_timeout 600;
          fastcgi_read_timeout 600;
        }

   location / {
       try_files $uri $uri/ =404;
   }

}

Note: Replaced php7.2-fpm with your installed PHP version.

Save and close the file. Then, enable the Nginx virtual host with the following command:
ln -s /etc/nginx/sites-available/example.com.conf /etc/nginx/sites-enabled/

Next, restart the Nginx and PHP-FPM service to apply the configuration changes:

systemctl restart nginx
systemctl restart php7.2-fpm

Step 6 – Test Nginx Web Server

The Nginx webserver is now configured with PHP-FPM support. It’s time to test whether PHP-FPM is loaded with the Nginx webserver or not.

To test it, create a sample info.php file in the Nginx document root directory:

nano /var/www/html/info.php

Add the following lines:

<?php
    phpinfo();
?>

Save and close the file, then change the ownership of the info.php file to www-data:

chown www-data:www-data /var/www/html/info.php

Next, open your web browser and type the URL http://example.com. You should see the following page:

The above page indicates that PHP-FPM is loaded with the Nginx webserver.

Note: Don’t forget to remove the info.php file after testing.

Conclusion

Congratulations! You have successfully configured Nginx and Apache web servers with PHP-FPM support. I hope you now have enough knowledge to use PHP-FPM to run multiple versions of PHP at a time. To start with PHP-FPM on Apache and Nginx, sign up for a VPS Hosting plan with Atlantic.Net today.