How to Install PHP-FPM with Apache on Ubuntu 22.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.

Pinterest LinkedIn Tumblr

How to install PHP-FPM with Apache on Ubuntu 22.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: Install PHP 8.1 FPM

By default Ubuntu 22.04 has the latest PHP 8.1 repository added. So you can install PHP using the following command.

sudo apt install php8.1-fpm php8.1 libapache2-mod-php8.1 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-imap php8.1-mbstring php8.1-opcache php8.1-soap php8.1-zip php8.1-intl php8.1-bcmath unzip -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.

● 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-05-08 05:42:31 UTC; 2min ago
       Docs: man:php-fpm8.1(8)
    Process: 564 ExecStartPost=/usr/lib/php/php-fpm-socket-helper install /run/php/php-fpm.s>

Step 2: Install Apache

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

sudo apt install apache2

Check Apache version using the below command

sudo apachectl -v
Server version: Apache/2.4.52 (Ubuntu)
Server built:   2022-03-25T00:35:40

Every process in Apache is managed with the systemctl command. Check the status of Apache with the following command.

sudo systemctl status apache2
● apache2.service - The Apache HTTP Server
     Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)
     Active: active (running) since Fri 2022-05-08 05:45:29 UTC; 1min 47s ago
   Main PID: 12782 (apache2)
      Tasks: 55 (limit: 1151)
     Memory: 5.1M
        CPU: 52ms
     CGroup: /system.slice/apache2.service
             ├─12782 /usr/sbin/apache2 -k start
             ├─12784 /usr/sbin/apache2 -k start
             └─12785 /usr/sbin/apache2 -k start

Step 3: 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 4: 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 5: 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>
     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
      ErrorLog ${APACHE_LOG_DIR}/error.log
      CustomLog ${APACHE_LOG_DIR}/access.log combined  

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 6: 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

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


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

Write A Comment

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