macOS 內建 Apache、PHP、PostgreSQL,但版本較舊(PHP 5.3.x)且缺乏 php-fpm,難以搭配 Nginx。手動編譯雖可掌控版本,但依賴處理繁瑣、升級痛苦。Homebrew 作為 macOS 套件管理神器,能一鍵安裝、升級、切換版本,極適合建立與生產環境一致的本地開發環境。
brew install nginx
若需特定版本,可執行 brew edit nginx 修改 Formula 中的下載 URL。
# 啟動
sudo nginx
# 重新載入/重啟/停止/退出
nginx -s reload|reopen|stop|quit
sudo 權限)/usr/local/Cellar/nginx/<version>/html/usr/local/etc/nginx/nginx.confmkdir -p ~/Library/LaunchAgents
cp /usr/local/Cellar/nginx/1.4.2/homebrew.mxcl.nginx.plist ~/Library/LaunchAgents/
launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.nginx.plist
# 賦予 nginx 執行檔 root 權限以便開機綁定 80 埠
sudo chown root:wheel /usr/local/Cellar/nginx/1.4.2/bin/nginx
sudo chmod u+s /usr/local/Cellar/nginx/1.4.2/bin/nginx
brew install mysql
sudo mysql_install_db \
--verbose --user=`whoami` \
--basedir="$(brew --prefix mysql)" \
--datadir=/usr/local/var/mysql \
--tmpdir=/tmp \
--explicit_defaults_for_timestamp
常見坑點:若初始化失敗,通常是殘留不完整的 /usr/local/var/mysql 目錄導致。解法:刪除該目錄與 /usr/local/opt/mysql/my.cnf 後重新執行初始化。
# 啟動
/usr/local/opt/mysql/bin/mysqld_safe &
# 設定 root 密碼
/usr/local/opt/mysql/bin/mysqladmin -u root password 'new-password'
# 執行安全安裝精靈(移除匿名帳號、禁止 root 遠端登入、刪除 test 資料庫等)
/usr/local/opt/mysql/bin/mysql_secure_installation
mkdir -p ~/Library/LaunchAgents
cp /usr/local/Cellar/mysql/5.6.13/homebrew.mxcl.mysql.plist ~/Library/LaunchAgents/
launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist
brew tap homebrew/dupes
brew tap josegonzalez/homebrew-php
brew install php55 \
--with-debug \
--with-fpm \
--with-gmp \
--with-homebrew-openssl \
--with-imap --with-intl \
--with-libmysql \
--without-bz2 \
--without-mysql \
--without-pcntl \
--without-pear
註:--with-fpm 與 --with-cgi 衝突,二選一。PHP 5.5 內建 Zend OPcache,無需額外安裝 eAccelerator。
將 Homebrew 版 PHP 加入 PATH(編輯 ~/.bashrc 或 ~/.zshrc):
export PATH="$(brew --prefix php55)/bin:$PATH"
source ~/.bashrc
php.ini:/usr/local/etc/php/5.5/php.iniphp-fpm.conf:/usr/local/etc/php/5.5/php-fpm.conf# 前景測試
php-fpm
# 背景守護進程模式:修改 php-fpm.conf 設定 daemonize = yes
# 開機自啟
mkdir -p ~/Library/LaunchAgents
cp /usr/local/Cellar/php55/5.5.3/homebrew-php.josegonzalez.php55.plist ~/Library/LaunchAgents/
launchctl load -w ~/Library/LaunchAgents/homebrew-php.josegonzalez.php55.plist
#!/bin/sh
param=$1
start() {
fpms=`ps aux | grep -i "php-fpm" | grep -v grep | awk '{print $2}'`
if [ -z "$fpms" ]; then php-fpm; echo "PHP-FPM Start"; else echo "PHP-FPM Already Start"; fi
}
stop() {
fpms=`ps aux | grep -i "php-fpm" | grep -v grep | awk '{print $2}'`
echo $fpms | xargs kill -9
for pid in $fpms; do
if echo $pid | egrep -q '^[0-9]+$'; then echo "PHP-FPM Pid $pid Kill"; else echo "$pid IS Not A PHP-FPM Pid"; fi
done
}
case $param in
'start') start;;
'stop') stop;;
'restart') stop; start;;
*) echo "Usage: ./phpfpm.sh start|stop|restart";;
esac
server {
listen 80;
server_name localhost;
index index.html index.htm index.php;
root /Library/WebServer/public_html;
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /Library/WebServer/public_html$fastcgi_script_name;
include /usr/local/etc/nginx/fastcgi_params;
}
}
記得將網站根目錄擁有者改為 www:www(或對應的使用者/群組),並重新載入 Nginx。
在網站根目錄建立 info.php:
<?php phpinfo(); ?>
瀏覽 https://localhost/info.php,若顯示 PHP 5.5.x 與相關擴充模組,即代表環境建置成功。