macOS Big Sur下如何将Sites文件夹设为http://localhost的Apache默认根目录
Apache配置Sites文件夹为localhost根目录的解决步骤
- 第一步:修正
httpd-vhosts.conf内的虚拟主机配置
你当前的配置中ServerName设置为dummy-host2.example.com,访问localhost时无法匹配到该虚拟主机规则,需要将配置替换为以下内容(注意将myusername替换为你实际的 macOS 用户名):
<VirtualHost *:80> ServerAdmin webmaster@localhost DocumentRoot "/Users/myusername/Sites/" ServerName localhost <Directory "/Users/myusername/Sites/"> Options Indexes FollowSymLinks MultiViews AllowOverride All Require all granted </Directory> ErrorLog "/private/var/log/apache2/localhost-error_log" CustomLog "/private/var/log/apache2/localhost-access_log" common </VirtualHost>
添加Directory配置是为了避免访问时出现403权限错误。
- 第二步:确认主配置文件已开启虚拟主机支持
编辑Apache主配置文件/etc/apache2/httpd.conf,检查以下两行配置前的#注释符是否已经删除:
LoadModule vhost_alias_module libexec/apache2/mod_vhost_alias.so Include /private/etc/apache2/extra/httpd-vhosts.conf
如果此前使用的是单站点模式,也可以直接修改主配置文件中默认的DocumentRoot和对应Directory的路径为/Users/myusername/Sites/,无需配置虚拟主机。
- 第三步:验证配置语法并重启Apache服务
执行以下命令检查配置是否存在语法错误:sudo apachectl configtest
返回Syntax OK后,执行重启命令使配置生效:sudo apachectl restart - 第四步:权限异常排查
如果重启后仍无法访问,检查Sites文件夹的读取权限,执行以下命令开放权限:chmod 755 ~/Sites
内容的提问来源于stack exchange,提问作者idzczakp
相关产品推荐
相关产品推荐

