Skip to main content

Integrating PHP with Apache

1. Configuring mod_php

Directive Values Description
LoadModule php_module modules/libphp.so Load the PHP module into Apache
AddHandler php-script .php Associate .php files with the PHP handler
DirectoryIndex index.php Set PHP files as default index files

Example: Basic PHP Configuration

<IfModule mod_php.so>
    LoadModule php_module modules/libphp.so
    AddHandler php-script .php
    DirectoryIndex index.php
</IfModule>
Note: Ensure that the path to libphp.so matches your PHP installation.

2. Setting Up PHP-FPM

Configuration Description Example
listen Socket or port for PHP-FPM to listen on 127.0.0.1:9000
user User to run PHP-FPM processes as www-data
group Group to run PHP-FPM processes as www-data
pm Process manager mode (static, dynamic, ondemand) dynamic
pm.max_children Maximum number of child processes 50

Example: PHP-FPM Configuration

; /etc/php/8.1/fpm/pool.d/www.conf
[www]
listen = 127.0.0.1:9000
user = www-data
group = www-data
pm = dynamic
pm.max_children = 50
pm.start_servers = 5
pm.min_spare_servers = 5
pm.max_spare_servers = 35

Example: Apache Configuration for PHP-FPM

<FilesMatch \.php$>
    SetHandler "proxy:fcgi://127.0.0.1:9000"
</FilesMatch>

3. Setting PHP Handler

Directive Values Description
AddType application/x-httpd-php .php Maps .php files to the PHP handler
AddHandler php-script .php Assigns the PHP handler to files with .php extension
ScriptAlias /php-cgi/ /usr/lib/cgi-bin/ Maps URL path to CGI directory for PHP scripts

Example: Configuring PHP Handler

<FilesMatch \.php$>
    SetHandler application/x-httpd-php
</FilesMatch>
Note: Ensure mod_php is installed and enabled in Apache to handle PHP files directly.

4. Configuring PHP-FPM Pools

Pool Name Configuration Directive Description Default Value
pm pm Process manager algorithm to use dynamic
pm.max_children pm.max_children Maximum number of child processes to spawn 50
pm.start_servers pm.start_servers Number of child processes to start at startup 5
pm.min_spare_servers pm.min_spare_servers Minimum number of idle processes to keep 5
pm.max_spare_servers pm.max_spare_servers Maximum number of idle processes to allow 35
pm.max_requests pm.max_requests Number of requests after which a child will be restarted 0

Example: PHP-FPM Pool Configuration

; /etc/php/8.1/fpm/pool.d/www.conf
[www]
user = www-data
group = www-data
listen = /run/php/php8.1-fpm.sock
pm = dynamic
pm.max_children = 50
pm.start_servers = 5
pm.min_spare_servers = 5
pm.max_spare_servers = 35
pm.max_requests = 500
Note: The pm.max_requests setting helps prevent memory leaks in long-running processes.
Warning: Setting pm.max_children too high can exhaust system resources.

5. Setting PHP Directives

Directive Values Description
php_value memory_limit, max_execution_time Sets PHP configuration values for the current directory and subdirectories
php_flag display_errors, log_errors Enables or disables specific PHP configuration flags
php_admin_value upload_max_filesize, post_max_size Sets PHP configuration values that cannot be overridden by user scripts
php_admin_flag expose_php, allow_url_fopen Enables or disables specific PHP configuration flags that cannot be overridden by user scripts

Example: Setting PHP Memory Limit

<Directory "/var/www/html">
    php_value memory_limit 256M
</Directory>

Example: Enabling Display Errors

<Files "index.php">
    php_flag display_errors on
</Files>
Note: php_admin_value and php_admin_flag are processed at the server level and cannot be overridden by PHP scripts.

6. Using .htaccess for PHP Settings

Directive Description Values Notes
php_value Sets PHP configuration values on, off, 1, 0 Can be used to set PHP settings like display_errors
php_flag Sets PHP configuration flags on, off Used for boolean settings like short_open_tag
php_admin_value Sets PHP configuration values in the admin context Any valid value for the setting Cannot be overridden by user scripts
php_admin_flag Sets PHP configuration flags in the admin context on, off Cannot be overridden by user scripts

Example: Setting PHP Configuration in .htaccess

<IfModule mod_php>
    php_flag display_errors on
    php_value error_log /path/to/php_errors.log
    php_admin_flag short_open_tag off
</IfModule>
Note: The .htaccess file must be placed in the directory where PHP files are located or in a parent directory to affect those files.

7. Configuring PHP Timeout Values

Directive Default Value Description
php_value max_execution_time 30 Maximum time in seconds a script is allowed to run. Setting to 0 disables the limit.
php_value max_input_time -1 Maximum time in seconds allowed for parsing input data. -1 means no limit.
php_value memory_limit 128M Maximum amount of memory a script may consume. Set to -1 for unlimited.
php_value post_max_size 8M Maximum size of POST data that PHP will accept. Should be larger than upload_max_filesize.
php_value upload_max_filesize 2M Maximum size of a single uploaded file. Must be less than or equal to post_max_size.

Example: Setting PHP Timeouts in .htaccess

<IfModule mod_php>
    php_value max_execution_time 60
    php_value max_input_time 120
    php_value memory_limit 256M
</IfModule>
Note: These directives can also be set in php.ini, httpd.conf, or via ini_set() in PHP scripts.

8. Setting PHP Error Reporting

Directive Values Description
error_reporting E_ALL or E_ALL & ~E_NOTICE Sets which errors are reported. E_ALL reports all errors, warnings, and notices.
display_errors On, Off Controls whether errors are displayed to the user. Should be Off in production.
log_errors On, Off Enables logging errors to the server error log.
error_log Path to log file Specifies the path to the error log file.

Example: Configuring PHP Error Reporting in Apache

<FilesMatch \.php$>
    php_value error_reporting E_ALL
    php_value display_errors Off
    php_value log_errors On
    php_value error_log /var/log/apache2/php_errors.log
</FilesMatch>
Note: The php_value directive is used in Apache configuration files to set PHP values for specific file types or directories.

9. Enabling PHP OpCache

Directive Values Description
opcache.enable 1 Enables or disables the OPcache as a PHP module.
opcache.enable_cli 0 Enables or disables OPcache for the command line interface.
opcache.memory_consumption 128 Maximum memory in megabytes that OPcache can use.
opcache.max_accelerated_files 4000 Maximum number of files that can be stored in the OPcache.
opcache.revalidate_freq 2 How often to check file timestamps for changes, in seconds.

Example: Enabling OPcache in Apache config

<IfModule mod_php>
    php_flag opcache.enable 1
    php_value opcache.memory_consumption 256
    php_value opcache.max_accelerated_files 10000
    php_value opcache.revalidate_freq 60
</IfModule>
Note: OPcache requires PHP 5.5 or later to function.

10. Testing PHP Installation

Command Description Output
php -v Displays PHP version information PHP 8.3.12 (cli) (built: Oct 5 2024 10:00:00) (NTS)
php -m Lists all loaded PHP modules [PHP Modules]<br>apache2handler<br>mysqli<br>pdo_mysql<br>xml
php -i Displays full PHP configuration (phpinfo) Complete PHP configuration including server API, extensions, and settings

Example: Creating a test PHP file

<?php<br>phpinfo();<br>?>
Note: Ensure the file has a .php extension and is placed in your web server's document root.
Warning: The phpinfo() function should not be used in production environments due to security concerns.

11. Managing Multiple PHP Versions

Version Module Configuration Notes
PHP 7.4 mod_php74 LoadModule php7_module /usr/libexec/apache2/libphp7.so Legacy version, not recommended for new installations
PHP 8.0 mod_php80 LoadModule php_module /usr/libexec/apache2/libphp8.so Latest stable version as of 2026
PHP 8.1 mod_php81 LoadModule php_module /usr/libexec/apache2/libphp81.so Experimental support, requires manual compilation

Example: Load specific PHP version

# Load PHP 8.0 module
LoadModule php_module /usr/libexec/apache2/libphp8.so

# Set PHP handler for .php files
AddHandler php8-script .php
Note: Ensure that only one PHP module is loaded at a time to avoid conflicts.
Warning: Mixing multiple PHP versions in the same virtual host can lead to unexpected behavior or errors.

12. Optimizing PHP-FPM Performance

Configuration Option Description Default Value Notes
pm Process manager algorithm dynamic Options: static, dynamic, ondemand
pm.max_children Maximum number of child processes 50 Higher values increase memory usage but may improve concurrency
pm.start_servers Number of child processes to start at startup 5 Only used with dynamic or ondemand
pm.min_spare_servers Minimum number of idle processes 5 Only used with dynamic
pm.max_spare_servers Maximum number of idle processes 35 Only used with dynamic
pm.max_requests Number of requests a child process handles before restarting 500 Helps prevent memory leaks; set to 0 to disable

Example: PHP-FPM Configuration

; /etc/php/8.1/fpm/pool.d/www.conf
pm = dynamic
pm.max_children = 100
pm.start_servers = 10
pm.min_spare_servers = 5
pm.max_spare_servers = 35
pm.max_requests = 1000
Note: Adjust pm.max_children based on available RAM and expected concurrent requests.

Performance Tuning Tips

  • Monitor memory usage to avoid over-allocating child processes.
  • Use ondemand process manager for low-traffic sites to save resources.
  • Set pm.max_requests to a value that prevents memory leaks without causing frequent restarts.

Was this article helpful?