我们有一个开箱即用的PHP应用程序在子域上运行,它正在其DocumentRoot中记录错误:
/var/www/appname/path/to/document/root/php-errors.log
…而不是我们希望错误日志去哪里:
/var/www/appname/logs/php-errors.log
由于我不想更改现成应用程序中的代码,也不想为所有子域更改错误日志位置(即文件 php.ini),那么如何仅在与子域相关的Apache配置文件中完成此操作呢?
<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>
如果您希望使用php-fpm(我最初想发布的内容)来完成此操作,以下是具体步骤:
确保您已经以root用户登录或者使用sudo
命令执行所有操作。
进入您的php-fpm目录*,使用以下命令:
cd /etc/php/fpm/
[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/
user
和group
的值,默认设置为www-data
。查找术语catch_workers_output
,确保其被取消注释并设置为yes
,如下所示:catch_workers_output = yes
现在你需要创建错误日志文件,并确保php-fpm可以访问它。这就是为什么你需要注意先前编辑文件时user
和group
的值。为你在编辑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
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
/etc/php-fpm.d
位置时,管理和自动化会更容易。我会做类似这样的事情:999-php-logs-example.com.conf
,然后我会添加类似以下内容:
[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
。抱歉,无法添加多行代码块。 - Jeremy我像这样配置它:
# 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
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
php_value
在PHP-FPM中不起作用,因为Apache没有加载libphp模块。 - bilogic