Getting PHP-FPM Error Log Directory Script
When running multiple PHP scripts across different domains and directories, it is essential to have a separate error log file for each script. This allows for more precise error tracking and debugging. This article will guide you through creating a PHP-FPM error log directory script that will help you achieve this goal.
Understanding PHP-FPM
PHP-FPM (FastCGI Process Manager) is a method for executing PHP scripts, particularly useful for high-traffic websites. It offers several advantages, such as improved performance, better handling of concurrent requests, and more granular control over the PHP environment.
One key feature of PHP-FPM is the ability to set up separate error logs for each PHP script, making it easier to track and resolve issues.
Setting Up a Separate Error Log Directory
To create a separate error log directory for each PHP script, follow these steps:
-
Create a new directory for the error logs. This can be located anywhere on your server, but it is recommended to keep it outside the web root for security reasons.
sudo mkdir /var/log/php-fpm -
Change the ownership and permissions of the directory to ensure that the web server user has write access.
sudo chown -R www-data:www-data /var/log/php-fpm sudo chmod -R 775 /var/log/php-fpm -
Modify the PHP-FPM configuration file to specify the error log directory for each PHP script. This file is usually located at
/etc/php/<version>/fpm/pool.d/www.conf.[www] listen = /run/php/php<version>-fpm.sock listen.owner = www-data listen.group = www-data user = www-data group = www-data pm = dynamic pm.max_children = 5 pm.start_servers = 2 pm.min_spare_servers = 1 pm.max_spare_servers = 3 request_terminate_timeout = 120s request_slowlog_timeout = 10s slowlog = /var/log/php-fpm/slow.log access.log = /var/log/php-fpm/access.log [domain1] listen = /run/php/php<version>-fpm-domain1.sock listen.owner = www-data listen.group = www-data user = www-data group = www-data pm = dynamic pm.max_children = 5 pm.start_servers = 2 pm.min_spare_servers = 1 pm.max_spare_servers = 3 request_terminate_timeout = 120s request_slowlog_timeout = 10s slowlog = /var/log/php-fpm/domain1-slow.log access.log = /var/log/php-fpm/domain1-access.log php_admin_value[error_log] = /var/log/php-fpm/domain1/error.log [domain2] listen = /run/php/php<version>-fpm-domain2.sock listen.owner = www-data listen.group = www-data user = www-data group = www-data pm = dynamic pm.max_children = 5 pm.start_servers = 2 pm.min_spare_servers = 1 pm.max_spare_servers = 3 request_terminate_timeout = 120s request_slowlog_timeout = 10s slowlog = /var/log/php-fpm/domain2-slow.log access.log = /var/log/php-fpm/domain2-access.log php_admin_value[error_log] = /var/log/php-fpm/domain2/error.logIn this example, we have created two additional PHP-FPM pools (
domain1anddomain2) with their own error log files. -
Restart the PHP-FPM service to apply the changes.
sudo systemctl restart php-fpm
Summary
In this article, we have discussed the importance of having separate error log files for each PHP script running on your server. By following the steps outlined, you can create a PHP-FPM error log directory script that will help you achieve this goal.