8

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:

3 Answers 3

15

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

4
  • Yes, this is what I am looking for. The vhost configuration is site-specific and inside /etc/apache2/sites-available. Do you know any trickery to change this so that errors are in fact displayed, but warnings are not? Commented May 21, 2013 at 20:27
  • I've edited my answer. Commented May 22, 2013 at 10:16
  • This is [Thrift Shop] awesome! Accepted. Commented May 23, 2013 at 0:17
  • You can also calculate the number just by running this in the command line: php -r 'echo E_ALL & ~E_NOTICE & ~E_WARNING & ~E_STRICT & ~E_DEPRECATED; echo "\n";' Commented Apr 11, 2023 at 13:31
1

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.

2
  • 1
    Can you please explain the meaning of 30711? I assume this is an or'ed bitmask of the E_* error reporting constants. If so, it's horrible practice to write it as you did. Commented Feb 25, 2015 at 6:26
  • @AndrewSchulman - The constants E_* are only available within php, and apache doesn't know what they are. See "Note: PHP Constants outside of PHP" on php.net/manual/en/errorfunc.configuration.php Commented Jan 24, 2019 at 12:12
0

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.

1
  • Actually, Apache knows exactly what site we are talking about because we can specify sections for specific directories in the configuration. The answer by etagenklo is close to doing just that. :) Just need to find a way to alter it so it applies to warnings. Commented May 21, 2013 at 20:27

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.