通过.htaccess设置PHP错误日志后error_log无值的排查方法
Got it, let’s work through why your configured error log path in .htaccess isn’t showing up in phpinfo, even though you’ve set it up and have 2.php with phpinfo() and custom error_log calls. Here’s a step-by-step breakdown tailored to your setup:
1. Verify Apache’s AllowOverride Setting
First off, .htaccess only works if Apache is allowed to override directory-level settings for your /var/www/html folder.
- Locate your Apache site configuration file (usually
/etc/apache2/sites-available/000-default.confon Debian/Ubuntu, or/etc/httpd/conf/httpd.confon RHEL/CentOS). - Look for the
<Directory /var/www/html>block. TheAllowOverridedirective here needs to be set toAllor at leastFileInfo(sincephp_valuefalls under the FileInfo override category). For example:<Directory /var/www/html> AllowOverride FileInfo # ... other settings </Directory> - After updating, restart Apache to apply changes:
sudo systemctl restart apache2 # Debian/Ubuntu # or sudo systemctl restart httpd # RHEL/CentOS
2. Check .htaccess Syntax and Configuration Validity
Make sure your .htaccess file has the correct directives and no typos:
- Your .htaccess should include these lines (double-check spelling and path):
php_flag log_errors On php_value error_log /var/tmp/PHP_errors.log - To confirm Apache is actually reading your .htaccess, add an invalid line (like
invalid_directive) to the file. If accessinghttp://localhost/2.phpthrows a 500 Internal Server Error, Apache is parsing the file. If no error occurs, go back to step 1—Apache is ignoring .htaccess entirely.
3. Confirm PHP Runtime Mode
The .htaccess php_value directive only works with mod_php (Apache’s embedded PHP module). If you’re using PHP-FPM, this won’t work:
- Check the
Server APIfield in phpinfo():- If it says
Apache 2.0 Handler, you’re using mod_php—stick with the .htaccess approach. - If it says
FPM/FastCGI, you need to update your PHP-FPM pool config instead:- Open your FPM pool file (e.g.,
/etc/php/8.1/fpm/pool.d/www.conf). - Add or modify this line:
php_admin_value[error_log] = /var/tmp/PHP_errors.log - Restart PHP-FPM:
sudo systemctl restart php8.1-fpm
- Open your FPM pool file (e.g.,
- If it says
4. Validate File/Directory Permissions
Even if the config is right, permission issues can block log writing:
- Ensure the
/var/tmpdirectory is writable by the Apache user (usuallywww-dataorapache):
The output should show permissions likels -ld /var/tmpdrwxrwxrwt(sticky bit set, global read/write). If not, fix it with:sudo chmod 1777 /var/tmp - If
PHP_errors.logalready exists, make sure the Apache user owns it or has write access:sudo chown www-data:www-data /var/tmp/PHP_errors.log
5. Check for Configuration Overrides
Other PHP config files might be overriding your .htaccess setting:
- In phpinfo(), look for:
Loaded Configuration File: This is the mainphp.ini—check if it has anerror_logline that’s overriding your .htaccess setting.Additional .ini files parsed: Any.inifiles listed here could also seterror_log.
- Note: If the main
php.iniusesphp_admin_value error_loginstead ofphp_value, this cannot be overridden by .htaccess—you’ll need to adjust the main config or usephp_valuethere instead.
6. Test the Custom error_log Call Directly
Don’t rely solely on phpinfo—test if your actual log is working:
- In
2.php, add a test error log call:error_log("Test message from 2.php"); - Visit
http://localhost/2.php, then check/var/tmp/PHP_errors.logfor the message. If it’s there, the log is working even if phpinfo isn’t reflecting it (rare, but possible due to config caching). If not, go back to the earlier steps to fix the root issue.
内容的提问来源于stack exchange,提问作者Trts

