为apache配置虚拟主机,在httpd-vhosts.conf文件中进行简单设置,启动apache进行访问,却出现了You don’t have permission to access / on this server的提示,原因是我的虚拟主机目录为非apache安装目录下的 .htaccess,所以违反了apache对默认对网站根访问权限。如下所示:
apache权限错误

apache的默认虚拟主机根目录地址为/Library/WebServer/Documents/ , 方法如下:

  1. 更改 Apache 默认网站根目录需更改 DocumentRoot、Directory .
  2. 还要记得改一下权限,将 httpd.conf 中的 AllowOverride none,Require all granted ,全部都改成 AllowOverride All,Require all granted ..
  3. Options指令可以在Apache服务器核心配置(server config)、虚拟主机配置(virtual host)、特定目录配置(directory)以及.htaccess文件中使用。Options指令的主要作用是控制特定目录将启用哪些服务器特性。
    apache的默认虚拟主机根目录地址
  4. Options FollowSymLinks为禁止Apache显示该目录结构,以上问题可以通过下边两种方式来解决:
  • 第一,添加Indexes: Options Indexes FollowSymLinks
  • 第二,将其注释,改为: Options All
  • 注意:在Indexes前,加 + 代表允许目录浏览;加 – 代表禁止目录浏览.
    apache的默认虚拟主机根目录地址
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    <Directory />
    AllowOverride All
    Require all granted
    </Directory>

    DocumentRoot "/Users/UserName/Documents/object"
    <Directory "/Users/UserName/Documents/object">

    # Options FollowSymLinks Multiviews
    # MultiviewsMatch Any
    Options All

    AllowOverride All

    # Controls who can get stuff from this server.
    Require all granted
    </Directory>

重启Apache: sudo apachectl restart , 在浏览器输入对应地址即可.