Mac 通过 brew 安装 Apache2.4

执行安装

brew install homebrew/apache/httpd24

删除原有自带的 apache

sudo rm -rf /usr/sbin/httpd
sudo rm -rf /etc/apache2

修改配置 /usr/local/etc/apache2/2.4/httpd.conf

# 修改监听端口(初始为8080)
Listen 80

找 `Require all denied` 替换成 `Require all granted`

# 开启 mod_rewrite 模块
LoadModule rewrite_module libexec/apache2/mod_rewrite.so

# 开启 PHP7 模块
LoadModule php7_module /usr/local/Cellar/php70/7.0.6/libexec/apache2/libphp7.so

# 开启引入虚拟主机
Include /usr/local/etc/apache2/2.4/extra/httpd-vhosts.conf

# 底部增加一行
Include /usr/local/etc/apache2/2.4/other/*.conf

修改文件 /usr/local/etc/apache2/2.4/extra/httpd-vhosts.conf

<VirtualHost *:80>
    ServerName localhost
    DocumentRoot "/Users/silverd/home/wwwroot"
    <Directory "/Users/silverd/home/wwwroot">
        Include /usr/local/etc/apache2/2.4/extra/php-yaf.conf
    </Directory>
</VirtualHost>

<VirtualHost *:80>
    ServerName local.api.hicrew.cn
    ServerAlias local.m.hicrew.cn
    DocumentRoot "/Users/silverd/home/wwwroot/hicrew/app/web"
    <Directory "/Users/silverd/home/wwwroot/hicrew/app/web">
        Include /usr/local/etc/apache2/2.4/extra/php-yaf.conf
    </Directory>
</VirtualHost>

<VirtualHost *:80>
    ServerName local.admincp.hicrew.cn
    DocumentRoot "/Users/silverd/home/wwwroot/hicrew/admin/web"
    <Directory "/Users/silverd/home/wwwroot/hicrew/admin/web">
        Include /usr/local/etc/apache2/2.4/extra/php-yaf.conf
    </Directory>
</VirtualHost>

新增文件 /usr/local/etc/apache2/2.4/other/php7.conf

<IfModule php7_module>
    AddType application/x-httpd-php .php
    AddType application/x-httpd-php-source .phps
    <IfModule dir_module>
        DirectoryIndex index.html index.php
    </IfModule>
</IfModule>

新增文件 /usr/local/etc/apache2/2.4/extra/php-yaf.conf

Options Indexes FollowSymLinks
Require all granted
AllowOverride All
<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ /index.php/$1 [L]
</IfModule>

设置开机启动

ln -sfv /usr/local/opt/httpd24/*.plist ~/Library/LaunchAgents
launchctl load ~/Library/LaunchAgents/homebrew.mxcl.httpd24.plist

设置启动、停止命令别名

vi ~/.bash_profile
alias httpd24.start="launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.httpd24.plist"
alias httpd24.stop="launchctl unload -w ~/Library/LaunchAgents/homebrew.mxcl.httpd24.plist"
alias httpd24.restart='httpd24.stop && httpd24.start'
source ~/.bash_profile

其实仍然可以通过以下命令来管理

sudo apachectl start
sudo apachectl stop
sudo apachectl restart

因为 apachectl 命令此时指向的是:

/usr/local/bin/apachectl -> /usr/local/Cellar/httpd24/2.4.23_2/bin/apachectl

对了,记得把原先系统自带的 Apache 停止掉:

sudo /usr/sbin/apachectl stop

# 或粗鲁点直接杀进程
ps -ef | grep /usr/sbin/httpd | grep -v grep | cut -c 6-12 | xargs sudo kill -9