How to Install PHP-FPM with Apache on Ubuntu 22.04

How to Install PHP-FPM with Apache on Ubuntu 22.04

Posted on

Methods to set up PHP-FPM with Apache on Ubuntu 22.04. There are two distinct choices to run PHP utilizing the online server. One is utilizing the PHP’s CGI and the opposite one is FPM.

FPM is a course of supervisor to handle the FastCGI in PHP. Apache ships with mod_php by default and works with all main internet servers. With mod_php there’s a little efficiency challenge as a result of it locks out the method.

You can even configure PHP-FPM swimming pools to run because the totally different person that owns the web site if you’re internet hosting a number of web sites in your server in a chroot environment setup.

On this information you might be learn to setup PHP 8.1-FPM and configure it with Apache and in addition setup PHP 8.1-FPM swimming pools for a number of customers.

Getting Began

Make certain your Ubuntu server is having the newest packages by working the next command.

sudo apt replace
sudo apt improve

This may replace the bundle index and replace the put in packages to the newest model.

Set up PHP 8.1 for Apache

Execute the next command to put in PHP 8.1 with different vital modules.

sudo apt set up 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

As soon as the set up is full confirm the set up utilizing the next command.

sudo service php8.1-fpm standing

You’ll obtain an output much like the one beneath.

Output
● php8.1-fpm.service - The PHP 8.1 FastCGI Course of Supervisor
     Loaded: loaded (/lib/systemd/system/php8.1-fpm.service; enabled; vendor preset: enabled)
     Energetic: energetic (working) since Mon 2022-05-16 08:50:31 UTC; 1 day 18h in the past
       Docs: man:php-fpm8.1(8)
    Course of: 110200 ExecStartPost=/usr/lib/php/php-fpm-socket-helper set up /run/php/php-fpm.sock /and so on/php/8.1/fpm/pool.d/www.conf 81 (code=exited, standing=0/SUCC>
   Fundamental PID: 110197 (php-fpm8.1)
     Standing: "Processes energetic: 0, idle: 3, Requests: 57553, sluggish: 0, Site visitors: 0.1req/sec"
      Duties: 4 (restrict: 2353)
     Reminiscence: 484.3M
        CPU: 4h 42min 14.593s

Set up Apache

Upon getting your PHP-FPM up and working you’ll be able to set up Apache internet server.

sudo apt set up apache2

Configure Apache with PHP-FPM

By default Apache will use mod_php so now you’ll be able to configure Apache to make use of PHP-FPM.

Disable the default Apache vhost configuration.

sudo a2dissite 000-default

Allow proxy_fcgi module.

sudo a2enmod proxy_fcgi

Create a brand new Apache vhost configuration.

sudo nano /and so on/apache2/sites-available/cloudbooklet.conf

Paste the beneath configuration within the file.

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

     <Listing /var/www/html>
          Choices Indexes FollowSymLinks
         AllowOverride All
         Require all granted
     </Listing>

     <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}/entry.log mixed  
</VirtualHost> 

Hit CTRL + X adopted by Y and Enter to avoid wasting and exit the file.

Now you’ll be able to allow the brand new Apache configuration.

sudo a2ensite cloudbooklet.conf

Restart Apache.

sudo service apache2 restart

Take a look at PHP-FPM with Apache

Right here we have now configured /var/www/html because the webroot within the Apache configuration. So now you’ll be able to navigate into that listing and create a phpinfo file to examine the setup.

cd /var/www/html
sudo nano data.php

Paste the next.

<?php phpinfo;

Hit CTRL + X adopted by Y and Enter to avoid wasting and exit the file.

Now go your browser and level it to your server IP tackle or area title adopted by the data.php. So your tackle will appear like this http://IP_Address/data.php

You will note the PHP data web page and ensure PHP-FPM is used with Apache.

PHP-FPM Configuration

PHP INI: /and so on/php/8.1/fpm/php.ini

Pool config: /and so on/php/8.1/fpm/pool.d/www.conf

Create New PHP-FPM Pool with totally different person

By default Nginx and Apache runs as www-data person. PHP-FPM www.conf can also be configured to run as www-data person. When you have a number of web sites and want to hold them remoted with chrooted setup and run them with their very own person. You’ll be able to create a number of PHP-FPM swimming pools with totally different customers.

Create a brand new PHP-FPM pool configuration.

sudo nano /and so on/php/8.1/fpm/pool.d/user1.conf

Paste the next.

[user1]
person = user1
group = group1

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

hear.proprietor = www-data
hear.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 brand new Apache vhost configuration file and set the handler to the brand new pool you’ve created.

sudo nano /and so on/apache2/sites-available/site1.conf
<VirtualHost *:80>
     ServerName area.com
     DocumentRoot /var/www/html/site1

     <Listing /var/www/html/site1>
          Choices Indexes FollowSymLinks
         AllowOverride All
         Require all granted
     </Listing>

     <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}/entry.log mixed  
</VirtualHost> 

Allow new web site.

sudo a2ensite site1.conf

Make certain the brand new web site webroot is owned by the person specified within the pool configuration and inside the desired group.

Restart Providers

As soon as the configuration is accomplished it’s essential restart PHP-FPM and Apache for the adjustments to take impact.

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

You’ll be able to create as many swimming pools you want utilizing the above talked about setup with PHP-FPM. These are the pliability accessible with PHP-FPM.

Put together your self for a task working as an Information Technology Professional with Linux working system

Conclusion

Now you’ve discovered find out how to set up PHP 8.1-FPM with Apache and configure Apache to make use of PHP-FPM on Ubuntu 22.04.

You may have additionally discovered to setup PHP-FPM swimming pools for a number of customers.

offshore vps