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..

http://localhost

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 http://mirrors.kernel.org/ubuntu/pool/multiverse/liba/libapache-mod-fastcgi/libapache2-mod-fastcgi_2.4.7~0910052141-1.2_amd64.deb
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 admin@example.com
     DocumentRoot /var/www/html
     ServerName example.com
     ServerAlias www.example.com

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

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

</VirtualHost>

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

http://example.com/phpinfo.php

You should see PHP default test page…

apache2, ubuntu

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

Enjoy!

You may also like the post below:

Samba Setup on Ubuntu 16.04 / 17.10 / 18.04 with Windows Systems

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

  1. Is there any downsides to putting the

    SetHandler “proxy:unix:/var/run/php/php7.2-fpm.sock|fcgi://localhost/”

    in apache2.conf…I mean it works just wondering if there are any downsides.

  2. Nice write-up! I too have the same question as Chris Richardson, “is there any downside to putting the “SetHandler “proxy:unix:/var/run/php/php7.2-fpm.sock|fcgi://localhost/”” in apache2.conf as opposed to in all of the vhost config files? I have a server with about 20 vhosts on it and I was wondering if this SetHandler could just be dropped into the Apache config once, as opposed to duplicated into all the conf files in sites-available. Thanks!

  3. sudo apt install php7.2 php7.2-fpm php7.2-common
    Reading package lists… Done
    Building dependency tree
    Reading state information… Done
    E: Unable to locate package php7.2
    E: Couldn’t find any package by glob ‘php7.2’
    E: Couldn’t find any package by regex ‘php7.2’
    E: Unable to locate package php7.2-fpm
    E: Couldn’t find any package by glob ‘php7.2-fpm’
    E: Couldn’t find any package by regex ‘php7.2-fpm’
    E: Unable to locate package php7.2-common
    E: Couldn’t find any package by glob ‘php7.2-common’
    E: Couldn’t find any package by regex ‘php7.2-common’

  4. Don’t need apt update when use add-apt-repository!! now add-apt-repository make a apt update automatically!

  5. Seems not with Debian Buster/10, fastcgi is replaced by cgid. After apt installing php7.3-fpm, “a2enmod cgid proxy proxy_fcgi” will do the job.

Leave a Reply to Chris Richardson Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.