mostafizur32's avatar

Herd not displaying actual php error. it's showwing 500 error

II am currently working on a PHP project, and I've encountered an issue where my controller is producing an error. However, instead of displaying the actual error message, Herd is returning a 500 Internal Server Error.

1 like
4 replies
Snapey's avatar

Do you have APP_ENV=local and APP_DEBUG=true in your env file ?

mostafizur32's avatar

@Snapey Yea Problem in nginx configuration. In apache server error are showing.

1 like
LaryAI's avatar
Level 58

To diagnose and display the actual PHP error instead of a generic 500 Internal Server Error, you can follow these steps:

  1. Enable Error Reporting in PHP: Ensure that PHP is configured to display errors. You can do this by modifying your php.ini file or by adding the following lines at the beginning of your PHP script:

    ini_set('display_errors', 1);
    ini_set('display_startup_errors', 1);
    error_reporting(E_ALL);
    
  2. Check Your Web Server Configuration: Sometimes, the web server configuration might be suppressing the error messages. If you are using Apache, make sure that the display_errors directive is set to On in your .htaccess file or in the server configuration:

    php_flag display_errors on
    

    For Nginx with PHP-FPM, ensure that the php.ini file used by PHP-FPM has display_errors enabled.

  3. Review Your Error Logs: Check the web server error logs for more detailed information about the error. For Apache, the error log is usually located at /var/log/apache2/error.log, and for Nginx, it is typically at /var/log/nginx/error.log.

  4. Use a Custom Error Handler: Implement a custom error handler in your PHP application to catch and display errors more gracefully. Here is an example of a simple custom error handler:

    set_error_handler(function ($errno, $errstr, $errfile, $errline) {
        echo "<b>Error:</b> [$errno] $errstr - $errfile:$errline";
        echo "<br>";
        echo "Terminating PHP script.";
        die();
    });
    
    // Example code that triggers an error
    echo $undefined_variable;
    
  5. Check for Fatal Errors: If your script is encountering a fatal error, it might not be displayed due to the script termination. You can use the register_shutdown_function to catch fatal errors:

    register_shutdown_function(function () {
        $error = error_get_last();
        if ($error !== NULL) {
            $info = "[SHUTDOWN] level: {$error['type']}, message: {$error['message']}, file: {$error['file']}, line: {$error['line']}";
            echo $info;
        }
    });
    
    // Example code that triggers a fatal error
    non_existent_function();
    

By following these steps, you should be able to see the actual PHP error messages instead of a generic 500 Internal Server Error. This will help you debug and resolve the issue more effectively.

mkwsra's avatar

I have the same issue, as if the team behind Herd made all errors disappear; could it be because I'm not a pro member of Herd?

Please or to participate in this conversation.