Configure a separate PHP-FPM pool for each vhost. This allows each vhost to have its own isolated PHP-FPM process, which communicates over its own unique socket

By technese - Last updated: Monday, November 11, 2024 - Save & Share - Leave a Comment

To create a separate FastCGI socket for each virtual host (vhost), you’ll typically need to configure a separate PHP-FPM pool for each vhost. This allows each vhost to have its own isolated PHP-FPM process, which communicates over its own unique socket. Here’s how you can set it up:

1. Configure Separate PHP-FPM Pools

PHP-FPM allows you to define multiple pools, each with unique settings, including its own socket. By creating a separate pool for each vhost, you can control resources independently and enhance security and isolation.

  1. Locate the PHP-FPM Pool Configuration Directory:
  1. Create a New Pool Configuration File for Each Vhost:
  1. Edit Each Pool Configuration File:
  1. Repeat for Each Vhost:
  1. Restart PHP-FPM:

2. Configure Apache for Each Vhost to Use the Correct FastCGI Socket

In your Apache configuration, you need to configure each vhost to use its respective FastCGI socket.

  1. Define Each Vhost in Apache (in /etc/apache2/sites-available/):
  1. Example Apache Configuration:
   <VirtualHost *:80>
       ServerName vhost1.example.com

       # FastCGI configuration for vhost1
       FastCgiExternalServer /var/www/vhost1/cgi-bin/php-fcgi -socket /var/run/php/php7.4-fpm-vhost1.sock -pass-header Authorization -pass-header Content-Type
       DocumentRoot /var/www/vhost1

       <Directory /var/www/vhost1>
           Options +ExecCGI
           Require all granted
       </Directory>
   </VirtualHost>

   <VirtualHost *:80>
       ServerName vhost2.example.com

       # FastCGI configuration for vhost2
       FastCgiExternalServer /var/www/vhost2/cgi-bin/php-fcgi -socket /var/run/php/php7.4-fpm-vhost2.sock -pass-header Authorization -pass-header Content-Type
       DocumentRoot /var/www/vhost2

       <Directory /var/www/vhost2>
           Options +ExecCGI
           Require all granted
       </Directory>
   </VirtualHost>
  1. Reload Apache:

Summary

By configuring a separate PHP-FPM pool and socket for each vhost, each site operates in isolation with its own dedicated FastCGI process. This setup not only enhances security but also allows for resource control per vhost, preventing one site from monopolizing resources at the expense of others.

Source: ChatGPT

Posted in General • • Top Of Page