Mac 搭建开发环境(一)LAMP

安装 homebrew

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

Command Line Tools

方法1. brew doctor 后根据提示自动安装
方法2. xcode-select --install
方法3. 在 AppStore 里安装最新版 Xcode

准备工作

brew install wget
brew install libevent
brew link libevent
brew install autoconf
brew install pkg-config
brew install libmemcached

# 如果出现连接不上,可尝试修改DNS解析:
sudo vi /etc/resolev.conf
nameserver 114.114.114.114
nameserver 8.8.8.8

安装基础服务

brew install homebrew/apache/httpd24
brew install homebrew/php/php71 --with-apache
brew install memcached
brew install redis
brew install mongodb

安装 PHP7 PECL 扩展(–HEAD 表示最新但非稳定版)

brew install homebrew/php/php71-memcached
brew install homebrew/php/php71-redis
brew install homebrew/php/php71-mongodb
brew install homebrew/php/php71-xxtea
brew install homebrew/php/php71-yaf
brew install homebrew/php/php71-swoole
brew install homebrew/php/php71-mcrypt

安装 PHP Composer 包管理器

brew install homebrew/php/composer
composer self-update

# 启用 Packagist 中国镜像
composer config -g repo.packagist composer https://packagist.phpcomposer.com

可选安装 PHP7-PECL-YAF 扩展

homwbrew 里已可以安装 php71-yaf,但版本可能不是最新。可以跳过本步。

wget http://pecl.php.net/get/yaf-3.0.4.tgz
tar zxvf yaf-3.0.4.tgz
cd yaf-3.0.4

phpize
./configure
make && make install
echo 'extension=yaf.so' > '/usr/local/etc/php/7.1/conf.d/ext-yaf.ini'
cd ..
rm -rf yaf-3.0.4 yaf-3.0.4.tgz package.xml

查看 php -v 可能会遇到 memcached 依赖问题

PHP Deprecated: PHP Startup: memcached.sess_lock_wait and memcached.sess_lock_max_wait are deprecated. Please update ...

按照所述修改 `/usr/local/etc/php/7.1/conf.d/ext-memcached.ini`

移除以下两句
memcached.sess_lock_wait = 150000
memcached.sess_lock_max_wait = 0

更换成新版的写法
memcached.sess_lock_wait_min = 0;
memcached.sess_lock_wait_max = 0;
memcached.sess_lock_retries = 0;

开启 PHP 解析短标签

修改 `/usr/local/etc/php/7.1/php.ini` 设置 short_open_tag = On

启动、重启、停止 Apache

sudo apachectl start
sudo apachectl stop
sudo apachectl restart

通过 `httpd -V` 也可以查看当前 Apache 的版本以及 httpd.conf 所在路径 (SERVER_CONFIG_FILE)
/usr/local/etc/apache2/2.4/httpd.conf

Apache 修改 /usr/local/etc/apache2/2.4/httpd.conf

# 如果访问 localhost 出现 403 Forbidden?
找 `Require all denied` 替换成 `Require all granted`

# 开启 mod_rewrite 模块
取消注释 `LoadModule rewrite_module libexec/apache2/mod_rewrite.so`

# 如果是 PHP7,还需修改 Mac 自带 apache 的 PHP 模块,
搜索 `LoadModule php5_module libexec/apache2/libphp5.so`
替换 `LoadModule php7_module /usr/local/Cellar/php71/7.1.*/libexec/apache2/libphp7.so`

# 开启引入虚拟主机
取消注释 `Include /usr/local/etc/apache2/2.4/extra/httpd-vhosts.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>

如何新建一个 Apache vhost?

sudo vi /usr/local/etc/apache2/2.4/extra/httpd-vhosts.conf

<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">
        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>
    </Directory>
</VirtualHost>

特别注意:

DocumentRoot 必须是绝对路径。不能简写为 ~/home/wwwroot/…,否则会报 404 Not Found

因为 DocumentRoot 是相对 ServerRoot 而言的,httpd.conf 中 ServerRoot 是 /Users

MySQL 安装和管理

# 第1步:安装
brew install mysql

# 第2步:启动(会以当前登录身份启动)
mysql.server start

# 第3步:设置密码(然后按提示操作)
mysql_secure_installation

# 第4步:停止
mysql.server stop

# 将 MySQL Server 设为开机启动(以 root 身份启动)
sudo ln -sfv /usr/local/opt/mysql/*.plist /Library/LaunchDaemons
sudo chown root:wheel /Library/LaunchDaemons/homebrew.mxcl.mysql.plist
sudo launchctl load -w /Library/LaunchDaemons/homebrew.mxcl.mysql.plist

# 停止 MySQL Server
sudo launchctl unload -w /Library/LaunchDaemons/homebrew.mxcl.mysql.plist

附录:用 launchctl 来管理服务

官方不推荐用 brew services 方式管理服务(以后可能被淘汰),那么可以用以下方法:

# 将 Redis Server 设为开机启动
sudo ln -sfv /usr/local/opt/redis/*.plist /Library/LaunchDaemons
sudo chown root:wheel /Library/LaunchDaemons/homebrew.mxcl.redis.plist
sudo launchctl load -w /Library/LaunchDaemons/homebrew.mxcl.redis.plist

# 停止 Redis Server
sudo launchctl unload -w /Library/LaunchDaemons/homebrew.mxcl.redis.plist

# 将 Memcache Server 设为开机启动
sudo ln -sfv /usr/local/opt/memcached/*.plist /Library/LaunchDaemons
sudo chown root:wheel /Library/LaunchDaemons/homebrew.mxcl.memcached.plist
sudo launchctl load -w /Library/LaunchDaemons/homebrew.mxcl.memcached.plist

# 停止 Memcache Server
sudo launchctl unload -w /Library/LaunchDaemons/homebrew.mxcl.memcached.plist

特别注意:当 Memcached 以 root 身份启动时,必须带上 -u 参数,需修改 plist

sudo sed -i '' \
    -e 's#<string>-l</string>#<string>-u</string>#g' \
    -e 's#<string>localhost</string>#<string>root</string>#g' \
    /Library/LaunchDaemons/homebrew.mxcl.memcached.plist

修改后的 plist 全文如下:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Label</key>
    <string>homebrew.mxcl.memcached</string>
    <key>KeepAlive</key>
    <true/>
    <key>ProgramArguments</key>
    <array>
        <string>/usr/local/opt/memcached/bin/memcached</string>
        <string>-u</string>
        <string>root</string>
    </array>
    <key>RunAtLoad</key>
    <true/>
    <key>Debug</key>
    <true/>
    <key>WorkingDirectory</key>
    <string>/usr/local</string>
    <key>StandardErrorPath</key>
    <string>/usr/local/var/log/memcached.log</string>
    <key>StandardOutPath</key>
    <string>/usr/local/var/log/memcached.log</string>
</dict>
</plist>

或者直接设置命令别名 vi ~/.zshrc,加入:

alias mysql.start="sudo launchctl load -w /Library/LaunchDaemons/homebrew.mxcl.mysql.plist"
alias mysql.stop="sudo launchctl unload -w /Library/LaunchDaemons/homebrew.mxcl.mysql.plist"
alias mysql.restart='mysql.stop && mysql.start'

alias redis.start="sudo launchctl load -w /Library/LaunchDaemons/homebrew.mxcl.redis.plist"
alias redis.stop="sudo launchctl unload -w /Library/LaunchDaemons/homebrew.mxcl.redis.plist"
alias redis.restart='redis.stop && redis.start'

alias memcached.start="sudo launchctl load -w /Library/LaunchDaemons/homebrew.mxcl.memcached.plist"
alias memcached.stop="sudo launchctl unload -w /Library/LaunchDaemons/homebrew.mxcl.memcached.plist"
alias memcached.restart='memcached.stop && memcached.start'

source ~/.zshrc

附录:homebrew 其他技巧

# 诊断 brew
brew doctor

# 查看配置和版本
brew config

# 更新 brew 源
brew update

# 升级软件
brew upgrade

# 卸载软件
brew uninstall wget

# 使用 brew 第三方安装源
brew tap phinze/homebrew-cask
brew install brew-cask

# 然后像 brew 一样来安装软件
brew cask install google-chrome

# 查看 homebrew cask 已支持软件列表
https://github.com/phinze/homebrew-cask/tree/master/Casks

# 卸载软件
brew cask uninstall google-chrome

2016.10.25 补充说明

自 MacOS Sierra 系统发布后,homebrew 里的 php71 安装时默认不会编译 Apache 的 libphp7.so 模块,务必加上 --with-apache 参数

brew install homebrew/php/php71 --with-apache

如果通过 homebrew 升级了 Apache2.4(Mac虽然自带 Apache,但有些库版本较旧,可通过 /usr/sbin/httpd -v 可查看自带的 Apache 版本)

那么请移步:Mac 通过 brew 安装 Apache2.4

2016.11.01 一键安装脚本

本司的全栈小王子陈问鱼同学把以上各步骤写成了一个自动脚本,无人值守、省时省力,以后再也不用一步步复制粘贴啦~

可以通过直接在命令行运行 cURL:

curl -o- https://raw.githubusercontent.com/silverd/silverd.github.io/master/res/attach/install_mac_dev_env.sh | sh

或者 Wget:

wget -qO- https://raw.githubusercontent.com/silverd/silverd.github.io/master/res/attach/install_mac_dev_env.sh | sh