How to Locate PHP Error Logs in Apache Web Server Configuration


2 views

When setting up PHP with Apache, error logs are crucial for debugging. By default, Apache stores error logs in these locations depending on your OS:


# Linux/Unix systems
/var/log/apache2/error.log
/var/log/httpd/error_log

# Windows systems
C:\xampp\apache\logs\error.log

To specifically track PHP errors, you should modify either php.ini or virtual host configuration:


# In php.ini
error_log = /var/log/php_errors.log
log_errors = On

# In Apache Virtual Host
<VirtualHost *:80>
    ErrorLog "/var/log/apache2/example.com-error.log"
    CustomLog "/var/log/apache2/example.com-access.log" common
    php_admin_value error_log "/var/log/php/example.com-php_error.log"
</VirtualHost>

Here's how to implement a dedicated PHP error log with proper permissions:


sudo mkdir /var/log/php
sudo touch /var/log/php/php_errors.log
sudo chown www-data:www-data /var/log/php/php_errors.log
sudo chmod 664 /var/log/php/php_errors.log

If you're not seeing PHP errors in your logs, check these settings:


display_errors = Off   # Should be Off in production
log_errors = On        # Must be On
error_reporting = E_ALL # For development

For real-time monitoring of PHP errors, you can use this command:


tail -f /var/log/php/php_errors.log

To separate warnings from critical errors, consider this setup:


# Log all errors to main log
error_log = /var/log/php/php_errors.log

# Log only critical errors to separate file
error_log = /var/log/php/php_critical.log php7:error


html

When setting up PHP with Apache, errors are typically logged in one of these locations depending on your OS and configuration:

  • Linux/Unix: /var/log/apache2/error.log or /var/log/httpd/error_log
  • Windows: Apache-install-dir/logs/error.log
  • macOS: /private/var/log/apache2/error_log

To find the exact path, check Apache's configuration:


# Check Apache's main config
grep ErrorLog /etc/apache2/apache2.conf

# Or check virtual hosts
grep -r ErrorLog /etc/apache2/sites-enabled/

# For PHP-specific errors
grep error_log /etc/php/*/apache2/php.ini

PHP can log separately from Apache. Check these directives in php.ini:


error_log = /var/log/php_errors.log
log_errors = On
display_errors = Off  # Recommended for production

Here's how to configure a dedicated PHP error log:


# In php.ini
error_log = /var/log/php_errors.log

# In Apache Virtual Host
<VirtualHost *:80>
    ErrorLog /var/log/apache2/example.com-error.log
    CustomLog /var/log/apache2/example.com-access.log combined
    php_value error_log /var/log/php/example.com-php_errors.log
</VirtualHost>

# Create the log directory
sudo mkdir -p /var/log/php
sudo chown www-data:www-data /var/log/php
sudo chmod 755 /var/log/php
  • Ensure the web server user (www-data/apache) has write permissions to the log files
  • After configuration changes, restart Apache: sudo systemctl restart apache2
  • For permission issues: sudo chown www-data:www-data /path/to/error.log

Typical PHP errors you might find in logs:


[Wed Jan 01 12:00:00.123456] [php7:error] [pid 1234] [client 1.2.3.4] 
PHP Fatal error: Uncaught Error: Call to undefined function my_function() 
in /var/www/example.com/index.php:10

[Wed Jan 01 12:00:01.123456] [php7:warning] [pid 1234] [client 1.2.3.4] 
PHP Warning: include_once(/var/www/example.com/config.php): failed to open stream: 
No such file or directory in /var/www/example.com/index.php on line 5