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
结果如图所示: