NOTE: The example used in this post is setting PHP's
error_reporting level, which is no longer supported as of PHP 7.0. Regardless approaches documented in this post are still applicable for setting other
Recently I needed to adjust PHP’s
The goal was to set it to
E_ALL & ~E_NOTICE which would silences notices.
The project in question was a Magento deployment, where it’s never advisable to modify core files (e.g. index.php). As such, I Googled “htaccess error_reporting E_ALL & ~E_NOTICE”, with hopes of making the change in the .htaccess file.
I landed on the Stack Overflow question “How to disable notice and warning in PHP within .htaccess file? “ where I found the following answer…
php_value error_reporting 2039
I added that to the .htaccess, refreshed the page and got a 500 error.
Looking in the logs I found the following error.
[Mon Jan 29 21:09:14.856002 2018] [core:alert] [pid 34416:tid 123145425289216] [client ::1:62547] /var/www/html/.htaccess: Invalid command 'php_value', perhaps misspelled or defined by a module not included in the server configuration
After a bit of research I realized the issue was that the answer I found only works with mod_php, but the project in question was using PHP-FPM.
In this post, let’s take a look at the option for setting a php_value when using PHP‑FPM.
The first option for setting
php_values is to use .user.ini files. For the Magento project in question this was just a matter of dropping a .user.ini in the root next to index.php and .htaccess with the following.
error_reporting = E_ALL & ~E_NOTICE
Note that there is some caching at play with .user.ini files, so you might not see your changes right away. From the PHP documentation…
user_ini.cache_ttlcontrols how often user INI files are re-read. The default is 300 seconds (5 minutes).
php_values can be set is within PHP-FPM conf files. The default
www.conf file explains how this works…
; Additional php.ini defines, specific to this pool of workers. These settings ; overwrite the values previously defined in the php.ini. The directives are the ; same as the PHP SAPI: ; php_value/php_flag - you can set classic ini defines which can ; be overwritten from PHP call 'ini_set'. ; php_admin_value/php_admin_flag - these directives won't be overwritten by ; PHP call 'ini_set' ; For php_*flag, valid values are on, off, 1, 0, true, false, yes or no.
A couple things here are interesting…
.user.ini, so if
php_values are set in PHP-FPM conf files,
.user.inioverrides don’t work.
In order adjust
error_reporting through a PHP-FPM conf file I’d create a new file named e.g.
99-user.conf with the following
php_value[error_reporting] = E_ALL & ~E_NOTICE
The following can be added to .htaccess…
SetEnv PHP_VALUE "error_reporting = 2039"
However, this has a major limitation in that you can only set one
PHP_VALUE this way, because if you tried to to do…
SetEnv PHP_VALUE "error_reporting = 2039" SetEnv PHP_VALUE "post_max_size = 16M"
…the second line would override the
PHP_VALUE environment variable value from the first line, preventing the first line from actually working.
Hi, I'm Max!
If you'd like to get in touch with me the best way is on Twitter.