从 php 获取 apache linux 用户
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/805907/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get apache linux user from php
提问by Mario Mueller
I'm on a foreign linux system and need to determine the user that apache runs on (and so does php).
我在一个外国的 linux 系统上,需要确定运行 apache 的用户(php也是如此)。
The aim: I need to get the owner of the script (this is no problem as I can use SplFileInfo) and compare it to the owner of the apache process.
目标:我需要获取脚本的所有者(这没有问题,因为我可以使用 SplFileInfo)并将其与 apache 进程的所有者进行比较。
I'm open to any alternative proposals.
我对任何替代建议持开放态度。
Regards, Mario
问候, 马里奥
Edit: Additional info:
编辑:附加信息:
The script is a thumbnail generator, that uses an XML file to generate thumbs from larger images. The script needs to create folders and write files. As I cannot influence the php configuration and I do not have any shell access, this has to be done very silently. The creation process stopps via exception and sends a mail on failue. As most of php's function cannot throw exceptions on failue, I need some manual checks to determine the environment I'm in. Therefore I need the apache user to compare it to some directory or fileowner.
该脚本是一个缩略图生成器,它使用 XML 文件从较大的图像生成缩略图。该脚本需要创建文件夹和写入文件。由于我无法影响 php 配置并且我没有任何 shell 访问权限,因此必须非常安静地完成此操作。创建过程通过异常停止并在失败时发送邮件。由于大多数 php 函数不能在失败时抛出异常,我需要一些手动检查来确定我所处的环境。因此我需要 apache 用户将其与某个目录或文件所有者进行比较。
回答by grantc
回答by Anonymous
see posix_getuid() and posix_getpwuid()
参见 posix_getuid() 和 posix_getpwuid()
回答by Jamie Carl
Some complicated answers here.
这里有一些复杂的答案。
This works for me:
这对我有用:
$user = getenv('APACHE_RUN_USER');
Not sure if this is just a new thing that been added to apache since this question was asked but it's definitely there now.
不确定这是否只是自提出此问题以来添加到 apache 的新事物,但现在肯定存在。
回答by zombat
phpinfowill dump a lot of system information. For apache2 installs, there is a section that displays the apache user and group ids. Try creating a php script that just has one line, a call to phpinfo(), and open it in your web browser.
phpinfo会转储很多系统信息。对于 apache2 安装,有一个部分显示 apache 用户和组 ID。尝试创建一个只有一行的 php 脚本,调用 phpinfo(),然后在 Web 浏览器中打开它。
回答by atmacola
Some php script mustbe run on apache user (cli), whoami is not appropriate in that case. Here is my solution :
某些 php 脚本必须在 apache 用户 (cli) 上运行,在这种情况下 whoami 不合适。这是我的解决方案:
$output = exec('apachectl -S 2>/dev/null | grep User');
$apacheUser = preg_match('/name="([^"]+)"/', $output, $match) ? $match[1] : 'www-data';

