每月一号前端报500错
每月一号前端报500错,Linux系统
这是runtime 的权限问题,请检查守护进程的启动用户应该设置成www;
方法一: 命令操作:
修改目录权限:)777 www组
/runtime
修改文件夹及子文件夹权限可以用命令: chmod -R 777 runtime
永久解决方法:
在启动workermam时使用 sudo -u www php think workerman start —d 命令
宝塔操作:
两种方法:
1.直接系统目录文件设置权限
(1).找到知识付费项目目录
(2).点击权限设置,设置权限为777,点击确定
2.命令操作
(1).目录切换到知识付费项目根目录,点击终端
(2).打开终端后切到知识付费根目录,输入命令执行即可
方法二:代码修改
由于www用户和root用户(比如command的cli进程日志)都有可能对log文件进行读写。
如果是由www用户创建的log文件,不会出任何问题。
但是如果是先由root用户创建的log文件,然后再到www用户角色去写,就会出问题了
因为一般默认创建的log文件的权限是 -rw-r—r-
也就是www没有权限去写入root用户创建的log文件。
网上的方法大体就是像下面代码一样在mkdir的时候修改目录的权限
修改文件:\thinkphp\library\think\log\driver\File.php里的save()函数
public function save(array $log = [], $append = false)
{
$destination = $this->getMasterLogFile();
$path = dirname($destination);
if (PHP_SAPI != 'cli') {
!is_dir($path) && mkdir($path, 0755, true);
}else{
!is_dir($path) && mkdir($path, 0777, true) && chmod($path, 0777);
}
// !is_dir($path) && mkdir($path, 0755, true);
$info = [];
foreach ($log as $type => $val) {
foreach ($val as $msg) {
if (!is_string($msg)) {
$msg = var_export($msg, true);
}
$info[$type][] = $this->config['json'] ? $msg : '[ ' . $type . ' ] ' . $msg;
}
if (!$this->config['json'] && (true === $this->config['apart_level'] || in_array($type, $this->config['apart_level']))) {
// 独立记录的日志级别
$filename = $this->getApartLevelFile($path, $type);
$this->write($info[$type], $filename, true, $append);
unset($info[$type]);
}
}
if ($info) {
return $this->write($info, $destination, false, $append);
}
return true;
}
但是上面只能修改文件夹的权限,并没有修改文件夹下具体的.log文件的权限。
修改文件:\thinkphp\library\think\log\driver\File.php里的write()函数
protected function write($message, $destination, $apart = false, $append = false)
{
// 检测日志文件大小,超过配置大小则备份日志文件重新生成
$this->checkLogSize($destination);
// 日志信息封装
$info['timestamp'] = date($this->config['time_format']);
foreach ($message as $type => $msg) {
$info[$type] = is_array($msg) ? implode("\r\n", $msg) : $msg;
}
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;
}
注:方法修改完成后,删除runtime文件,前端刷新检查。