PHP move_uploaded_file 失败

Leo*_*rdo 0 nginx php-fpm centos7 php7

这是一个常见问题,但我仔细检查了建议的解决方案,但没有成功。

这些是来自 PHP 的错误:

Warning: move_uploaded_file(images/img01.jpg): failed to open stream: Permission denied in /usr/share/nginx/html/media/test.php on line 28

Warning: move_uploaded_file(): Unable to move '/tmp/phpRvUCVx' to 'images/img01.jpg' in /usr/share/nginx/html/media/test.php on line 28
Run Code Online (Sandbox Code Playgroud)

服务器已经安装了 nginx 和 php 7.3 (php-fpm)

上传文件夹的权限:

 drwxrwxrwx.  2 nginx  nginx       6 Apr  5 03:11 images
Run Code Online (Sandbox Code Playgroud)

信息来自ps aux | grep php

centos   24211  0.0  0.0 112708   980 pts/0    S+   16:01   0:00 grep --color=auto php
root     24674  0.0  0.6 285532 11452 ?        Ss   Apr04   0:04 php-fpm: master process (/etc/opt/remi/php73/php-fpm.conf)
nginx    24675  0.0  0.4 287740  8724 ?        S    Apr04   0:00 php-fpm: pool www
nginx    24676  0.0  0.4 287740  8720 ?        S    Apr04   0:00 php-fpm: pool www
nginx    24677  0.0  0.4 287740  8684 ?        S    Apr04   0:00 php-fpm: pool www
nginx    24678  0.0  0.5 287916  9232 ?        S    Apr04   0:00 php-fpm: pool www
nginx    24679  0.0  0.5 287916  9308 ?        S    Apr04   0:00 php-fpm: pool www
nginx    25107  0.0  0.4 287740  8716 ?        S    Apr04   0:00 php-fpm: pool www
Run Code Online (Sandbox Code Playgroud)

信息来自ps aux | grep nginx

root     15041  0.0  0.1 125116  2324 ?        Ss   Apr04   0:00 nginx: master process /usr/sbin/nginx
nginx    15042  0.0  0.2 125956  5328 ?        S    Apr04   0:00 nginx: worker process
nginx    15043  0.0  0.2 125956  5328 ?        S    Apr04   0:00 nginx: worker process
nginx    24675  0.0  0.4 287740  8724 ?        S    Apr04   0:00 php-fpm: pool www
nginx    24676  0.0  0.4 287740  8720 ?        S    Apr04   0:00 php-fpm: pool www
nginx    24677  0.0  0.4 287740  8684 ?        S    Apr04   0:00 php-fpm: pool www
nginx    24678  0.0  0.5 287916  9272 ?        S    Apr04   0:00 php-fpm: pool www
nginx    24679  0.0  0.5 287916  9308 ?        S    Apr04   0:00 php-fpm: pool www
nginx    25107  0.0  0.4 287740  8716 ?        S    Apr04   0:00 php-fpm: pool www
centos   26097  0.0  0.0 112712   976 pts/0    S+   16:39   0:00 grep --color=auto nginx
Run Code Online (Sandbox Code Playgroud)

PHP-FPM 的配置

user = nginx
group = nginx
listen = /var/run/php73-fpm/php73-fpm.sock
listen.owner = nginx
listen.group = nginx
Run Code Online (Sandbox Code Playgroud)

我错过了什么?提前致谢

Mic*_*ton 5

您在这里遇到了多个问题,其中一些问题可能是您在尝试解决原始问题时自我介绍的。

首先,您的 PHP 进程似乎以 nginx 用户身份运行。这不是默认配置,不推荐使用。您应该让它以自己最初设置的用户 ID 运行。

其次,您images目录的权限允许所有用户对其进行写入。这显然是一个坏主意,永远不应该这样做,即使是“测试”也不行。适当地设置所有权和权限。如果您想到chmod 777可能对您有帮助的想法,请记住您走错了路。

第三,您似乎已将您的网站文件放在/usr/share/nginx/html. 您不应将此目录用于您自己的文件;它仅适用于 nginx 附带的默认文件。改用下面的目录/srv,例如/srv/www. 还要避免/var/www,这是为 Web 服务器默认文件(通常是 Apache httpd 提供的文件)保留的另一个目录。

最后,对于眼前的问题,SELinux 不允许 nginx 或 php-fpm 写入随机目录。您需要通过将其默认上下文httpd_sys_rw_content_t设置为,然后设置任何现有文件的上下文来告诉 SELinux 该目录及其内容应该是可写的。例如:

semanage fcontext -a -t httpd_sys_rw_content_t "/srv/www/wherever/images(/.*)?"
restorecon -rv /srv/www/wherever/images
Run Code Online (Sandbox Code Playgroud)