Linux

How to Install PHP-FPM with Apache on Ubuntu 20.04

Disclosure: This post may contain affiliate links, which means we may receive a commission if you click a link and purchase something that we recommended.

How to install PHP-FPM with Apache on Ubuntu 20.04. There are two distinct options to run PHP using the web server. One is using the PHP’s CGI and the other one is FPM. FPM is a process manager to manage the FastCGI in PHP. Apache ships with mod_php by default and works with all major web servers. With mod_php there is a little performance issue because it locks out the process.

In this guide you are learn how to setup PHP 8.1-FPM and configure it with Apache and also configure PHP variables.

This setup can also be done on other VPS or dedicated or cloud VM’s. This setup is tested on Google Compute Engine but it works on any Ubuntu or Debian Linux distributions.

Getting Started

Make sure your Ubuntu server is having the latest packages by running the following command.

sudo apt update
sudo apt upgrade

This will update the package index and update the installed packages to the latest version.

Step 1: Add PPA for PHP 8.1

Add the ondrej/php which has PHP 8.1 package and other required PHP extensions.

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

Once you have added the PPA you can install PHP 8.1.

Step 2: Install PHP 8.1 FPM

Now we shall install PHP 8.1-FPM and some common modules to run a PHP application like WordPress.

sudo apt install php8.1-fpm php8.1-common php8.1-mysql php8.1-xml php8.1-xmlrpc php8.1-curl php8.1-gd php8.1-imagick php8.1-cli php8.1-dev php8.1-imap php8.1-mbstring php8.1-soap php8.1-zip php8.1-bcmath -y

Wait for the installation to complete.

Once the installation is complete verify the installation using the following command.

sudo service php8.1-fpm status

You will receive an output similar to the one below.

Output
● php8.1-fpm.service - The PHP 8.1 FastCGI Process Manager
     Loaded: loaded (/lib/systemd/system/php8.1-fpm.service; enabled; vendor preset: enabled)
     Active: active (running) since Tue 2022-02-22 04:51:01 UTC; 3min ago
       Docs: man:php-fpm8.1(8)
    Process: 564 ExecStartPost=/usr/lib/php/php-fpm-socket-helper install /run/php/php-fpm.s>
   Main PID: 471 (php-fpm8.1)
     Status: "Processes active: 0, idle: 10, Requests: 1068, slow: 0, Traffic: 0req/sec"
      Tasks: 11 (limit: 9529)
     Memory: 1.2G
     CGroup: /system.slice/php8.1-fpm.service

Step 3: Install Apache

Once you have your PHP-FPM up and running you can install Apache web server.

sudo apt install apache2

Step 4: Configure Apache with PHP-FPM

By default Apache will use mod_php so now you can configure Apache to use PHP-FPM.

Disable the default Apache vhost configuration.

sudo a2dissite 000-default

Enable Apache Event Module

Disable any default preform modules for any existing PHP versions.

sudo a2dismod php7.4

Disable Apache Prefork module.

sudo a2dismod mpm_prefork

Enable Apache Event module.

sudo a2enmod mpm_event proxy_fcgi setenvif

Enable PHP FPM Configuration

Now you can enable PHP-FPM configuration.

sudo a2enconf php8.1-fpm

Enable HTTP2

If you need HTTP 2 support you can enable it also.

sudo a2enmod http2

Now Apache is configured with PHP FPM.

Step 5: Configure PHP-FPM

Now we configure PHP for Web Applications by changing some values in php.ini file.

For PHP 8.1 with Nginx the php.ini location will be in following directory.

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

Hit F6 for search inside the editor and update the following values for better performance.

upload_max_filesize = 32M 
post_max_size = 48M
memory_limit = 256M
max_execution_time = 600
max_input_vars = 3000
max_input_time = 1000

Once you have modified your PHP settings you need to restart your PHP-FPM for the changes to take effect.

sudo service php8.1-fpm restart

Step 6: Configure Apache Virtual Hosts

Create a new Apache vhost configuration.

sudo nano /etc/apache2/sites-available/domain.conf

Paste the below configuration in the file.

<VirtualHost *:80>
     ServerName domain.com
     ServerAlias www.domain.com
     
     Protocols h2 http/1.1

     DocumentRoot /var/www/html/domain/public

     <Directory /var/www/html/domain/public>
         Options -Indexes +FollowSymLinks
         AllowOverride All
         Require all granted
     </Directory>
 
      ErrorLog ${APACHE_LOG_DIR}/error.log
      CustomLog ${APACHE_LOG_DIR}/access.log combined  
</VirtualHost> 

Hit CTRL + X followed by Y and Enter to save and exit the file.

Now you can enable the new Apache configuration.

sudo a2ensite domain.conf

Restart Apache.

sudo service apache2 restart

Step 7: Verify PHP-FPM with Apache

Here we have configured /var/www/html/domain/public as the web root in the Apache configuration. So now you can navigate into that directory and create a phpinfo file to check the setup.

cd /var/www/html/domain/public
sudo nano info.php

Paste the following.

<?php phpinfo();

Hit CTRL + X followed by Y and Enter to save and exit the file.

Now go your browser and point it to your server IP address or domain name followed by the info.php. So your address will look like this http://domain.com/info.php

You will see the PHP info page and confirm PHP-FPM is used with Apache.

Prepare yourself for a role working as an Information Technology Professional with Linux operating system

Conclusion

Now you have learned how to install PHP 8.1-FPM with Apache and configure Apache. You have also learned to setup PHP-FPM pools for multiple users.

4 Comments

  1. After following this process my WordPress instance is broken.
    Can’t make it work.
    Just restored from backup.

  2. Keith Tachibana Reply

    Hugo is kinda correct – you do have a glaring error on the very last part when you write the code for info.php. It should be phpinfo() and then you need the closing angle bracket and question mark, like this: ?>. The omission of the parenthesis means it doesn’t run as an executable which is what you want. Once you do that and add the closing angle brackets, then the PHP info populates like it should; otherwise you’ll get an error message in the Apache error log stating: “Got error ‘PHP message: PHP Fatal error: Uncaught Error: Undefined constant “phpinfo” in /var/www/html/info.php:1\nStack trace:\n#0 {main}\n thrown in /var/www/html/info.php on line 1’ “

Write A Comment

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.