尝试使用 php 在 webroot 之外创建文件时“无法打开流:只读文件系统”

ran*_*ess 6 php.ini arch-linux php-fpm apache-2.4 nextcloud

我正在尝试在运行 arch linux(警报)的 RaspberryPi 3 上设置 nextcloud 一周。
我已经设置了 apache、php 和 php-fpm、postgresql 并从 AUR 安装了 nextcloud-testing(因为 nextcloud 17 不支持 php 7.4)。
apache 的 webroot 位于/srv/http但 nextcloud 安装到/usr/share/webapps/nextcloud.
我的虚拟主机:

<VirtualHost *:443>
DocumentRoot "/srv/http"
<Directory "/srv/http">
    Options FollowSymLinks
    AllowOverride None
    Require all granted
</Directory>
...
#ssl stuff
...
ScriptAlias /cgi-bin/ "/srv/http/cgi-bin/"
<Directory "/srv/http/cgi-bin">
    AllowOverride None
    Options None
    Require all granted
</Directory>

Alias /nextcloud /usr/share/webapps/nextcloud/
<Directory /usr/share/webapps/nextcloud>
    Options FollowSymlinks
    AllowOverride all
    Require all granted
</Directory>
</VirtualHost>
Run Code Online (Sandbox Code Playgroud)

https://mydomain/nextcloud在浏览器中访问时,错误消息是无法写入配置目录。检查这个使用的 php 代码is_writable(),所以为了调试这个我试过(我会在这一切工作后再次加强安全性):

  • chown -R http:http /usr/share/webapps/nextcloud
  • chmod -R 777 /usr/share/webapps/nextcloud
  • 目录/usr/share/webapps/usr/share并对/usr其他人拥有 x 权限
  • sestatus 返回 command not found
  • su -s /bin/bash http,然后echo test > /usr/share/webapps/nextcloud/test.txt工作
  • 设置open_basedir = //etc/php/php.ini并重新启动php-fpm 没有帮助

我创建了/srv/http/test.php

<VirtualHost *:443>
DocumentRoot "/srv/http"
<Directory "/srv/http">
    Options FollowSymLinks
    AllowOverride None
    Require all granted
</Directory>
...
#ssl stuff
...
ScriptAlias /cgi-bin/ "/srv/http/cgi-bin/"
<Directory "/srv/http/cgi-bin">
    AllowOverride None
    Options None
    Require all granted
</Directory>

Alias /nextcloud /usr/share/webapps/nextcloud/
<Directory /usr/share/webapps/nextcloud>
    Options FollowSymlinks
    AllowOverride all
    Require all granted
</Directory>
</VirtualHost>
Run Code Online (Sandbox Code Playgroud)

https://mydomain/test.php 显示

username: http
open_basedir: string(0) ""
/usr/share/webapps/nextcloud file is not writable and it has the following file permissions : 40777

Warning: fopen(/usr/share/webapps/nextcloud/test.txt): failed to open stream: Read-only file system in /srv/http/test.php on line 30
cannot fopen
Run Code Online (Sandbox Code Playgroud)

设置$myfile = "/srv/http";错误消息时,正如预期的
Warning: fopen(/srv/http/test.txt): failed to open stream: Permission denied in /srv/http/test.php on line 30
那样/srv/http归所有者所有,root并且对其他人没有写权限。当chmod o+w /srv/http脚本输出file is writable并将当前日期写入/srv/http/test.txt.

由于Read-only file system-warning for/usr/share/webapps/nextcloud我怀疑 arch、apache、php、php-fpm 或其他地方的安全设置或默认行为来限制对 php 的写访问/srv/http,但我无法弄清楚哪个设置以及如何包含/usr/share/webapps/nextcloud.
我想我可以将 nextcloud 移至/srv/http/,但我宁愿以正确的方式这样做,以免破坏软件包更新和其他事情。

所以问题是我怎样才能允许 php 在/usr/share/webapps/nextcloud.

编辑: 感谢诺弗的回答。这确实是拒绝访问的原因。我没有移动 nextcloud-instance 或ProtectSystem=full通过注释完全删除安全限制,而是为 php-fpm.service 创建了一个systemctl edit php-fpm.service包含以下内容的插入文件:

[Service]
ReadWritePaths=/etc/webapps/nextcloud/config /usr/share/webapps/nextcloud/apps /usr/share/webapps/nextcloud/data
Run Code Online (Sandbox Code Playgroud)

小智 10

我从以下链接中发现 Systemdphp-fpm服务可能被配置为阻止对某些文件夹和子文件夹的任何写入操作,并/usr受此影响。

您可能想像提到的那样移动 nextcloud 实例,或者您可能还想编辑 systemd 服务 ( /usr/lib/systemd/system/php-fpm.service) 并注释该行ProtectSystem=full。您将需要重新启动服务 ( sudo systemctl restart php-fpm)。

请注意,服务中的这一行是出于安全目的,因此您可能会受到一些攻击。

https://github.com/getgrav/grav/issues/2756