Change php error reporting to hide warnings for specific site only [Debian|Ubuntu]

Posted on

Change php error reporting to hide warnings for specific site only [Debian|Ubuntu] – Problems with loading a website are often blamed on the Internet connection, but even the most perfectly set up network cannot help if there is no service to reply at your destination. One of the most popular HTTP servers used for this task is Apache2. Much of Apache’s popularity can be attributed to its easy installation and use, but never the less it is possible to run into problems with even the easiest of the software. If you’ve encountered an issue loading your web page, follow these simple troubleshooting methods outlined in this guide to attempt to get your web server back up and working again. Below are some tips in manage your apache2 server when you find problem about apache-2.2, ubuntu, php, , .

Imagine a couple of sites-enabled available from /etc/apache2/sites-available. (Only Debian-based distros work like this.)

Is it possible to mute warning messages from php scripts from a specific site, without touching the actual htdocs?

Normally there are a couple of solutions to achieve someting related:

  1. Add an error_reporting() directive e.g. error_reporting(E_ERROR); to the scripts executed.
  2. Set php_flags in .htaccess files like so: php_flag display_errors off
  3. Use ini_set inside scripts:

    ini_set('display_errors', 'Off');
    ini_set('display_startup_errors', 'Off');
    error_reporting(0);
    
  4. Prepend @ to functions that throw warnings or errors.

  5. Change php.ini to actually say
    error_reporting = E_ALL ^ E_WARNING
    display_errors = 1

However, these all mean touching the htdocs or having the change applied globally. Imagine the htdocs are mounted read-only. How would I suppress warnings (or notices) for certain sites only?

I am assuming Debian/Ubuntu has a2ensite specific configurations for a reason and I am hoping I can alter those. There is a log_level directive in the example ‘site available’ configuration, but this handles the amount of messages logged. Not the messages output by the php scripts.

Manually adding sections in php.ini or apache2.conf or httpd.conf will also work. If it is possible.

See also:

If you have access to the apache vhost configurations, of course you can also use php_flag directives inside a Virtual Host in the sites-available directory. This way, they’re only applied to this vhost.

Add something like this inside your Virtual Host:

<IfModule mod_php5.c>
    php_admin_flag display_errors off
</IfModule>

EDIT:

If you want only actual errors displayed, you can use the php directive error_reporting with an integer value that describes the types of error levels you want to be displayed:

<IfModule mod_php5.c>
    php_admin_flag display_errors on
    php_admin_value error_reporting 22517
</IfModule>

This is equivalent to a setting of

error_reporting = E_ALL & ~E_NOTICE & ~E_WARNING & ~E_STRICT & ~E_DEPRECATED;

in php.ini. More possible values for example here:

https://web.archive.org/web/20131009000719/http://www.websitefactors.co.uk/php/2013/01/php-error-reporting-settings

Error reporting value calculator:

http://www.bx.com.au/tools/ultimate-php-error-reporting-wizard

I added :

<IfModule mod_php5.c>
 php_admin_flag display_errors on
 php_admin_value error_reporting 30711
</IfModule>

to /etc/apache2/sites-enabled/work and thats working just fine.

Yes I know I should fix those notices, but I’m working on a old script, and will need to come back to that, for now this has solved my problem, and was what the op was looking for. Hope this helps someone.

There is no way to do this without .htaccess or applying the configuration globally. Apache cannot “see” files outside its root folder (htdocs) and .htaccess files only work when in the site’s folder. php_flag doesn’t function outside of .htaccess, either.

So basically, it’s not possible to do on a site-by-site basis without some modification or an .htaccess file.

Leave a Reply

Your email address will not be published.