Mac10.13.6下使用homebrew安装PHP+Apache开发环境

提前安装好homebrew

详见

删除Mac自带的Apache

停掉Apache服务

sudo apachectl stop

删除Mac自带的Apache

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

安装Apache

使用homebrew下载Apache

brew install apache2

保留下载后的使用提示

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

修改Apache配置文件

打开配置文件

sudo vim /usr/local/etc/httpd/httpd.conf

修改以下几项

修改端口

Listen 8080

修改服务名称

ServerName localhost:8080

修改访问权限

<Directory />
    AllowOverride None
    Require all denied
</Directory>

改为

<Directory />
    AllowOverride All
    Require all granted
</Directory>

修改网站访问目录

DocumentRoot "/var/www"
<Directory "/var/www">

启动Apache sudo apachectl start,打开浏览器输入localhost:8080将会看到 “It works!”,证明启动成功。

安装PHP5.6

下载php5.6

brew install php@5.6

保留下载后的使用提示

To enable PHP in Apache add the following to httpd.conf and restart Apache:
   LoadModule php5_module /usr/local/opt/php@5.6/lib/httpd/modules/libphp5.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/5.6/

php@5.6 is keg-only, which means it was not symlinked into /usr/local,
because this is an alternate version of another formula.

If you need to have php@5.6 first in your PATH run:
  echo 'export PATH="/usr/local/opt/php@5.6/bin:$PATH"' >> ~/.zshrc
  echo 'export PATH="/usr/local/opt/php@5.6/sbin:$PATH"' >> ~/.zshrc

For compilers to find php@5.6 you may need to set:
  export LDFLAGS="-L/usr/local/opt/php@5.6/lib"
  export CPPFLAGS="-I/usr/local/opt/php@5.6/include"


To have launchd start php@5.6 now and restart at login:
  brew services start php@5.6
Or, if you don't want/need a background service you can just run:
  php-fpm

如上所述,修改httpd.confzshrc,加入环境变量,并开启php5.6和php-fpm
重启Apache,创建info.php,看是否成功

配置Apache多站点模式

打开配置文件

sudo vim /usr/local/etc/httpd/httpd.conf

#Include /usr/local/etc/httpd/extra/httpd-vhosts.conf前面注释去掉

# Virtual hosts 
Include /usr/local/etc/httpd/extra/httpd-vhosts.conf

修改httpd-vhosts.conf

<VirtualHost *:8080>
    ServerAdmin localhost
    DocumentRoot "/var/www/"
    ServerName localhost
    ServerAlias localhost
    ErrorLog "/usr/local/var/log/httpd/dummy-host.example.com-error_log"
    CustomLog "/usr/local/var/log/httpd/dummy-host.example.com-access_log" common
</VirtualHost>

<VirtualHost *:8080>
    ServerAdmin beibei-test.com
    DocumentRoot "/var/www/idvert/beibei"
    ServerName beibei-test.com
</VirtualHost>

修改hosts,在其中加入127.0.0.1 beibei-test.com

sudo vim /etc/hosts

打开浏览器,输入beibei-test.com:8080即可看到配置好的网站