How do I enable error reporting in PHP?
When you migrate a site from a shared hosting environment to your own server, the behavior of PHP changes significantly by default. On many managed hosts, configurations are tuned for security and stability, which means fatal errors often result in a generic "500 Internal Server Error" rather than the specific stack trace you are used to seeing. If you are debugging a script and suddenly encounter an opaque 500 error instead of a message like Fatal error: Expecting ;, you need to adjust your configuration to expose those underlying issues.
The core mechanism controlling this behavior is the display_errors directive in php.ini. By default, production environments usually have this set to Off. This prevents sensitive file paths and variable values from appearing in browser responses, which is a critical security practice. However, when you are developing or debugging on your own server, keeping errors hidden makes troubleshooting nearly impossible. You must explicitly enable display_errors to see the raw output that was previously swallowed.
In addition to displaying errors in the browser, you should configure PHP to log them. This is handled by the error_log directive. Relying solely on browser display is fragile because a single script error can halt execution before any further logging occurs. Instead, set up a dedicated log file that captures all runtime errors.
To implement this, edit your php.ini file. You will typically need to locate it using the php --ini command or check your web server configuration. Once you have the file open, add or modify the following lines:
display_errors = On
error_reporting = E_ALL
error_log = /var/log/php/error.log
log_errors = On
The error_reporting = E_ALL line ensures that PHP reports all types of errors, warnings, and notices. Without this, you might miss subtle issues like undefined array indices or type mismatches. The log_errors = On directive ensures these messages are written to the file specified in error_log. If you are running PHP as a CLI application rather than through Apache or Nginx, the path for error_log should be an absolute path accessible by the user running the process.
There is a significant trade-off here regarding security versus debuggability. Enabling display_errors on a live production site is generally discouraged because it leaks internal implementation details to potential attackers. If you are deploying code that has not yet been hardened, the best practice is to enable error reporting only during the development phase or behind a specific flag for debugging environments.
If your setup involves multiple PHP versions (common with tools like PHPSwitcher or virtual hosting), remember that the php.ini file might be version-specific. Ensure you are editing the configuration for the exact PHP version your web server is currently serving traffic on, otherwise, your changes will have no effect.
For projects using frameworks like Laravel, the approach can vary slightly. While framework-specific configuration files often wrap these directives, the underlying PHP engine settings still apply. If you encounter issues where errors are displayed but the script continues running (which happens if display_errors is on but error_log is not configured), ensure both directives are active.
By correctly configuring these four directives, you restore the visibility of fatal errors like syntax mistakes while maintaining a log trail for analysis. This allows you to catch issues immediately rather than waiting until they cause unexpected downtime or obscure 500 errors that are impossible to diagnose without the original stack trace.