前言

为避免每次 macOS 更新系统时软件意外升级,以及新的 apache、php 配置文件覆盖掉原有的自定义配置,给本地开发造成额外困扰,故使用 homebrew 去管理和控制 apache、php

安装 Homebrew

1
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

关闭系统自带 apache

1
2
sudo apachectl stop
sudo launchctl unload -w /System/Library/LaunchDaemons/org.apache.httpd.plist

使用 Homebrew 安装 apache、php

1
2
brew install httpd
brew install php

查看 apache、php 相关安装信息

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
brew info httpd
############################################################
httpd: stable 2.4.33 (bottled)
Apache HTTP server
https://httpd.apache.org/
/usr/local/Cellar/httpd/2.4.33 (1,633 files, 26.4MB) *
Poured from bottle on 2018-07-13 at 12:43:24
From: https://github.com/Homebrew/homebrew-core/blob/master/Formula/httpd.rb
==> Dependencies
Required: apr ✔, apr-util ✔, brotli ✔, nghttp2 ✔, openssl ✔, pcre ✔
==> Caveats
DocumentRoot is /usr/local/var/www.

The default ports have been set in /usr/local/etc/httpd/httpd.conf to 8080 and in
/usr/local/etc/httpd/extra/httpd-ssl.conf to 8443 so that httpd can run without sudo.

To have launchd start httpd now and restart at login:
brew services start httpd
Or, if you don't want/need a background service you can just run:
apachectl start
############################################################
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
brew info php

## 查看 php 相关信息,如
############################################################
php: stable 7.2.7 (bottled)
General-purpose scripting language
https://secure.php.net/
/usr/local/Cellar/php/7.2.7 (515 files, 79.1MB) *
Poured from bottle on 2018-07-12 at 22:06:39
From: https://github.com/Homebrew/homebrew-core/blob/master/Formula/php.rb
==> Dependencies
Build: httpd ✔, pkg-config ✔
Required: apr ✔, apr-util ✔, argon2 ✔, aspell ✔, autoconf ✔, freetds ✔, freetype ✔, gettext ✔, glib ✔, gmp ✔, icu4c ✔, jpeg ✔, libpng ✔, libpq ✔, libsodium ✔, libzip ✔, openssl ✔, pcre ✔, unixodbc ✔, webp ✔
==> Caveats
To enable PHP in Apache add the following to httpd.conf and restart Apache:
LoadModule php7_module /usr/local/opt/php/lib/httpd/modules/libphp7.so

<FilesMatch \.php$>
SetHandler application/x-httpd-php
</FilesMatch>

Finally, check DirectoryIndex includes index.php
DirectoryIndex index.php index.html

The php.ini and php-fpm.ini file can be found in:
/usr/local/etc/php/7.2/

To have launchd start php now and restart at login:
brew services start php
Or, if you don't want/need a background service you can just run:
php-fpm
############################################################
1
2
/usr/local/etc/httpd/httpd.conf ## apache 配置文件
/usr/local/etc/php/7.2/php.ini ## php 配置文件

编辑 httpd 配置

1
vi /usr/local/etc/httpd/httpd.conf
1
2
3
4
5
6
7
## ======= 添加 php 模块 ======= ##
LoadModule php7_module /usr/local/opt/php/lib/httpd/modules/libphp7.so

<FilesMatch \.php$>
SetHandler application/x-httpd-php
</FilesMatch>
## ========================== ##
1
2
3
4
5
## ======= 修改 DirectoryIndex ======= ##
<IfModule dir_module>
DirectoryIndex index.php index.html
</IfModule>
## ========================== ##

(配置 vhosts)

本地开发环境不必考虑性能问题,以方便运行为上,故可使用 vhosts 以便配置;生产环境慎用 :-)

1
vi /usr/local/etc/httpd/httpd.conf
1
2
3
## ======= 启用 rewrite 模块======= ##
LoadModule rewrite_module lib/httpd/modules/mod_rewrite.so
## ========================== ##
1
2
3
4
5
6
## ======= 允许重写 ======= ##
<Directory />
AllowOverride All
Require all granted
</Directory>
## ========================== ##
1
2
3
4
5
6
7
8
9
10
## ======= httpd.conf ======= ##
## 在 httpd.conf 文件的最后加上下列代码
## 将 YOUR_VHOST_PATH 替换为实际路径
RewriteEngine on
RewriteMap lowercase int:tolower
RewriteMap vhost txt:YOUR_VHOST_PATH
RewriteCond ${lowercase:%{SERVER_NAME}} ^(.+)$
RewriteCond ${vhost:%1} ^(/.*)$
RewriteRule ^/(.*)$ %1/$1
## ========================== ##

编辑 hosts 文件

1
sudo vi /etc/hosts
1
2
3
4
## ======= hosts ======= ##
## 域名可以随便填写,强制重定向到 127.0.0.1
127.0.0.1 xxx.example.com
## ========================== ##

vhost 文件示例

1
2
3
## ======= YOUR_VHOST_PATH ======= ##
xxx.example.com /var/www/example
## ========================== ##

重启 apache

1
2
apachectl configtest ## 测试配置文件合法性
apachectl restart

此后,如有新工程调试,只需在 hosts 中添加域名,并编辑 vhost 文件,无需重启 apache,即可访问相应域名进行调试。

开机启动

1
2
brew services start httpd
brew services start php

Tips

macOS 下个人目录的 LibraryDocuments等文件夹默认权限为 700,apache 用户无法访问,所以需要保证 vhost 指向的路径具有访问权限,将文件夹权限改为 755 即可。