分享一下 Mac 中使用 Homebrew 设置方便的 PHP 开发环境

设置Mac开发环境

原文:Setup Mac Development Envrionment

本文说明了如何在Mac上优雅的设置一套PHP开发环境,如果你有什么建议的话可以 email 我或者在 twitter联系我也可以(微博的话同ID).

接下来是主要内容

Greate IDE and Apps

Mac上有很多牛逼的编辑器和应用

Xcode or Command Line Tools

如果你是一个iOS/Mac应用开发,你可以从Mac App Store中安装Xcode,如果不是直接安装Command Line Tools就可以了

xcode-select --install

如果你已经安装了Xcode

xcode-select -s /Applications/Xcode.app/Contents/Developer

System Integrity Protection

自从 OS X 10.11, 苹果公司引入了一套安全机制防止系统文件被修改,这个玩意叫SIP,也叫rootless

不幸的是,这个玩意会把/usr/local/etc目录都会锁上,这会妨碍安装Homebrew和配置DNS的一些小技巧。所以得先关上这个bug(feature),设置完了之后再打开。

关闭步骤:

  • 关机并按住 CMD+R 启动.
  • 进入恢复模式之后打开 Utility -> Terminal
  • 执行 csrutil disable
  • 重启

启用步骤:

  • 进入恢复模式,终端中执行 csrutil enable
  • 重启

可以用 csrutil status 来检查当前SIP的状态

Homebrew

Homebrew是一套包管理系统,有点类似FreeBSD的ports-tree,可以方便的安装开源的App

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

安装完Homebrew之后强烈建议安装最新的git来替代苹果在Command Line Tools中自带的。

brew install git

Nginx, PHP, and MySQL

Nginx

相比于Apache,我更喜欢用Nginx。

brew install nginx

PHP

Homebrew 预编译的PHP使用的libcurl是macOS系统自带的,但是这个版本本身是有问题的,所以最好是使用Homebrew中的curl

brew install curl --with-openssl

PHP7.0已经发布了,不过一些项目都还在PHP5.x上,所以就安装PHP5.6就好了

安装 php56之前, 先来tap一下PHP的仓库

brew tap homebrew/php

然后安装配备Homebrew版本libxml2和curl的php56.

brew install php56  --with-homebrew-libxml2  --with-homebrew-curl  --with-pear

如果你有需要安装的PHP模块,搜索并安装就好了,不过要注意版本 brew search php-*

MySQL/PostgreSQL

安装MySQL或者PostgreSQL

brew install mysql postgresql

Homebrew Services

Homebrew Services是一套可以通过 launchctl来管理安装的服务的套件,非常好用推荐服用。

brew tap homebrew/services

然后就可以:

  • 启动MySQL brew services start mysql
  • 启用PHP-FPM brew services restart php56
  • 启用Nginx brew services stop Nginx

Configure Nginx

只有root用户可以监听1000以下的端口, Homebrew 安装 Nginx 都是使用当前用户,brew services也是以普通用户来启动服务,HTTP服务又是在80端口,我们也不想访问的时候加上一个8080,也不像brew services的时候sudo一下,所以接下来的配置呢,会让Nginx运行在8080端口,然后通过系统的pf把80端口映射到8080端口。

增加一个Nginx配置文件

  • Nginx: /usr/local/etc/nginx/servers/dev.conf

别忘了替换 USERNAME

server {
    listen       8080;
    server_name ~^(.+)\.dev$;
    set $file_path $1;
    root    /Users/USERNAME/workspace/www/$file_path/public;
    location / {
        index index.html index.php;
        try_files $uri $uri/ /index.php?$query_string;
    }
    charset utf-8;
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   html;
    }
    location ~ \.php$ {
        root    /Users/USERNAME/workspace/www/$file_path/public;
        fastcgi_pass   127.0.0.1:9000; # PHP-FPM default running on this port.
        fastcgi_index  index.php;
        include        fastcgi_params;
        fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }
}

这个配置会将 foobar.devDocumentRoot 指向 /Users/USERNAME/workspace/www/foobar/public 并且把路由请求交给 index.php, 大部分的PHP框架都是这么配置的.

这样配置之后只要重启Nginx就好了

brew services restart nginx

DNS 和端口转发

在开发环境中更倾向于使用一个dev结尾的域名用于开发方便,比如 foo.dev -> ~/workspace/www/foo 或者 bar.dev -> ~/workspace/www/bar. 这样我们就可以直接在浏览器中访问了相对应的项目了。

dev 域名和 DNS 解析

dnsmasq 是一个非常强悍的DNS转发器。

brew install dnsmasq

一样的可以使用Homebrew Services管理服务 brew services start dnsmasq

编辑 /usr/local/etc/dnsmasq.conf 加入下面这些配置.

address=/.dev/127.0.0.1
listen-address=127.0.0.1
port=35353

创建一个 sudo mkdir /etc/resolver 并且放一个 dev 文件进去,内容如下

nameserver 127.0.0.1
port 35353

重启dnsmasqbrew services restart dnsmasq

ping一下可以检查一下是不是配置好了,ping foobar.dev

Configure 端口转发

自从 Yosemite(10.10)开始, ipfwpf 取代. 下面就是如何用pf配置80端口转发8080

  • 创建一个pf规则文件 /etc/pf.anchors/dev.cutedge
rdr pass on lo0 inet proto tcp from any to self port 80 -> 127.0.0.1 port 8080
rdr pass on lo0 inet proto tcp from any to self port 443 -> 127.0.0.1 port 8443

注意: 最后一行必须为空

测试这个规则文件:

sudo pfctl -vnf /etc/pf.anchors/dev.cutedge

检查pf的状态:

sudo pfctl -s nat 

修改 /etc/pf.conf

  • 找到rdr-anchor "com.apple/*",在这一行后插入 rdr-anchor "devport"
  • 找到load anchor "com.apple" from "/etc/pf.anchors/com.apple",在这一行后插入load anchor "devport" from "/etc/pf.anchors/dev.cutedge"

保持最后一行空白

检查一下配置文件pf.conf 是否正确 sudo pfctl -ef /etc/pf.conf

  • 自动启动 pf
sudo defaults write /System/Library/LaunchDaemons/com.apple.pfctl ProgramArguments '(pfctl, -f, /etc/pf.conf, -e)'

执行这一条需要SIP处于关闭状态

Final

OK,现在PHP开发环境就配置好了,在新建一个$HOME/workspace/www/test/public/index.php文件

<?php
phpinfo();

然后访问http://test.dev/,就可以看到了

最后,不要忘了重新打开SIP。

《L05 电商实战》
从零开发一个电商项目,功能包括电商后台、商品 & SKU 管理、购物车、订单管理、支付宝支付、微信支付、订单退款流程、优惠券等
《L02 从零构建论坛系统》
以构建论坛项目 LaraBBS 为线索,展开对 Laravel 框架的全面学习。应用程序架构思路贴近 Laravel 框架的设计哲学。
讨论数量: 6
wenber

还是觉得valet方便一点

7年前 评论

@ziyanziyu 各有不同吧,那个方便哪个来

7年前 评论

@khsing mysql的配置文件怎么找也找不到啊

7年前 评论

@sj1370201 直接cp /usr/local/opt/mysql/support-files/my-default.cnf /usr/local/etc/my.cnf 修改就好了

7年前 评论

谢谢分享。自动启动pf那句脚本,执行后,/System/Library/LaunchDaemons/com.apple.pfctl.plist 的内容就变成乱码了。直接修改文件会不会更好些?

5年前 评论

mac High Sierra : brew services start dnsmasq 好像不行,必须加sudo

5年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!