macOS 10.12 Sierra Apache PHP初步搭建

  • 终端运行命令,启动Apache
$ sudo apachectl start
  • 浏览器访问 http://localhost 确认 Apache 启动成功 
    确认Apache启动成功

  • 终端运行命令,备份 Apache 配置文件

$ cd /etc/apache2/
$ sudo cp httpd.conf httpd.conf.bak
  • 打开Apache配置文件
$ sudo vim httpd.conf
  • 查找 “DocumentRoot” 并修改
DocumentRoot "/Users/wangyuanyuan/Documents/MacDev/Sites"
<Directory "/Users/wangyuanyuan/Documents/MacDev/Sites">
    Options Indexes FollowSymLinks Multiviews
    MultiviewsMatch Any

    AllowOverride All

    Require all granted
</Directory>
  • 修改用户和组
User wangyuanyuan
Group staff
  • 删除该行前的 ‘#’,添加 PHP 支持
LoadModule php5_module libexec/apache2/libphp5.so
  • 删除该行前的 ‘#’,开启 mod_rewrite
LoadModule rewrite_module libexec/apache2/mod_rewrite.so

  • 删除该行前的 ‘#’,开启 ServerName
    ServerName localhost:80
 
  • 添加php文件类型识别
<IfModule dir_module>
    DirectoryIndex index.html index.php
</IfModule>
  • 创建测试文件
$ cd Documents/MacDev/Sites/
$ cp /Library/WebServer/Documents/* ./
  • 修改测试文件
$ rm index.*
$ touch index.php
$ echo "<h1>My User Web Root</h1> <hr> <?php phpinfo(); ?>" > index.php

 

  • 终端运行命令,重启 Apache 服务
$ sudo apachectl -k restart

 

这里写图片描述

参考: 
macOS 10.12 Sierra Apache Setup: Multiple PHP Versions 
https://getgrav.org/blog/macos-sierra-apache-multiple-php-versions