這是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文件,前端刷新檢查。