We are committed to provide service 24 X 7

Deals, Shopping, Training, Tools

How to Install PHP-FPM with Apache on Ubuntu 22.04

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.

You can also configure PHP-FPM pools to run as the different user that owns the website if you are hosting multiple websites on your server in a chroot environment setup.

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

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.

Install PHP 8.1 for Apache

Execute the following command to install PHP 8.1 with other necessary modules.

sudo apt install php8.1 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

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 Mon 2022-05-16 08:50:31 UTC; 1 day 18h ago
       Docs: man:php-fpm8.1(8)
    Process: 110200 ExecStartPost=/usr/lib/php/php-fpm-socket-helper install /run/php/php-fpm.sock /etc/php/8.1/fpm/pool.d/www.conf 81 (code=exited, status=0/SUCC>
   Main PID: 110197 (php-fpm8.1)
     Status: "Processes active: 0, idle: 3, Requests: 57553, slow: 0, Traffic: 0.1req/sec"
      Tasks: 4 (limit: 2353)
     Memory: 484.3M
        CPU: 4h 42min 14.593s

Install Apache

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

sudo apt install apache2

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

sudo a2enmod proxy_fcgi

Create a new Apache vhost configuration.

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

Paste the below configuration in the file.

<VirtualHost *:80>
     ServerName External_IP_Address
     DocumentRoot /var/www/html

     <Directory /var/www/html>
          Options Indexes FollowSymLinks
         AllowOverride All
         Require all granted
     </Directory>

     <FilesMatch ".php$"> 
         SetHandler "proxy:unix:/var/run/php/php8.1-fpm.sock|fcgi://localhost/"          
      </FilesMatch>
 
      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 cloudbooklet.conf

Restart Apache.

sudo service apache2 restart

Test PHP-FPM with Apache

Here we have configured /var/www/html as the webroot 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
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://IP_Address/info.php

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

PHP-FPM Configuration

PHP INI: /etc/php/8.1/fpm/php.ini

Pool config: /etc/php/8.1/fpm/pool.d/www.conf

Create New PHP-FPM Pool with different user

By default Nginx and Apache runs as www-data user. PHP-FPM www.conf is also configured to run as www-data user. If you have multiple websites and wish to keep them isolated with chrooted setup and run them with their own user. You can create multiple PHP-FPM pools with different users.

Create a new PHP-FPM pool configuration.

sudo nano /etc/php/8.1/fpm/pool.d/user1.conf

Paste the following.

[user1]
user = user1
group = group1

listen = /run/php/php8.1-fpm-user1.sock

listen.owner = www-data
listen.group = www-data

pm = dynamic
pm.max_children = 5
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 3

Now create a new Apache vhost configuration file and set the handler to the new pool you have created.

sudo nano /etc/apache2/sites-available/site1.conf
<VirtualHost *:80>
     ServerName domain.com
     DocumentRoot /var/www/html/site1

     <Directory /var/www/html/site1>
          Options Indexes FollowSymLinks
         AllowOverride All
         Require all granted
     </Directory>

     <FilesMatch ".php$"> 
         SetHandler "proxy:unix:/var/run/php/php8.1-fpm-user1.sock|fcgi://localhost/"          
      </FilesMatch>
 
      ErrorLog ${APACHE_LOG_DIR}/error.log
      CustomLog ${APACHE_LOG_DIR}/access.log combined  
</VirtualHost> 

Enable new site.

sudo a2ensite site1.conf

Make sure the new site webroot is owned by the user specified in the pool configuration and inside the specified group.

Restart Services

Once the configuration is completed you need to restart PHP-FPM and Apache for the changes to take effect.

sudo php-fpm8.1 -t
sudo service php8.1-fpm restart
sudo service apache2 restart

You can create as many pools you like using the above mentioned setup with PHP-FPM. These are the flexibility available with PHP-FPM.

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 to use PHP-FPM on Ubuntu 22.04.

You have also learned to setup PHP-FPM pools for multiple users.

Cart

Your Cart is Empty

Back To Shop