Mac OS X 上用 Homebrew 安装 Nginx + PHP-FPM + MySQL + MsSQL + phpMyAdmin

Mac OS X 是一个非常不错的PHP开发环境,如果还在上面跑虚机做PHP,那么就太可惜了。
以前没用homebrew安装,一直磕磕碰碰,直到遇见它,一切变得如此的简单。对于我这些有洁癖的完美主义者来说,实在是一个福音。
(本文适用于Mac OS X 10.9 Mavericks 和 10.10 Yosemite)

1. Xcode

首先从app store下载,并安装xcode。

xcode-select --install

2. 安装Homebrew

ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
brew doctor
brew update && brew upgrade

3. PHP-FPM

安装freetds、unixODBC

brew install freetds unixodbc

修改freetds.conf(/usr/local/etc/freetds.conf)在[global]添加

tds version = 8.0
port = 1433
client charset = UTF-8

现在开始安装php
Homebrew默认的源不支持php-fpm,因此我们需要额外增加

brew tap homebrew/dupes
brew tap homebrew/php
brew install --with-mssql php71

安装mcrypt

brew install mcrypt
brew install php71-mcrypt

设置 PHP CLI 命令

添加PHP命令$PATH

echo 'export PATH="/usr/local/sbin:$PATH"' >> ~/.bash_profile && . ~/.bash_profile

设置自启动

mkdir -p ~/Library/LaunchAgents
ln -sfv /usr/local/opt/php71/homebrew.mxcl.php71.plist ~/Library/LaunchAgents/
自动启动PHP-FPM
launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.php71.plist

确认PHP-FPM运行,并监听9000端口

lsof -Pni4 | grep LISTEN | grep php

输出如下:

php-fpm   51666 littlesuper    6u  IPv4 0x7ea4180c94b46ad      0t0  TCP 127.0.0.1:9000 (LISTEN)
php-fpm   51667 littlesuper    0u  IPv4 0x7ea4180c94b46ad      0t0  TCP 127.0.0.1:9000 (LISTEN)
php-fpm   51668 littlesuper    0u  IPv4 0x7ea4180c94b46ad      0t0  TCP 127.0.0.1:9000 (LISTEN)

4. MySQL

安装MySQL

brew install mysql

设置自启动

ln -sfv /usr/local/opt/mysql/*.plist ~/Library/LaunchAgents

启动数据库

launchctl load ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist

安全性配置

mysql_secure_installation
> Enter current password for root (enter for none): 直接回车
> Change the root password? [Y/n] Yes
> Remove anonymous users? [Y/n] Yes
> Disallow root login remotely? [Y/n] Yes
> Remove test database and access to it? [Y/n] Yes
> Reload privilege tables now? [Y/n] Yes

测试连接

mysql -uroot -p

输入root的密码,如果登陆成功,则显示如下:

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

5. phpMyadmin

brew install autoconf

设置 $PHP_AUTOCONF:

echo 'PHP_AUTOCONF="'$(which autoconf)'"' >> ~/.bash_profile && . ~/.bash_profile

开始安装

brew install phpmyadmin

6. Nginx

安装nginx

brew install nginx

以root身份启动80端口的nginx

sudo cp -v /usr/local/opt/nginx/*.plist /Library/LaunchDaemons/
sudo chown root:wheel /Library/LaunchDaemons/homebrew.mxcl.nginx.plist

测试Web服务器

启动nginx

sudo launchctl load /Library/LaunchDaemons/homebrew.mxcl.nginx.plist

nginx默认配置只有8080端口,通过命令测试:

curl -IL http://127.0.0.1:8080

正常的话,应该得到以下信息:

HTTP/1.1 200 OK
Server: nginx/1.6.2
Date: Mon, 19 Oct 2014 19:07:47 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Mon, 19 Oct 2014 19:01:32 GMT
Connection: keep-alive
ETag: "5444dea7-264"
Accept-Ranges: bytes

停止nginx服务:

sudo launchctl unload /Library/LaunchDaemons/homebrew.mxcl.nginx.plist

7. 个性化配置

nginx.conf

创建目录

mkdir -p /usr/local/etc/nginx/logs
mkdir -p /usr/local/etc/nginx/vhosts

sudo mkdir -p /www

sudo chown :staff /www
sudo chmod 775 /www

修改nginx.conf(/usr/local/etc/nginx/nginx.conf)

worker_processes  1;
error_log  /usr/local/etc/nginx/logs/error.log debug;

events {
    worker_connections  1024;
}

http {
    include             mime.types;
    default_type        application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /usr/local/etc/nginx/logs/access.log  main;

    sendfile            on;
    keepalive_timeout   65;
    index index.html index.php;

    include /usr/local/etc/nginx/vhosts/*.conf; 
}

修改PHP-FPM配置(/usr/local/etc/nginx/php-fpm)

location ~ \.php$ {
    try_files      $uri = 404;
    fastcgi_pass   127.0.0.1:9000;
    fastcgi_index  index.php;
    fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include        fastcgi_params;
}

创建一个站点样例(/usr/local/etc/nginx/vhosts/default.conf)

server {
    listen       80;
    server_name  localhost;
    root       /www/;

    access_log  /usr/local/etc/nginx/logs/default.access.log  main;

    location / {
        include   /usr/local/etc/nginx/php-fpm;
    }

    location = /info {
        allow   127.0.0.1;
        deny    all;
        rewrite (.*) /.info.php;
    }

    error_page  404     /404.html;
    error_page  403     /403.html;
}

服务控制命令

由于以上的命令太长太麻烦,因此我们通过别名,让控制更轻松。

修改~/.bash_alias文件

# os
alias ls='ls -Gfh'
alias ll='ls -al'

# www
alias nginx.start='sudo launchctl load /Library/LaunchDaemons/homebrew.mxcl.nginx.plist'
alias nginx.stop='sudo launchctl unload /Library/LaunchDaemons/homebrew.mxcl.nginx.plist'
alias nginx.restart='nginx.stop && nginx.start'
alias php-fpm.start="launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.php71.plist"
alias php-fpm.stop="launchctl unload -w ~/Library/LaunchAgents/homebrew.mxcl.php71.plist"
alias php-fpm.restart='php-fpm.stop && php-fpm.start'
alias mysql.start="launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist"
alias mysql.stop="launchctl unload -w ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist"
alias mysql.restart='mysql.stop && mysql.start'
alias nginx.logs.error='tail -250f /usr/local/etc/nginx/logs/error.log'
alias nginx.logs.access='tail -250f /usr/local/etc/nginx/logs/access.log'
alias nginx.logs.default.access='tail -250f /usr/local/etc/nginx/logs/default.access.log'
alias nginx.logs.default-ssl.access='tail -250f /usr/local/etc/nginx/logs/default-ssl.access.log'
alias nginx.logs.phpmyadmin.error='tail -250f /usr/local/etc/nginx/logs/phpmyadmin.error.log'
alias nginx.logs.phpmyadmin.access='tail -250f /usr/local/etc/nginx/logs/phpmyadmin.access.log'

在~/.bash_profile任意位置,增加:

source ~/.bash_aliases

修改以后,执行命令如下:

nginx.start
nginx.stop
nginx.restart
sudo nginx -t

php-fpm.start
php-fpm.stop
php-fpm.restart
php-fpm -t

mysql.start
mysql.stop
mysql.restart

— 至此,我们已配置好Mac OS X的PHP开发环境,全文完。
(改编自:http://blog.frd.mn/install-nginx-php-fpm-mysql-and-phpmyadmin-on-os-x-mavericks-using-homebrew/)