How To Log Apache 500 Internal Server Errors?

Published August 28, 2024

Problem: Capturing Apache 500 Internal Server Errors

Apache 500 Internal Server Errors are hard to diagnose without good logging. These errors show server-side issues that stop a request from being processed, but they don't give details about the cause of the problem.

Configuring Apache to Log 500 Internal Server Errors

Locating the Apache Configuration File

To configure Apache to log 500 Internal Server Errors, you need to find the main configuration file. The file location depends on your operating system and Apache version:

  • Ubuntu and Debian: /etc/apache2/apache2.conf
  • CentOS and Red Hat: /etc/httpd/conf/httpd.conf
  • macOS: /etc/apache2/httpd.conf

To check your Apache version, run apache2 -v or httpd -v in the terminal.

Tip: Backup Your Configuration

Before making changes to your Apache configuration file, create a backup. This allows you to revert changes if needed. Use this command:

sudo cp /path/to/apache2.conf /path/to/apache2.conf.backup

Replace /path/to/apache2.conf with the actual path to your configuration file.

Enabling Error Logging

After finding the configuration file, follow these steps to enable error logging:

  1. Open the configuration file with a text editor using sudo privileges.

  2. Find or add the LogLevel directive. Set it to log 500 errors:

    LogLevel warn

    This logs warnings and more severe issues, including 500 errors.

  3. Find or add the ErrorLog directive to specify where Apache will write error logs:

    ErrorLog ${APACHE_LOG_DIR}/error.log

    Replace ${APACHE_LOG_DIR} with the actual path if needed.

  4. Save the changes and exit the text editor.

  5. Restart Apache to apply the new settings:

    • Ubuntu/Debian: sudo systemctl restart apache2
    • CentOS/Red Hat: sudo systemctl restart httpd
    • macOS: sudo apachectl restart

After these changes, Apache will log 500 Internal Server Errors to the specified error log file. You can monitor this file to fix issues as they occur.

Logging PHP Errors in Apache

Modifying php.ini Settings

To log PHP errors in Apache, you need to change the php.ini file. Here's how:

  1. Find the php.ini file:

    • Ubuntu/Debian: /etc/php/[version]/apache2/php.ini
    • CentOS/Red Hat: /etc/php.ini
    • macOS: /etc/php.ini or /usr/local/php/php.ini

    You can find the exact location by running php --ini in the terminal.

  2. Open the php.ini file with a text editor using sudo privileges.

  3. Enable error reporting and logging by setting these directives:

    error_reporting = E_ALL
    display_errors = Off
    log_errors = On
  4. Set the error_log directive to specify where PHP should write error logs:

    error_log = /var/log/php_errors.log

    Make sure the directory exists and is writable by the web server.

  5. Save the changes and exit the text editor.

Tip: Verify PHP Configuration

After making changes to php.ini, you can verify the current PHP configuration by creating a PHP file with the following content and accessing it through your web browser:

<?php
phpinfo();
?>

This will display all current PHP settings, including error reporting and logging configurations.

Adjusting PHP Error Reporting Level

PHP has different error reporting levels. Here's how to set them:

  1. Understand the error reporting levels:

    • E_ALL: Reports all errors and warnings
    • E_ERROR: Reports only fatal runtime errors
    • E_WARNING: Reports runtime warnings
    • E_NOTICE: Reports notices for non-critical errors
  2. Set the level:

    • For development:
      error_reporting = E_ALL
    • For production:
      error_reporting = E_ALL & ~E_NOTICE & ~E_DEPRECATED & ~E_STRICT
  3. After making changes, restart Apache to apply the new settings.