如何在 Apache 配置文件中设置 PHP 错误日志位置?

How do you set the PHP error log location in the Apache configuration file?

我们在子域上有一个开箱即用的 PHP 应用程序 运行,它在其 DocumentRoot 中记录错误:

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

...而不是我们希望错误日志去的地方:

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

因为我不想更改开箱即用的应用程序中的代码,也不想更改 all 的错误日志位置子域(读取:文件 php.ini),我怎样才能在与子域相关的 Apache 配置文件中执行此操作?

来自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>

我是这样配置的:

文件/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

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

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

对于那些希望使用 php-fpm(我最初是 post 的意思)执行此操作的人,请按以下步骤操作:

确保您以 root 身份登录,或使用 sudo 执行所有命令。

使用以下命令进入您的 php-fpm 目录*:

cd /etc/php/fpm/

在该目录中,编辑您的 php.ini 文件并将以下内容添加到末尾:

如果要通过主机设置错误日志

[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

如果要按路径设置错误日志 (如果您在有 IP 地址但没有域的服务器上工作,则很方便)

[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

您现在需要使用以下命令进入池目录*:

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

在该目录中,编辑文件 www.conf。请注意该文件中 usergroup 的值,两者的开箱即用设置均为 www-data。查找术语 catch_workers_output 并确保它未被注释并设置为 yes,如下所示:

catch_workers_output = yes

现在您需要创建错误日志文件并确保 php-fpm 可以访问它。这就是为什么您需要记下先前文件编辑中 usergroup 的值。为您在编辑 php.ini 时设置的每个 HOST/PATH 创建一个错误日志文件,并赋予它们适当的权限和所有权,例如:

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

最后,使用下面的命令**重启您的 php-fpm 服务:

service php-fpm restart

* 注意:如果像我一样安装声明版本的 php-fpm,目录路径将更改为(例如)以下内容:

/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 php5.6-fpm restart

service php7.1-fpm restart