我在MacOS X Lion 10.7.4上运行Apache / PHP。 我的目录结构设置如下:
/Users/achan/Sites/ lrwxrwx--- 1 achan staff 23B Apr 27 16:21 epwbst@ -> /Users/achan/dev/epwbst`
其中epwbst/是~/Sites内的符号链接。
如果我把test.php放在Sites/目录中,Apache就会正确地提供这个文件。 它发出phpinfo()像它应该。 如果我把相同的文件放在符号链接下,我得到这个错误:
[Mon May 28 14:47:13 2012] [error] [client ::1] PHP Warning: Unknown: failed to open stream: No such file or directory in Unknown on line 0 [Mon May 28 14:47:13 2012] [error] [client ::1] PHP Fatal error: Unknown: Failed opening required '/Users/achan/Sites/epwbst/test.php' (include_path='.:/usr/lib/php/pear') in Unknown on line 0
为了确保Apache能够正常工作,我在~/Sites/epwbst/下创build了一个testinghtml文件,并且按照预期的那样Apache进行了处理。
为什么Apache不能在我的symlinked目录下运行php?
我在这里粘贴了我的PHPconfiguration: http : //pastebin.com/gg27JyVZ
好吧,这让我疯狂了好几个小时 。 这是一个权限问题,但不是人们怎么想的。 问题在于符号链接本身的权限:
/Users/achan/Sites/ lrwxrwx--- 1 achan staff23B Apr 27 16:21 epwbst@ -> /Users/achan/dev/epwbst`
这里是rub: chmod通常不会改变符号链接的权限,因为(除了php5_module和其他一些超出这个答案范围的情况),这些权限在很大程度上是不相关的,因为在几乎所有的上下文中它们都被忽略。 这是修复:
chmod -h 755 /Users/achan/Sites/epwbst
注意-h 。 从手册页:
... -h If the file is a symbolic link, change the mode of the link itself rather than the file that the link points to. ...
出于某种原因, php5_module实际上关注了符号链接上的权限。 如果限制性太强, php5_module将会拒绝目标, 即使httpd用户可以使用/usr/bin/php读取并运行同一个目标 。
考虑以下:
% umask 027 % cd ~/Sites % mkdir bar % chmod 755 bar % ln -sv bar foo foo -> bar % ls -al ~/Sites/foo lrwxr-x--- 1 xyz xyz 3 Apr 22 13:17 foo -> bar % ls -adl ~/Sites/foo/. drwxr-xr-x 2 xyz xyz 68 Apr 22 13:17 /Users/xyz/Sites/foo/. % echo 'Hello!' >bar/hello.txt % chmod 644 bar/hello.txt % curl http://localhost/~xyz/bar/hello.txt Hello! % curl http://localhost/~xyz/foo/hello.txt Hello!
到现在为止还挺好。 现在考虑:
% echo '<?php phpinfo(); ?>' >bar/info.php % chmod 644 bar/info.php % curl http://localhost/~xyz/bar/info.php <html xmlns="http://www.w3.org/1999/xhtml"><head> ... </div></body></html> % curl http://localhost/~xyz/foo/info.php <br /> <b>Warning</b>: Unknown: failed to open stream: No such file or directory in <b>Unknown</b> on line <b>0</b><br /> <br /> <b>Fatal error</b>: Unknown: Failed opening required '/Users/xyz/Sites/foo/info.php' (include_path='.:') in <b>Unknown</b> on line <b>0</b><br />
嗯。 我的httpd以用户_www运行,所以让我们来看看用户是否可以阅读.../foo/info.php :
% sudo sudo -u _www cat ~/Sites/foo/info.php Password: <?php phpinfo(); ?>
对。 现在让我们看看用户是否可以运行 .../foo/info.php :
% sudo sudo -u _www /usr/bin/php ~/Sites/foo/info.php Password: phpinfo() PHP Version => 5.4.24 ... If you did not receive a copy of the PHP license, or have any questions about PHP licensing, please contact [email protected].
是?! WTF? 哎呀! 现在修复它:
% chmod -h 755 foo % curl http://localhost/~xyz/foo/info.php <html xmlns="http://www.w3.org/1999/xhtml"><head> ... </div></body></html>
砰。 完成。
所以,是的。 看来, php5_module做一些偏执和非标准的东西。 这可能已经被忽略了,因为umask经常默认为022 ,这至less会与755创build符号链接。 而且,在符号链接创build期间,许多文件系统(不是HFS +)在内核级强加777权限,而不考虑umask 。
您和我似乎是在HFS +上运行Apache + PHP的太阳系中的两个人,同时将我们的umask设置为比默认更严格的东西。 我敢打赌,你甚至会使用区分大小写的HFS +。 ; O)