Editing the php.ini File for a Domain Using PHP-FPM

Editing the php.ini file for a domain using PHP-FPM (PHP FastCGI Process Manager) involves several steps, including locating the correct php.ini file, making the necessary changes, and ensuring that PHP-FPM recognizes and applies these changes. This guide provides a comprehensive overview of the process.

Understanding PHP-FPM

PHP-FPM is an alternative PHP FastCGI implementation with additional features for handling heavy-loaded websites. It allows multiple PHP versions to run simultaneously, and configurations can be tailored for each domain or pool.

Steps to Edit the php.ini File for a Domain Using PHP-FPM

 

  1. Identify the Correct php.ini File

 

By default, PHP-FPM uses a global php.ini file, typically located at

/etc/php/{version}/fpm/php.ini. However, it’s common to have different configurations for different domains or pools.

 

  1. Locate the Pool Configuration File:

   Pool configuration files are usually found in the /etc/php/{version}/fpm/pool.d/ directory, with filenames ending in .conf. For example, example.com.conf.

 

  

   /etc/php/7.4/fpm/pool.d/example.com.conf

  

 

  1. Check for Pool-Specific php.ini:

   Sometimes, a pool-specific php.ini file is included within the pool configuration. Look for directives like php_admin_value or php_admin_flag in the pool configuration file to identify custom settings for that domain.

 

  1. Editing the php.ini File

 

To modify PHP settings for a specific domain:

 

  1. Open the Pool Configuration File:

   Use a text editor to open the pool configuration file for your domain. For example, using nano:

 

 

   sudo nano /etc/php/7.4/fpm/pool.d/example.com.conf

  

 

  1. Add/Modify php.ini Settings:

   Add or modify the php_admin_value and php_admin_flag directives to set PHP configuration values specific to this pool. For example:

   ini

   [example.com]

   user = www-data

   group = www-data

   listen = /run/php/php7.4-fpm-example.com.sock

 

   php_admin_value[upload_max_filesize] = 50M

   php_admin_value[post_max_size] = 55M

   php_admin_flag[display_errors] = off

   php_admin_value[memory_limit] = 128M

  

 

   In this example:

   – upload_max_filesize is set to 50M.

   – post_max_size is set to 55M.

   – display_errors is turned off.

   – memory_limit is set to 128M.

 

  1. Save and Close the File:

   Save the changes and close the text editor. In nano, you can save by pressing Ctrl + O, then exit by pressing Ctrl + X.

 

  1. Reload PHP-FPM

 

After editing the php.ini settings, you need to reload PHP-FPM to apply the changes.

 

  1. Reload PHP-FPM Service:

 

 

   sudo systemctl reload php7.4-fpm

  

 

   This command reloads the PHP-FPM service, applying the new configuration without dropping active connections.

 

  1. Verify the Changes

 

To ensure that the new settings have been applied correctly:

 

  1. Create a PHP Info File:

   Create a phpinfo.php file in the root directory of the domain (e.g., /var/www/example.com/public_html/phpinfo.php):

 

   php

   <?php

   phpinfo();

   ?>

  1. Access the PHP Info File:

Open a web browser and navigate to http://example.com/phpinfo.php. This file displays all the PHP settings currently in effect. Look for the specific settings you changed to verify they are correctly applied.

  1. Delete the PHP Info File:

   For security reasons, delete the phpinfo.php file after verification:

   rm /var/www/example.com/public_html/phpinfo.php

Additional Considerations

Custom php.ini Files for Specific Directories

 

If you need to apply specific PHP settings to a subdirectory within a domain, you can use .user.ini files. These files allow per-directory PHP configuration overrides.

 

  1. Create a .user.ini File:

   Create a .user.ini file in the desired directory:

   ini

   upload_max_filesize = 20M

   post_max_size = 25M

 

  1. Ensure PHP Reads .user.ini Files:

   PHP-FPM must be configured to read .user.ini files. This is typically enabled by default but can be verified in the main php.ini file:

   ini

   user_ini.filename = “.user.ini”

   user_ini.cache_ttl = 300

 

Security and Best Practices

 

– Backup Configuration Files: Always backup configuration files before making changes.

– Restrict File Permissions: Ensure that configuration files have appropriate permissions to prevent unauthorized access or modification.

– Monitor Logs: Regularly monitor PHP-FPM and web server logs to detect any issues or suspicious activities.

 

By following these steps, you can effectively edit the php.ini file for a domain using PHP-FPM, ensuring that each domain has tailored PHP settings that meet its specific requirements. Proper configuration and regular monitoring are key to maintaining a secure and efficient web environment.