Enabling PHP error logs on Web Hosting

How to configure error reporting and logging for PHP applications.

Regularly analyzing PHP logs is essential for identifying errors, warnings, and bugs before they affect user experience. It is highly recommended to keep error logging enabled, especially during the development and testing phases.

1. Enabling PHP logs via .htaccess

The most efficient way to enable error display and logging on a web hosting environment is by adding specific directives to your site's root .htaccess file.

Basic configuration:

php_value display_errors On
php_value display_startup_errors On

Advanced configuration (recommended):

php_flag display_errors On
php_flag display_startup_errors On
php_flag log_errors On
php_flag ignore_repeated_errors Off
php_flag ignore_repeated_source Off
php_flag track_errors On

php_value error_reporting -1
php_value error_log /home/ваш_логин/public_html/php-errors.log

Once these lines are added, PHP errors will:

  • Appear directly on the screen (when display_errors is On)
  • Be recorded in the specified php-errors.log file

2. Enabling errors within a PHP script

If you only need to debug a specific script, you can enable error reporting directly within the PHP file by adding the following code to the top of the script:

ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
ini_set('error_reporting', E_ALL);

3. Configuration via php.ini

If your hosting environment provides access to a custom php.ini file, you can manage these settings globally:

error_reporting = E_ALL
display_errors = On
display_startup_errors = On
log_errors = On
log_errors_max_len = 1024
error_log = /home/ваш_логин/public_html/php-errors.log

Logging for Cron jobs

Errors generated by scripts running via Cron or the command line (CLI) do not appear in standard web server logs. To capture this output, redirect the execution result to a dedicated log file in your Cron command:

/usr/local/bin/php /home/ваш_логин/public_html/site.ru/script.php &>> /home/ваш_логин/logs/cron.log

It is a best practice to create a separate /logs directory in your home folder to store all system and cron-related logs securely.

Best practices

  • Development mode: keep display_errors = On for immediate feedback while coding.
  • Production sites: always disable display_errors on live sites to prevent sensitive path information from being leaked to visitors. Ensure log_errors = On remains active for background monitoring.
  • Maintenance: periodically check your php-errors.log file to find hidden issues that don't necessarily break the page but impact performance.
  • Caching: after updating your PHP settings, clear your browser cache and, if applicable, flush the OPCache to see the changes.

Help

If you have any questions or need assistance, please contact us through the ticket system — we're always here to help!

Need help?Our engineers will help you free of charge with any question in minutesContact us