7

We have an out-of-the-box PHP application running on a subdomain, and it's logging errors in its DocumentRoot:

/var/www/appname/path/to/document/root/php-errors.log

...instead of where we want the error logs to go:

/var/www/appname/logs/php-errors.log

Since I don't want to change the code in the out-of-the-box application, and I don't want to change the error log location for all the subdomains (read: file php.ini), how can I do this in just the Apache configuration file that relates to the subdomain?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
e_i_pi
  • 4,590
  • 4
  • 27
  • 45

3 Answers3

8

From error_log per Virtual Host?:

<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_flag display_errors on
    php_value error_reporting 2147483647
    php_value error_log /var/www/domains/example.com/php.error.log
</VirtualHost>
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Matt
  • 5,315
  • 1
  • 30
  • 57
  • 1
    I'm going to accept this answer because it's dead right, but I got my question wrong - I need it for PHP run as FastCGI. I'll pop a new question up for that one – e_i_pi Jun 15 '17 at 04:22
  • 1
    See if this helps: https://serverfault.com/questions/763719/setting-php-value-in-virtualhost-on-debian-squeeze-apache-2-4-10-mod-proxy-fcg#773813 – Matt Jun 15 '17 at 04:29
  • I'd reply here with how to do it in FPM to keep the trail, but I'll post a new answer because it's a bit tricky and has a few steps. Thanks for the answer and comment response @mkaatman, you're a life saver – e_i_pi Jun 16 '17 at 00:15
  • I thought it was better to indicate that `php_value` won't work for PHP-FPM because Apache did not load the libphp module – bilogic Jul 16 '23 at 08:43
8

For those wishing to do this using php-fpm (which I meant to originally post about), here's how you do it:

Ensure you're logged in as root, or using sudo for all your commands.

Go into your php-fpm directory*, using the command below:

cd /etc/php/fpm/

In that directory, edit your php.ini file and add the following to the end:

If you want to set error log by host

[HOST=www.example.com]
error_log=/var/www/myapplication/path/to/my/error.log

[HOST=sub.example.com]
error_log=/var/www/mysubapplication/path/to/my/error.log

If you want to set error log by path (handy if you're working on a server with an IP address but no domain)

[PATH=/var/www/myapplication]
error_log=/var/www/myapplication/path/to/my/error.log

[PATH=/var/www/mysubapplication]
error_log=/var/www/mysubapplication/path/to/my/error.log

You now need to go into the pool directory*, using the command below:

cd /etc/php/fpm/pool.d/

In that directory, edit the file www.conf. Note the values for user and group in that file, whose out-of-the-box setting is www-data for both. Look for the term catch_workers_output and make sure it is uncommented and set to yes, like so:

catch_workers_output = yes

Now you need to create the error log file(s) and ensure that php-fpm has access to it. This is why you needed to note the values for user and group from the previous file edit. Create an error log file for each HOST/PATH you set when editing php.ini and give them the appropriate permissions and ownership, for example:

touch /var/www/myapplication/path/to/my/error.log
chmod 0660 /var/www/myapplication/path/to/my/error.log
chown www-data:www-data /var/www/myapplication/path/to/my/error.log

Finally, restart your php-fpm service using the command** below:

service php-fpm restart

* Note: If, like me, you install declared versions of php-fpm, the directory paths will change to (for example) the following:

/etc/php/5.6/fpm/
/etc/php/5.6/fpm/pool.d/

/etc/php/7.1/fpm/
/etc/php/7.1/fpm/pool.d/

** The service takes a specific versioned name if you installed declared versions, and you will need use (for example) the following:

service php5.6-fpm restart

service php7.1-fpm restart
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
e_i_pi
  • 4,590
  • 4
  • 27
  • 45
  • A side-note, I find it is easier to manage/automate when you add the logs to the `/etc/php-fpm.d` location. I do something like: `999-php-logs-example.com.conf` Then I add something like: `[PATH=/var/www/vhosts/example.com]error_log=/var/www/vhosts/example.com/logs/php-error.log slowlog=/var/www/vhosts/example.com/logs/php-slow.log`.... sorry, cannot add multi-line code block – Jeremy Oct 18 '19 at 18:25
2

I configure it like this:

File /etc/apache2/envvars

# For supporting multiple Apache 2 instances
if [ "${APACHE_CONFDIR##/etc/apache2-}" != "${APACHE_CONFDIR}" ] ; then
    SUFFIX="-${APACHE_CONFDIR##/etc/apache2-}"
else
    SUFFIX=
fi
export APACHE_LOG_DIR=/var/log/apache2$SUFFIX

File /etc/apache2/sites-available/000-default.conf

ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
LF00
  • 27,015
  • 29
  • 156
  • 295