Laravel Log 权限问题

2019-03-22 11:36:39 阅读:9 编辑

日志权限问题

PHP-fpm, crontab, supervisor 都要用 www 用户,不然会造成文件无法写入的问题

可以通过 ps -aux 来查看是由哪个用户运行的 (PHP-fpm, supervisor)

PHP-fpm ( 默认是 www 用户)

cat /usr/local/PHP/etc/PHP-fpm.conf
[www]
listen = /dev/shm/PHP-cgi.sock
listen.backlog = -1
listen.allowed_clients = 127.0.0.1
listen.owner = www
listen.group = www
listen.mode = 0666
user = www
group = www

crontab

/etc/crontab
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root

# For details see man 4 crontabs

# Example of job definition:
# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
# *  *  *  *  * user-name  command to be executed
  *  *  *  *  * www /usr/local/PHP/bin/PHP /usr/share/nginx/HTML/91 yunying/artisan schedule:run >> /dev/null 2>&1
重启 cron
service crond restart

supervisor

/etc/supervisord.d/*.ini
vi artisan.ini
[program:91 yunying-queue]

process_name=%(program_name) s_%(process_num) 02 d

command=/usr/local/PHP/bin/PHP /usr/share/nginx/HTML/91 yunying/artisan queue:work   --tries=1 

autostart=true

autorestart=true

user=www

numprocs=1

redirect_stderr=true

stdout_logfile=/tmp/91 yunying-queue.log

问题分析:

项目突然间验证码无法使用,查找原以为是 cache 没有权限。

chmod -R 777 storage

就可以了。 过了几天,突然间又不行了,就觉得奇怪,为什么昨天又可以了。 后来查看 nginx 的 errorlog, 没有权限写入 Laravel log, 当时猜测是不是 Laravel log 又其它用户创建,使得 PHP-fpm 的 www 用户没有权限写入。 因为项目还有其它两种方式执行代码,计划任务 crontab 与 supervisor 来确保队列的正确运行。然后发现 crontab 与 supervisor 均用 nobody 用户来运行。 这样一来就解释的通。当跨天时刚才 crontab 或 superisor 先执行到有写入 log 的代码。则会生成 nobody 用户的 log. 这时再访问 Web,PHP-fpm 的 www 用户将没有权限写入当天日志的文件。 创建该文件的用户与下次想要写入的用户不同

[root@iZm5 ed1 ybubtyazfdqo5 mzZ logs]# ls -ll
total 1180
-rwxrwxrwx 1 nobody nobody  57193 Mar 17 23:53 Laravel-2019-03-17.log
-rwxrwxrwx 1 www    www     38275 Mar 18 23:54 Laravel-2019-03-18.log
-rwxrwxrwx 1 www    www     76203 Mar 19 23:55 Laravel-2019-03-19.log
-rwxrwxrwx 1 www    www     83893 Mar 20 23:44 Laravel-2019-03-20.log
-rwxrwxrwx 1 www    www     21703 Mar 21 23:55 Laravel-2019-03-21.log
-rwxrwxrwx 1 nobody nobody   6088 Mar 22 11:43 Laravel-2019-03-22.log