本文记录在 macOS 设备上配置 Apache 服务过程,包括 Apache 服务的操作命令、主配置、多虚拟主机配置、站点目录权限设置、php 扩展功能的开启。以及一些应该注意的问题。
macOS 10.13 中已自带了 Apache 环境,我们只需要启动她。
使用“终端(terminal)”命令(command + 空格 输入 “terminal”,可以快速找到打开)
sudo apachectl -v // 查看 Apache 版本信息(可能需要输入密码,输入回车即可)
下面是我的机器上的 Apache 版本:
启动 Apache 服务
sudo apachectl start //启动 Apache 服务
在浏览器中输入“http://localhost”,如果 Apache 正常启动,可以看到一个内容为“It works!”的页面:
还可以使用下面命令来停止、重启 Apache 服务:
sudo apachectl stop // 停止 Apache 服务
sudo apachectl restart // 重启 Apache 服务
查看默认的页面文档目录:
index.html.en 页面位于“/Library/WebServer/Documents/”下,这是 Apache 的默认页面。
(可以使用 Finder 的 “前往文件夹” 方便定位到文件夹)
注:资源库是 Library 目录的别名。
创建自己的主机目录
1.在“/Library/WebServer/”下创建一个名为”Sites”的文件夹
注:在 WebServer 目录中创建是为了避免权限问题,在这里创建系统会提示需要输入一次管理密码,Sites 目录的权限就有了。
如果在系统以外的目录中新建目录(比如在桌面中),可能需要另外添加目录权限。
2. 在文件夹中创建一个名为”index.html”的文件,完整内容:
<!DOCTYPE html>
<html lang="zh-cn">
<head>
<meta charset="UTF-8">
<title>Test 页面</title>
</head>
<body>
<h1>这个是 Test 页面</h1>
</body>
</html>
让浏览器 http://localhost 能显示这个 index.html 页面
需要修改主配置文件 /etc/apache2/httpd.conf (这里使用 Sublime Text 编辑器,拖拽 httpd.conf 到编辑器中)
注:修改前最好先备份一份。
默认为:
DocumentRoot "/Library/WebServer/Documents"
<Directory "/Library/WebServer/Documents">
….
</Directory>
修改为:
DocumentRoot "/Library/WebServer/Sites"
<Directory "/Library/WebServer/Sites">
….
</Directory>
</