我有一个安装了 nginx 和 php-fpm (7.2) 的 Ubuntu18.04 Web 服务器。
有 6 个池,每个池都有自己的用户和组:
/etc/php/7.2/fpm# grep -r ^user *
php.ini:user_dir =
pool.d/dev3.website.com.conf:user = dev3_app
pool.d/dev1.website.com.conf:user = dev1_app
pool.d/dev4.website.com.conf:user = dev4_app
pool.d/dev6.website.com.conf:user = dev6_app
pool.d/dev5.website.com.conf:user = dev5_app
pool.d/dev2.website.com.conf:user = dev2_app
/etc/php/7.2/fpm# grep -r ^group *
pool.d/dev3.website.com.conf:group = dev3_app
pool.d/dev1.website.com.conf:group = dev1_app
pool.d/dev4.website.com.conf:group = dev4_app
pool.d/dev6.website.com.conf:group = dev6_app
pool.d/dev5.website.com.conf:group = dev5_app
pool.d/dev2.website.com.conf:group = dev2_app
每个站点都运行一个 laravel 应用程序,存储目录使组可写:
/var/www/dev3.website.com# ls -la
total 2236
drwxr-xr-x 20 root dev3_app 4096 Jul 17 21:39 .
drwxr-xr-x 9 root root 4096 Jul 17 21:33 ..
...
drwxrwxr-x 7 root dev3_app 4096 Jul 17 21:29 storage
nginx 通过 TCP 端口连接:
server {
listen 80;
server_name dev3.website.com;
root /var/www/dev3.website.com/public;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ [^/]\.php(/|$) {
fastcgi_split_path_info ^(.+?\.php)(/.*)$;
if (!-f $document_root$fastcgi_script_name) {
return 404;
}
fastcgi_pass 127.0.0.1:9001;
fastcgi_index index.php;
# include the fastcgi_param setting
include fastcgi_params;
# SCRIPT_FILENAME parameter is used for PHP FPM determining
# the script name. If it is not set in fastcgi_params file,
# i.e. /etc/nginx/fastcgi_params or in the parent contexts,
# please comment off following line:
fastcgi_param SCRIPT_FILENAME
$document_root$fastcgi_script_name;
}
}
有了这个配置,我得到
The stream or file "/var/www/dev3.website.com/storage/logs/laravel-2019-07-17.log" could not be opened: failed to open stream: Permission denied
当我在存储目录上运行 chmod a+w 时,它可以工作。
我还运行了 ps -ef |grep php:
root 2468 1 0 00:53 ? 00:00:05 php-fpm: master process (/etc/php/7.2/fpm/php-fpm.conf)
root 11897 10961 0 22:12 pts/0 00:00:00 grep --color=auto php
我怀疑它以 root 用户身份运行“主进程”,但对于每个池,它以指定用户身份运行“工作”进程。您必须对日志目录进行 chmod 才能使其工作这一事实进一步支持了这一点。
您可以采取的故障排除步骤是让其中一个站点将文件写入其中
/tmp/test
,然后检查哪个用户拥有它。您还可以编写一个在其中一个池中休眠几分钟的循环,然后运行您的
ps -ef | grep php
命令以查看“主进程”是否按照您期望的用户生成了一个进程。