How to run Apache with PHP-FPM on Ubuntu Linux

|

,

|

This tutorial outlines how to run PHP-FPM (FastCGI Process Manager) with an Apache2 HTTP server for enhanced benefits like adaptive process spawning, advanced process management, and improved performance, particularly for large, high-traffic websites. It provides detailed steps on installing and configuring Apache2 HTTP Server with PHP7.2 with FastCGI support, including dealing with common errors and…

This article explains how to run Apache with PHP-FPM on Ubuntu Linux.

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.

Running Apache with PHP-FPM on Ubuntu Linux can provide additional benefits, such as adaptive process spawning, basic statistics, and advanced process management with graceful stop/start, which can be helpful for busy websites.

PHP-FPM can also improve website performance significantly, especially for big and busy websites built on PHP. Following the steps in the tutorial, you can enable PHP-FPM support for your Apache2 HTTP server and enjoy these benefits.

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 it is 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 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. You should see the Apache2 default test page, as shown below. When you see that, then Apache2 is working as expected.

http://localhost

You may have to install the Apache2 Fast-CGI module manually. The commands below will cause an 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-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 close.

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…

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

Enjoy!

Like this:



19 responses to “How to run Apache with PHP-FPM on Ubuntu Linux”

  1. bhavin Avatar
    bhavin

    Many thanks for the blog

    1. Acácio Antunes Avatar
      Acácio Antunes

      Thanks
      Very good

  2. ERic Avatar
    ERic

    Thanks Sir, very clear. God Bless.

  3. Aline Matos Avatar
    Aline Matos

    Thank you!

  4. Ronald Avatar
    Ronald

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

  5. Mehmet Avatar
    Mehmet

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

  6. Chris Richardson Avatar
    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. Andy Avatar
    Andy

    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. Random Guy Avatar
    Random Guy

    THANK YOU SO SO SO MUCH!

  9. Sasindu Avatar
    Sasindu

    Thank you. That helps a lot.

  10. rinchi Avatar
    rinchi

    doesnt work 🙁

  11. Roberto Avatar
    Roberto

    Thank you man!

  12. Volkan Celebi Avatar
    Volkan Celebi

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

    Not working for me

  14. tiago pereira Avatar
    tiago pereira

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

  15. Khalid Avatar
    Khalid

    Many thanks!!
    It was useful for me !

  16. Luke R Avatar
    Luke R

    Thank you, super helpful!

  17. aRNoLD Avatar
    aRNoLD

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

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

Leave a Reply to Saurabh 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.