这是因为apache运行为apache用户和/var/www/html
由Amazon Linux AMI中的根拥有。您可以按照Frank的建议更改所有权/权限,也可以使用userdirs。
在我看来,你想要从主目录(~
)方便地访问网络服务器的文件夹。我想在我的EC2上这样做,并决定使用每个用户的网页目录(mod_userdir)。
此功能可让您将HTTP服务器的部分空间保留在用户拥有的目录中。每个用户都有自己的目录,默认位于/home/username/public_html
。该目录中的页面,脚本和其他文件可通过向您的域添加/~username
访问互联网。此外,您可以将httpd.conf中该文件夹的名称从public_html
更改为其他内容,如gitRepo
。在这种情况下,如果/home/ec2-user/gitRepo/index.html
中有index.html
文件,则公众可以通过http://ec2-hostname.aws.amazon.com/~ec2-user/index.html
访问该文件,并由ec2-user拥有,这对于从用户级别编辑文件非常方便。
要在EC2上进行设置(假设您要使用的文件夹名称为“gitRepo”),请使用nano /etc/httpd/conf/httpd.conf
打开Apache配置文件并向下滚动,直到看到<IfModule mod_userdir.c>
。然后改变这部分如下所示:
<IfModule mod_userdir.c>
#
# UserDir is disabled by default since it can confirm the presence
# of a username on the system (depending on home directory
# permissions).
#
UserDir enabled all
#
# To enable requests to /~user/ to serve the user's public_html
# directory, remove the "UserDir disabled" line above, and uncomment
# the following line instead:
#
UserDir gitRepo
</IfModule>
之后,你应该是好去,但确保权限设置正确:
chmod 711 /home/ec2-user
chmod 755 /home/ec2-user/gitRepo
chown -R ec2-user /home/ec2-user/gitRepo
最后重新加载web服务器这样的:
sudo service httpd reload
'apache'用户(或等效)没有读取您的主目录的权限。 Chmod/chown目标目录以授予相关用户权限。顺便说一句,你可能会在'/ var/log/httpd/error_log'或httpd记录错误的地方找到一个稍微有点显示的消息。 – 2012-07-30 22:12:00
@FrankFarmer当我尝试使用sudo su apache切换到apache用户时,我收到以下消息:此帐户当前不可用。你有没有经历过这个? – Apollo 2012-07-31 00:07:03
是的。默认情况下,你不能su apache,因为apache用户通常将shell设置为'nologin'。您可以在'/ etc/passwd'中将用户的shell更改为sh,但这通常是一个糟糕的想法。一般来说,su-ing到apache应该不是必需的,特别是如果你只是想chown。''sudo chown apache:apache〜/ dirIWant /'就足够了,尽管将目录移出你的主目录会更明智一些。 – 2012-07-31 00:59:07