How can I set the location of the PHP error log in the apache conf file?

We have a finished PHP application running on a subdomain and it logs errors in it DocumentRoot:

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

      

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

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

      

Since I don't want to change the code in the finished application, and I don't want to change the location of the error log for all subdomains (read: php.ini), how can I only do this in the Apache conf file pertaining to the subdomain?

+4


source to share


3 answers


From: error_log for 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>

      

+7


source


For those looking to do it with php-fpm (which I originally wrote about), here's how you do it:

Make sure you are logged in as root or use sudo

for all your commands.

Change to php-fpm * directory using below command:

cd /etc/php/fpm/

      

In this directory, edit the php.ini file and add the following at 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 an error log along the way (handy if you are 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

      

Now you need to change to the pool directory * using the command below:

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

      



In this directory, edit the www.conf file. Note the values ​​for user

and group

in this file, whose default settings are www-data

for both. Search for a term catch_workers_output

and make sure it's not catch_workers_output

and is set to a value yes

like so:

catch_workers_output = yes

      

Now you need to create an error log file and make sure 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 owner, 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 the php-fpm service using the ** command below:

service php-fpm restart

      


* Note. If, like me, you install the declared versions of php-fpm, the directory paths change (for example) to 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/

      

** Service accepts a specific versioned name if you have installed the declared versions and you would need to use (for example) the following:

service php5.6-fpm restart

service php7.1-fpm restart

      

+5


source


I set it up like this

/etc. / apache 2 / envvars

# for supporting multiple apache2 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

      

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

ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined

      

+1


source







All Articles