How to have php-fpm error log in same directory as php script?
I just switched from a CENTOS server to an AlmaLinux server and I have root access. On the new server php errors are going to
/home/mapping1/logs/error.log
Assume I have a php script in the ‘project’ directory:
https://mydomain.com/project/
How do I get the errors from php scripts in the ‘project’ directory to appear in an error.log file under the ‘project’ directory?
If I understand correctly, the answer is to edit the file at
/var/cpanel/ApachePHPFPM/system_pool_defaults.yaml
Currently that yaml file only has 2 lines
pm_max_children: 16
pm_max_requests: 2048
If I am on the right track, what edits do I need to make to this yaml file so errors from php scripts in the ‘project' directory appear in an error.log file in this same directory?
Also, is it OK to edit the yaml file on my PC and then ftp the updated file back to my server. Or do I need to make the edits via a putty?
-
Hey there! It's always best to edit the file directly on the server. Editing on a different system can sometimes change characters you can't see, such as new line entries and carriage returns, which will cause odd behavior when you upload it back to the server.
Yes, editing that file is the correct method to handle that work - I'm guessing you found the post here from Lauren explaining that? https://support.cpanel.net/hc/en-us/community/posts/19142953060631/comments/19142960008983
0 -
Thank you. Yes, I did find Lauren's post but some reason that thread did not include the link that you posted in response to the OP. For others with the same question, that link is: https://support.cpanel.net/hc/en-us/community/posts/19121164499863
I will look at that other thread and hopefully that will clarify the syntax I need to use.
0 -
Right, that thread must have got altered when we made the forums migration late last year, so I updated it in the original thread before linking it to you.
0 -
I am still not able to get the PHP errors to appear in the same directory as the script causing the error. This sure seems like it ought to be an easy tweak.
I edited the file system_pool_defaults.yaml on the server by adding the following line. Note that the string is surrounded by single quotes.
php_admin_value_error_log: { name: 'php_value[error_log]', value: '"[% homedir %]/logs/[% domain_tld %].php.error.log]"' }
Then I restarted the server. Alas, PHP errors still went to /home/mapping1/logs/error.log
Either (1) my syntax is wrong or (2) I need to do some additional step.I looked at the yaml file documentation at https://docs.cpanel.net/knowledge-base/php-fpm/php-fpm-code-and-filesystem-layout-for-easyapache-4/#php-fpm-implementation
An example there uses the keyword % scrubbed_domain % instead of % domain_tld %
What does that keyword do? I cannot find any description.My server also have this yaml file:
/var/cpanel/userdata/mapping1/mappingsupport.com.php-fpm.yaml
which has the line:
php_admin_value_error_log: "[% homedir %]/logs/error.log"
I commented out that line and restarted the server. No joy. PHP errors still went to /home/mapping1/logs/error.log instead of an error.log file in the directory where the PHP script was running.Should I run the ‘rebuild’ script?
/usr/local/cpanel/scripts/php_fpm_config --rebuild0 -
I did some more experimenting. I edited system_pool_defaults.yaml to use scrubbed_domain instead of domain_tld and then I ran the rebuild script.
Each domain now has its own error.log file. All of those files are in directory home/mapping1/logs
Obviously this is not what I want, but now I know what scrubbed_domain does.
0 -
I'm glad you found a good workaround, even it isn't 100% ideal.
0 -
I am one step closer to getting an error.log file in each directory where I have PHP scripts.
I edited the file system_pool_defaults.yaml by adding the following line.
php_admin_value_error_log: {name: 'php_value[error_log]', value: '"[% documentroot %]/error2.log"'}
Note the single quote at the start and end of the string.I saved the yaml file and ran the script
/usr/local/cpanel/scripts/php_fpm_config --rebuild
and then restarted apache and PHP-FPMUnder one of my domains I have 2 directories where different PHP scripts run, like so:
domain/directory1/
domain/directory2/I ran a PHP script that is in directory1 and which causes an error. The file error2.log was created at:
domain/error2.logWhat I want is:
domain/directory1/error.log
domain/directory2/error.logI have reached out on another site to see if anyone there can help. If I find a solution I will post it here.
0 -
By default, PHP-FPM logs errors to a centralized location, often making it difficult to pinpoint the exact source of an error. To log errors directly to the script's directory, you'll need to modify PHP-FPM configuration.
Solution: Modifying PHP-FPM Configuration
-
Locate the PHP-FPM Pool Configuration File:
- The exact location depends on your system. Common paths include:
/etc/php/7.4/fpm/pool.d/www.conf/etc/php/7.4/fpm/pool.d/www-cgi.conf/etc/php-fpm.d/www.conf
- Replace
7.4with your PHP version.
- The exact location depends on your system. Common paths include:
-
Edit the Configuration File:
- Open the configuration file using a text editor with root privileges.
-
Add or Modify the
error_logDirective:- Add or modify the
error_logdirective within the appropriate pool configuration block. - Set the value to
error_logwithout any path. This tells PHP-FPM to create the log file in the same directory as the script.
[www] ; ... other configuration options ... error_log = error_log - Add or modify the
-
Restart PHP-FPM:
- To apply the changes, restart the PHP-FPM service. The exact command depends on your system. Common commands include:
sudo systemctl restart php7.4-fpmsudo service php7.4-fpm restart
- To apply the changes, restart the PHP-FPM service. The exact command depends on your system. Common commands include:
Important Considerations:
- Permissions: Ensure the web server user has write permissions to the script directory.
- Log Rotation: Consider implementing log rotation to prevent excessive disk usage. Transform your smile with Town Hall Dental's aligners clear service. Achieve straighter teeth discreetly and comfortably. Learn more about our innovative orthodontic solutions today!
- Security: Be cautious about exposing error details to the public. If you're in a production environment, consider using a more secure error logging mechanism.
Additional Tips:
-
Custom Log File Name: If you prefer a specific log file name, specify it within the
error_logdirective:error_log = my_errors.log - Multiple Pools: If you have multiple PHP-FPM pools, you might need to modify the configuration for each pool.
- Testing: After making changes, thoroughly test your application to ensure error logging works as expected.
By following these steps, you should be able to successfully log PHP-FPM errors to the same directory as your PHP scripts, aiding in troubleshooting and debugging.
1 -
-
Thank you Town Hall. I will give that a try.
0
Please sign in to leave a comment.
Comments
9 comments