How To Set Up Separate PHP Error Logs For Each Virtual Host?

Published September 14, 2024

Problem: Managing PHP Error Logs for Multiple Virtual Hosts

Running multiple virtual hosts on one server can make PHP error log management difficult. The standard setup often puts all errors in one file, which makes it hard to find and fix problems for each virtual host.

Configuring Separate PHP Error Logs in Apache Virtual Hosts

Modifying the VirtualHost Configuration

To set up separate PHP error logs for each virtual host, modify the Apache configuration files. Access the main Apache configuration file, usually located at /etc/apache2/apache2.conf or /etc/httpd/httpd.conf, depending on your system.

In the configuration file, find the VirtualHost directives. These define the settings for each virtual host on your server. They typically look like this:

<VirtualHost *:80>
    ServerName example.com
    DocumentRoot /var/www/example.com
    # Other directives...
</VirtualHost>

Adding PHP Error Log Directives

After locating the VirtualHost directives, add the PHP error log settings. Use the php_value directive within each VirtualHost block. The key directive to add is php_value error_log, which sets the location of the PHP error log file for that virtual host.

Here's how to add the PHP error log directive:

<VirtualHost *:80>
    ServerName example.com
    DocumentRoot /var/www/example.com
    php_value error_log /var/log/apache2/example.com-php-errors.log
    # Other directives...
</VirtualHost>

Specify a unique log file path for each virtual host. This helps you identify which errors belong to which site. For example:

<VirtualHost *:80>
    ServerName site1.com
    DocumentRoot /var/www/site1.com
    php_value error_log /var/log/apache2/site1.com-php-errors.log
</VirtualHost>

<VirtualHost *:80>
    ServerName site2.com
    DocumentRoot /var/www/site2.com
    php_value error_log /var/log/apache2/site2.com-php-errors.log
</VirtualHost>

By adding these directives, you tell PHP to write errors for each virtual host to its own log file. This makes it easier to monitor and fix issues specific to each site.

Tip: Set Appropriate Permissions

After creating separate PHP error log files, make sure to set the correct permissions. Use the following command to give Apache write access to the log files:

sudo chown www-data:www-data /var/log/apache2/site1.com-php-errors.log
sudo chmod 644 /var/log/apache2/site1.com-php-errors.log

Replace 'www-data' with the appropriate Apache user on your system if different.

Example VirtualHost Configuration with Separate PHP Error Logs

Here's a sample configuration code for a virtual host with a custom PHP error log:

<VirtualHost *:80>
    ServerName example.com
    DocumentRoot /var/www/domains/example.com/html
    ErrorLog /var/www/domains/example.com/apache.error.log
    CustomLog /var/www/domains/example.com/apache.access.log common
    php_flag log_errors on
    php_value error_reporting 2147483647
    php_value error_log /var/www/domains/example.com/php.error.log
</VirtualHost>

This configuration sets up a virtual host for example.com with these elements:

  • ServerName: Defines the domain name for this virtual host.
  • DocumentRoot: Specifies the directory where the website files are stored.
  • ErrorLog: Sets the location for Apache's error log.
  • CustomLog: Defines the location for Apache's access log.
  • php_flag log_errors on: Enables PHP error logging.
  • php_value error_reporting 2147483647: Sets PHP to report all errors, warnings, and notices.
  • php_value error_log: Specifies the location for the PHP error log, separate from the Apache error log.

By using this configuration, you create a separation between Apache logs and PHP logs for each virtual host. This setup helps you monitor and fix issues specific to PHP for each website on your server.

Tip: Rotate Your Logs

To prevent log files from growing too large and consuming excessive disk space, implement log rotation. You can use tools like logrotate to automatically archive and compress old log files, while creating new ones. Here's a basic logrotate configuration for your PHP error logs:

/var/www/domains/*/php.error.log {
    weekly
    rotate 4
    compress
    delaycompress
    missingok
    notifempty
    create 640 www-data www-data
}

This configuration rotates the PHP error logs weekly, keeps 4 weeks of logs, compresses old logs, and sets appropriate permissions.

Verifying the Separate PHP Error Logs

After setting up separate PHP error logs for your virtual hosts, check that the setup works. This involves checking log file creation and testing error logging for each virtual host.

Checking Log File Creation

Check that the log files have been created in the specified locations. Use the 'ls' command in the terminal to check the directory where you set up the error logs:

ls -l /var/www/domains/*/php.error.log

This command lists all PHP error log files for your virtual hosts. If the files exist, you'll see them listed with their permissions, owner, and modification date.

If the files don't exist, they might be created when the first error occurs. In this case, move to the next step to generate some test errors.

Tip: Use grep to find specific log files

If you have many virtual hosts and want to find a specific log file, you can use the grep command:

ls -l /var/www/domains/*/php.error.log | grep example.com

This will show only the log file for the example.com domain.

Testing Error Logging for Each Virtual Host

To test error logging, create a simple PHP script that generates an error for each virtual host. Here's how to do it:

  1. Create a test PHP file in each virtual host's document root:
echo "<?php trigger_error('Test error for ' . $_SERVER['SERVER_NAME'], E_USER_WARNING);" > /var/www/domains/example.com/html/test-error.php
  1. Repeat this for each virtual host, replacing 'example.com' with the domain name.

  2. Access the test-error.php file for each virtual host through a web browser or using curl:

curl http://example.com/test-error.php
  1. Check the PHP error log for each virtual host:
tail -n 5 /var/www/domains/example.com/php.error.log

You should see the test error message in the log file, showing that the separate error logging is working.

  1. Repeat steps 3 and 4 for each virtual host to confirm that errors are logged separately.

If you see the test errors in the correct log files, your configuration for separate PHP error logs is working. This setup helps you monitor and fix PHP issues specific to each virtual host.