20

I've set up a fresh install of Ubuntu Server 12.04 LTS on Amazon AWS with *Apache2/MySQL/PHP5. When I run a PHP script and it encounters an error I don't see any error reporting from PHP, all I see is

HTTP Error 500 (Internal Server Error): An unexpected condition was encountered while the server was attempting to fulfil the request.

I have checked my /etc/php5/apache2/php.ini file and as far as I can tell error reporting should be set up. The contents of the file (regarding errors) are:

;    display_errors
;      Default Value: On
;      Development Value: On
;      Production Value: Off

;    display_startup_errors
;      Default Value: Off
;      Development Value: On
;      Production Value: Off

;    error_reporting
;      Default Value: E_ALL & ~E_NOTICE
;      Development Value: E_ALL | E_STRICT
;      Production Value: E_ALL & ~E_DEPRECATED

Can anyone advise? If I try something like $obj = new ObjectDoesntExist; it doesn't tell me Fatal error: Class 'ObjectDoesntExist' it gives me a server 500 error.

Any advise?

* The modules I have installed are: mysql-server mysql-client apache2 php5 libapache2-mod-php5 phpmyadmin. Other than that it is a completely base install of Ubuntu Server 12.04 LTS

EDIT: If I use ini_set('display_errors', '1'); at the start of my script it displays errors as normal, but how do I enable this site wide?

iainjames88
  • 315
  • 1
  • 2
  • 6
  • Do you see anything in error_log if your apache server? It sounds like mode_php just doesn't work. Would a php file containing only ` phpinfo(); ?>` give output? – favoretti Jul 31 '12 at 21:11
  • have you checked apache error logs `/var/log/apache2/error.log`? – Kalpesh Jul 31 '12 at 21:13
  • 2
    All those lines in your php.ini are commented out. Find where `display_errors` is declared and turn it on. – Steve Robbins Jul 31 '12 at 21:17
  • `` gives output as expected. Apache error log shows the PHP errors I was expecting (unexpected T_STRING / Failed opening required files) amongst other things I recognise (Apache restarts). – iainjames88 Jul 31 '12 at 21:19
  • @stevether I found `display_errors` and `display_startup_errors` and changed their values from `Off` to `On` but still the same issue? Do I have to uncomment the lines above them that read the same as the commented lines I originally posted? – iainjames88 Jul 31 '12 at 21:22

4 Answers4

14

The php.ini snippet you pasted has a semicolon (the ; character) in each line. The semicolon is the start of a comment in php.ini, so everything in a line following a semicolon is not used. Try manually setting display_errors to on and error_reporting to E_ALL, or removing the appropriate semicolons to fix this.

Furthermore, check your apache's error log, php might be logging its errors there.

Wouter
  • 833
  • 6
  • 13
  • I've uncommented the `;` from the snippet I posted above and set `display_errors = On` and `display_startup_errors = On` where I found them in the `php.ini` file but still the same problem? – iainjames88 Jul 31 '12 at 21:24
  • 1
    Did you restart apache after making the changes in your php.ini? Any errors in apache's errorlog? – Wouter Jul 31 '12 at 21:25
  • 2
    Restarted apache while waiting for your reply and that's solved it. Feel a bit silly for not doing that originally! Thanks for the help :) – iainjames88 Jul 31 '12 at 21:26
3

please edit the php.ini file by using the sudo gedit /etc/php5/apache2/php.ini and on the Production Value,or you can also use the ini_set('display_errors', '1'); at the top of your php file

Community
  • 1
  • 1
user2779489
  • 391
  • 1
  • 3
  • 11
0

I had the same problem but unrelated with php.ini if you have same error, before you begin snipping on your server, first check your code. Mistake can be a missing } ) or ; or something like that. this is just reference what to do first.

mandza
  • 330
  • 9
  • 24
0
// Disable displaying errors, enable logging
ini_set('display_errors', 0);
ini_set('log_errors', 1);
ini_set('error_log', '/var/log/php_error.log');

// Handle errors and exceptions
set_error_handler(function($errno, $errstr, $errfile, $errline) {
    error_log(sprintf("[%s] %s in %s on line %d", date('Y-m-d H:i:s'), $errstr, $errfile, $errline));
    http_response_code(500);
    exit();
});

set_exception_handler(function($exception) {
    error_log(sprintf("[%s] Uncaught exception '%s' with message '%s' in %s:%d\nStack trace:\n%s\n", 
        date('Y-m-d H:i:s'), 
        get_class($exception), 
        $exception->getMessage(), 
        $exception->getFile(), 
        $exception->getLine(), 
        $exception->getTraceAsString()
    ));
    http_response_code(500);
    exit();
});
  • set_error_handler to handle PHP errors, and set_exception_handler to handle exceptions. When an error or exception occurs, the message is logged to the error log file using the error_log function, and the response code is set to 500 to indicate a server error. Finally, an example error is triggered by attempting to use an undefined variable.
warfish
  • 613
  • 5
  • 20