How to Run Apache2 with PHP7.2-FPM on Ubuntu 16.04 | 17.10 | 18.04

You most likely will see webmasters and others run PHP-FPM with Nginx. However, if you want to get that additional benefits that PHP-FPM provides, this tutorial is going to show you how to run PHP-FPM with Apache2 HTTP server.

PHP-FPM (FastCGI Process Manager) is an alternative to PHP FastCGI. It provides features like Adaptive process spawning, basic statistics, advanced process management with graceful stop/start which may be useful for really busy websites.

If you want to set up Apache2 HTTP server with PHP-FPM support and enjoy those benefits, the steps below should be a great place to start.

Step 1: Install Apache2 HTTP Server

If you don’t already know, Apache2 HTTP server is the most popular HTTP server in use today. with PHP-FPM support, you may be able to improve your website performance greatly, and especially useful for big and busy websites build on PHP.

To install Apache2 with PHP7.2 with FastCGI support, run the commands below

sudo apt update 
sudo apt install apache2

After installing Apache2, the commands below can be used to stop, start and enable Apache2 to always start up when the server boots.

sudo systemctl stop apache2.service
sudo systemctl start apache2.service
sudo systemctl enable apache2.service

To test Apache2 setup, open your browser and browse to the server hostname or IP address and you should see Apache2 default test page as shown below. When you see that, then Apache2 is working as expected.

apache2 ubuntu install

To install Apache2 FastCGI module, you may have to manually install it. The commands below will error out. that the package has not installation candidate. continue below to install manually.

sudo apt-get install libapache2-mod-fastcgi

Package libapache2-mod-fastcgi is not available, but is referred to by another package.
This may mean that the package is missing, has been obsoleted, or
is only available from another source

E: Package 'libapache2-mod-fastcgi' has no installation candidate

In order to add that package, download it and install it manually.

cd /tmp && wget 
sudo dpkg -i libapache2-mod-fastcgi_2.4.7~0910052141-1.2_amd64.deb; sudo apt install -f

Step 2: Install PHP7.2 PHP-7.2-FPM

PHP 7.2 isn’t available on Ubuntu default repositories. in order to install it, you will have to get it from third-party repositories.

Run the commands below to add the below third party repository to upgrade to PHP 7.2

sudo apt-get install software-properties-common
sudo add-apt-repository ppa:ondrej/php

Then update and upgrade to PHP 7.2

sudo apt update

Next, run the commands below to install PHP 7.2 and related modules.

sudo apt install php7.2 php7.2-fpm php7.2-common

After installing PHP 7.2, run the commands below to open PHP default config file for Apache2.

sudo nano /etc/php/7.2/fpm/php.ini

Then make the changes on the following lines below in the file and save. The value below are great settings to apply in your environments.

file_uploads = On
allow_url_fopen = On
memory_limit = 256M
upload_max_filesize = 100M
max_execution_time = 360
date.timezone = America/Chicago

After making the change above, save the file and close out.

Step 3: Configure Apache2 HTTP to use PHP7.2-FPM

Now that both Apache2 and PHP7.2-FPM are installed, run the commands the commands below to enable FastCGI module for Apache2

sudo a2enmod actions fastcgi alias proxy_fcgi

Next, open Apache2 default configuration file and add the following highlighted lines, then save the file and close out.

sudo nano /etc/apache2/sites-available/000-default.conf

Configure the file as shown below:

<VirtualHost *:80>
     ServerAdmin [email protected]
     DocumentRoot /var/www/html

     <Directory /var/www/html/wordpress/>
        Options +FollowSymlinks
        AllowOverride All
        Require all granted
    <FilesMatch \.php$>
     SetHandler "proxy:unix:/var/run/php/php7.2-fpm.sock|fcgi://localhost/"

     ErrorLog ${APACHE_LOG_DIR}/error.log
     CustomLog ${APACHE_LOG_DIR}/access.log combined


Save and close out.

Step 4: Restart Apache2 and Test

After the configurations above, run the commands below to restart Apache2.

sudo systemctl restart apache2.service

To test PHP 7.2 settings with Apache2, create a phpinfo.php file in Apache2 root directory by running the commands below

sudo nano /var/www/html/phpinfo.php

Then type the content below and save the file.

<?php phpinfo( ); ?>

Save the file. then browse to your server hostname followed by /phpinfo.php

You should see PHP default test page…

apache2, ubuntu

That’s it! This is how you use Apache2 with PHP-FPM (FastCGI)


You may also like the post below: