How to Run Apache with PHP-FPM on Ubuntu Linux

Computer monitor with security lock icon
Computer monitor with security lock icon

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

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

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

Install Apache2 HTTP Server

If you don’t already know, the Apache HTTP server is the most popular used today. However, with PHP-FPM support, you may be able to improve your website performance significantly, and especially useful for big and busy websites built 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 stop, start and enable Apache2 always to start up when the server boots.

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

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

http://localhost
apache2 ubuntu install

To install the Apache2 Fast-CGI module, you may have to install it manually. The commands below will error that the package has no installation candidate.

sudo apt-get install libapache2-mod-fastcgi

You may get the error below after running the commands above.

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

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

Install PHP7.2 PHP-7.2-FPM

PHP 7.2 isn’t available on Ubuntu’s default repositories. 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 the PHP default config file for Apache2.

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

Then save the changes on the following lines below in the file. The value below is an ideal setting to apply in your environment.

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, please save the file and close it.

Configure Apache2 HTTP to use PHP7.2-FPM

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

sudo a2enmod actions fastcgi alias proxy_fcgi

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

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

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 the 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 the PHP default test page…

apache2, ubuntu

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

Enjoy!

Posted by
Richard

I love computers; maybe way too much. What I learned I try to share at geekrewind.com.

19 Comments

  1. Many thanks for the blog

    1. Acácio Antunes

      Thanks
      Very good

  2. Thanks Sir, very clear. God Bless.

  3. Thank you!

  4. Best guide on this found. Just saved a world of pain!

  5. Thank you.. Nice guide. Everyting what I need 😉

  6. Chris Richardson

    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.

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

  8. THANK YOU SO SO SO MUCH!

  9. Thank you. That helps a lot.

  10. doesnt work 🙁

  11. Thank you man!

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

  13. Not working for me

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

  15. Many thanks!!
    It was useful for me !

  16. Thank you, super helpful!

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

  18. how to enable php display error ??
    bythe ways thanks this time its work.

Leave a Reply

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

%d bloggers like this: