修改mac自带apache的根目录


macOS版本:Catalina version 15.15.6
Server version: Apache/2.4.41 (Unix)

问题描述:

我想用macOS自带的apache写php程序,于是我正常启动apache,但是出现以下错误

AH00558: httpd: Could not reliably determine the server’s fully qualified domain name, using wangMacBook-Pro.local. Set the ‘ServerName’ directive globally to suppress this message

原因未知,我的apache不能够正常工作,我尝试解决这个问题,但一直无果。
当我使用这条命令,可以成功启动apache

sudo /usr/sbin/apachectl -start

apache能够正常启动,我在httpd.conf中找到了默认的网站根目录:
在这里插入图片描述
接着在网站根目录下写程序。但是出现一个问题,我在vscode中修改根目录中的默认程序时,vscode提示没有权限,于是我用管理员命令给目录添加读写权限,然后可以正常的写文件了,

当我想新建文件或文件夹时,又出现了权限不足的问题,又尝试修改权限。但这次无论怎么修改,在vscode中也创建不了文件。

我想到了修改apache的网站根目录来解决这个问题。但是在网上找了好多方法来修改网站更目录,都没有效果。在不断尝试中,我发现*新建一个虚拟主机可以达到我的目的


解决方案:

只需要修改/etc/apache2/httpd.conf文件即可,开启虚拟主机功能,这里需要修改两处。将前面的注释符号删除,即可打开扩展
在这里插入图片描述
在这里插入图片描述
然后修改/private/etc/apache2/extra/httpd-vhosts.conf文件

首先备份该文件防止文件丢失损坏
在这里插入图片描述
通过阅官方文档,我了解虚拟主机如何配置,我将原来自带的虚拟主机配置信息删除,写下了自己的虚拟主机配置,如下图所示

在这里插入图片描述

# Virtual Hosts
#
# Required modules: mod_log_config

# If you want to maintain multiple domains/hostnames on your
# machine you can setup VirtualHost containers for them. Most configurations
# use only name-based virtual hosts so the server doesn't need to worry about
# IP addresses. This is indicated by the asterisks in the directives below.
#
# Please see the documentation at 
# <URL:http://httpd.apache.org/docs/2.4/vhosts/>
# for further details before you try to setup virtual hosts.
#
# You may use the command line option '-S' to verify your virtual host
# configuration.

#
# VirtualHost example:
# Almost any Apache directive may go into a VirtualHost container.
# The first VirtualHost section is used for all requests that do not
# match a ServerName or ServerAlias in any <VirtualHost> block.
#

<VirtualHost 127.0.0.1:80> # 这里写上虚拟主机的ip地址与端口号
    ServerAdmin webmaster@dummy-host.example.com
    # documentroot参数配置网站的根目录
    DocumentRoot "/Users/Frank/workspace"
    # ServerName制定网站的域名
    ServerName localhost
    # 添加日志保存的地址
    ErrorLog "/private/var/log/apache2/dummy-host.example.com-error_log"
    CustomLog "/private/var/log/apache2/dummy-host.example.com-access_log" common
</VirtualHost>

配置完成后重新启动apache,即可生效

sudo /usr/sbin/apachectl restart

在自定义的网站根目录中写下index.html并访问
在这里插入图片描述

这里的网站根目录最好写在/Users/当前用户明/下,否则会出一些问题
例如:/Users/Frank/www

结果如图所示:
在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值