如何更改 Apache 的 DocumentRoot?

如何更改 Apache 的 DocumentRoot?

我在 Ubuntu 20.04 上使用 Apache 2.4.41。

通过以下配置,我可以成功查看默认的 Ubuntu Apache 页面/var/www/html/index.html

<VirtualHost *:80>
        # Server Name
        ServerName example1.org
        ServerAlias dev.example2.org

        DocumentRoot "/var/www/html"

</VirtualHost>

然而,如果我试图改变DocumentRoot指令,

<VirtualHost *:80>
        # Server Name
        ServerName example1.org
        ServerAlias dev.example2.org

        DocumentRoot "/home/rufus/www/html"

</VirtualHost>

那么任何查看该文件的尝试/home/rufus/www/html/index.html都会返回 403 Forbidden 错误。

两个目标目录都具有相同的所有权和权限,直至根目录。来自ls -lh

drwxr-xr-x 14   root root  /var/
drwxr-xr-x 3    root root  /var/www/
drwxr-xr-x 2    root root  /var/www/html/
-rw-r--r-- 1    root root  /var/www/html/index.html

drwxr-xr-x 10 root root  /home/
drwxr-xr-x 11 root root  /home/rufus/
drwxr-xr-x 13 root root  /home/rufus/www/
drwxr-xr-x 10 root root  /home/rufus/www/html/
-rw-rw-r-- 1  root root  /home/rufus/www/html/index.html

我在两种情况下都尝试了相同的 URL,http://dev.example2.orghttp://dev.example2.org/index.html。每次配置更改后我都会重新加载 Apache。

这个问题和我的几乎一模一样。唯一的答案建议添加一个块,例如

<VirtualHost *:80>
        # Server Name
        ServerName example1.org
        ServerAlias dev.example2.org

        DocumentRoot "/home/rufus/www/html"

        <Directory /home/rufus/www/html>
            Options FollowSymLinks 
            AllowOverride All
            Order deny,allow
            Allow from all
        </Directory>

</VirtualHost>

我尝试了这两种方法Allow from allRequire all granted如评论中所述),每次更改后都重新加载 Apache。第二种配置仍然给我一个 403 错误。

为什么第二个出现 403 Forbidden 错误,我该如何更改我的 DocumentRoot?

答案1

您是否在默认文档根目录中设置了新目录?应该在第 292 行左右。

288 # DocumentRoot: The directory out of which you will serve your
 289 # documents. By default, all requests are taken from this directory, but
 290 # symbolic links and aliases may be used to point to other locations.
 291 #
 292 DocumentRoot "/var/www/html"
 293
 294 #
 295 # Each directory to which Apache has access can be configured with respect
 296 # to which services and features are allowed and/or disabled in that
 297 # directory (and its subdirectories).
 298 #
 299 # First, we configure the "default" to be a very restrictive set of
 300 # features.
 301 #
 302 <Directory />
 303     Options FollowSymLinks
 304     AllowOverride None
 305 </Directory>
 306
 307 #
 308 # Note that from this point forward you must specifically allow
 309 # particular features to be enabled - so if something's not working as
 310 # you might expect, make sure that you have specifically enabled it
 311 # below.
 312 #
 313
 314 #
 315 # This should be changed to whatever you set DocumentRoot to.
 316 #
 317 <Directory "/var/www/html">

相关内容