使用 php 写入属于不同用户的目录中的文件
我有一台托管多个域的服务器。 从其中一个域,使用 php 脚本,我希望能够将几行附加到其他域的 .htaccess 中。
例如,来自 masterdomain.com 我想将一些行附加到 otherdomain.com 的 .htaccess 中。因此,从 /home/masterdomain/www/ 中的 php 文件中,我想将几行附加到位于 /home/otherdomain/www 的 .htaccess 中。
为此,我编写了一个 shell 脚本,当我以 root 身份运行 shell 脚本时,它可以工作,但是当通过 php 运行它时, exec('./write_htaccess.sh')
它不工作,什么也没有发生了。我检查了一下,没有返回任何错误。
我已经在 write_htaccess.sh 上尝试过 chmod u+s 来尝试让它每次都以 root 身份运行,但这也不起作用,但我可能错误地设置了 s 位。
我怎样才能做到这一点?我是否必须授予 php root 权限,最好的方法是什么?
I have a server that hosts several domains.
From one of the domains, using a php script I want to be able to append a few lines to the .htaccess of other domains.
For example from masterdomain.com I want to append some lines to the .htaccess of otherdomain.com. So from a php file in /home/masterdomain/www/ I want to append a few lines to the .htaccess located at /home/otherdomain/www.
To do this I have written a shell script, when I run the shell script as root, it works but when running it via php, exec('./write_htaccess.sh')
it's not working, nothing happends. I checked and there are no errors returned.
I have tried chmod u+s on write_htaccess.sh to try and make it run as root each time but that didn't work either, I might have set the s bit wrongly though.
How could I achieve this? Do I have to give php root priviledges, what would be the best way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
PHP 可能由运行 Web 服务器的用户运行;我猜当你说服务器时你指的是网络服务器。在 Red Hat 或 Fedora 上,即 apache(除非您使用不同的 Web 服务器),其他版本的 linux 可能会使用 www 或其他一些用户。当您从 PHP 运行 shell 脚本时,它是由用户 apache 执行的。该用户需要对 .htaccess 文件的写入权限,或者需要属于对 .htaccess 文件具有写入权限的组。
尝试将 .htaccess 文件的组所有权更改为 apache (或其他)并使其组可写。这可以避免使用 sudo 或 setuid 位。当然,这意味着该组中的任何人都可以修改 .htaccess 文件。
PHP is probably being run by whichever user your web server is running under; I'm guessing when you say server you mean web server. On Red Hat or Fedora that is apache (unless you're using a different web server), other flavors of linux might use www or some other user. When you run your shell script from PHP it's being executed by the user apache. That user needs write permissions to the .htaccess file, or needs to be in a group with write access to the .htaccess file.
Try changing the .htaccess file's group ownership to apache (or whatever) and making it group writable. That avoids mucking about with sudo or setuid bits. Of course, it means anyone in that group can modify the .htaccess file.
将 php 添加到 sudoers 并允许它只做一件事怎么样?
How about adding php to sudoers and allow it to do just the one thing.
您可以使用 php 连接到 sftp/ftp/ssh 并以您要编辑的文件的用户身份进行身份验证。
You can connect to sftp/ftp/ssh with php and authenticate as the user which files you are trying to edit.
Bash(和其他 shell)将其他命令作为真实用户 ID 而不是有效用户 ID 来调用 - 因此 's' 标志无效。使用 sudo 调用该程序,它应该按预期工作,或者将脚本设置为从 [x]inetd 运行(使用适当的访问控制)。
Bash (and other shells) invoke other commands as the real user id not the effective user id - hence the 's' flag has no effect. Invoke the program using sudo and it should work as expected, or setup the script to run from [x]inetd (with appropriate access controls).
您可以使文件组可写,并将两个用户添加到同一组吗?
Can you make the file group-writable, and add both users to the same group?
您可以向 apache 用户授予 sudo 权限,无需密码,并且只允许一个命令,即您的
write_htaccess.sh
脚本。然后使用 exec('sudo ./write_htaccess.sh') 。不过,就我个人而言,我可能只会开放文件和目录权限。
You could grant sudo permission to the apache user with no password and only allow one command which would be your
write_htaccess.sh
script. Then useexec('sudo ./write_htaccess.sh')
.Personally, I would probably just open up file and directory permissions, though.