Apache2更改本地主机目录

Posted

技术标签:

【中文标题】Apache2更改本地主机目录【英文标题】:Apache2 changing localhost directory 【发布时间】:2015-05-29 12:46:52 【问题描述】:

我一直在安装 apache2 和 php5,我知道这是 var/www/html/index.php 中的 index.php 文件,但我想将其更改为 /home/pc/www/public/index.php 并且我在 etc/apache2/sites-available/000-default.conf 文件根目录中设置到“Doc​​umentRoot /home/pc/www/public/” 这是我的 conf 文件:

<VirtualHost *:80>
    # The ServerName directive sets the request scheme, hostname and port that
    # the server uses to identify itself. This is used when creating
    # redirection URLs. In the context of virtual hosts, the ServerName
    # specifies what hostname must appear in the request's Host: header to
    # match this virtual host. For the default virtual host (this file) this
    # value is not decisive as it is used as a last resort host regardless.
    # However, you must set it for any further virtual host explicitly.
    #ServerName www.example.com

    ServerAdmin webmaster@localhost
    DocumentRoot /home/pc/www/public/


    # Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
    # error, crit, alert, emerg.
    # It is also possible to configure the loglevel for particular
    # modules, e.g.
    #LogLevel info ssl:warn

    ErrorLog $APACHE_LOG_DIR/error.log
    CustomLog $APACHE_LOG_DIR/access.log combined

    # For most configuration files from conf-available/, which are
    # enabled or disabled at a global level, it is possible to
    # include a line for only one particular virtual host. For example the
    # following line enables the CGI configuration for this host only
    # after it has been globally disabled with "a2disconf".
    #Include conf-available/serve-cgi-bin.conf
</VirtualHost>

# vim: syntax=apache ts=4 sw=4 sts=4 sr noet

当我尝试在 firefox 中访问 localhost 时,出现此错误:

Forbidden

You don't have permission to access / on this server.
Apache/2.4.7 (Ubuntu) Server at localhost Port 80

但是当我将 DocumentRoot 设置为默认目录时,它可以工作。请帮忙。

【问题讨论】:

如果已解决,请发布解决方案,如果答案有帮助,请将其标记为已接受。 【参考方案1】:

Apache 用户(通常是apachenobody)是否有权访问该目录?确保您至少拥有该目录的o+r 权限。

【讨论】:

对不起,我不明白你想说什么 以 root 身份运行 chmod ugo+r /home/pc/www/public/ 并重新启动 Apache,看看是否能解决您的问题。 运行ls -al /home/pc/www/public,然后将输出添加到您的问题中。还要添加usergroup apache is running under.【参考方案2】:

OP 的解决方案。

我也需要更改 apache2.conf 中的目录。

这是我需要在 apache2.conf 文件中更改的代码:

 <Directory /home/pc/www/public/>
        Options Indexes FollowSymLinks
        AllowOverride None
        Require all granted
    </Directory>

【讨论】:

以上是关于Apache2更改本地主机目录的主要内容,如果未能解决你的问题,请参考以下文章

Apache2:启用的虚拟主机目录未加载,而是获取默认页面

具有两个子目录的 apache2 虚拟主机配置

当我在我的本地主机系统上安装 apache2 时,它显示了这个错误(Ubuntu 17.10)

MAC为Apache2服务器配置多个虚拟主机

Apache2.4.11 虚拟主机配置不工作

Apache2.2(win)基础 更改虚拟目录