问题:一个正常运行的系统,突然间出现了错误500 。
检查发现是thinkphp框架的runtime中日志文件夹所属用户变成了root。导致无法将日志写入日志文件,然后报错。
第一次解决方法:chown www.www runtime -R
修改一下文件夹权限,就可以正常访问了。或者删除runtime文件夹,也可以正常访问。
但是过了几天之后,又出现了这种错误。
查找原因,发现是有cli任务,是root用户执行。在正常访问执行之前,cli任务先执行了任务,导致日志是root用户创建,然后正常访问的www用户没有权限来写入日志。
第二次解决方法:
/thinkphp/library/think/log/driver/File.php 修改此文件的write方法。
protected function write($message, $destination, $apart = false, $append = false)
{
…
if (PHP_SAPI == ‘cli’) {
$message = $this->parseCliLog($info);
} else {
// 添加调试日志
$this->getDebugLog($info, $append, $apart);
$message = $this->parseLog($info);
}
//return error_log($message, 3, $destination);
/** 解决root生成的文件,www用户没有写权限的问题 by Werben 20190704 begin */
if (!is_file($destination)) {
$first = true;
}
$ret = error_log($message, 3, $destination);
try {
if (isset($first) && is_file($destination)) {
chmod($destination, 0777);
unset($first);
}
} catch (\Exception $e) { }
return $ret;
/** 解决root生成的文件,www用户没有写权限的问题 by Werben 20190704 end */
…
}
(代码来自于CSDN)
原理是如果文件没有写入成功,就将目标文件权限改为777 。