OS X 下配置 Nginx+PHP+MySQL

Homebrew

Mac 下的 Homebrew 相当于 Linux 下的 apt-get、yum,可以获得最新版的各种安装包。

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

安装完成后,运行以下命令检查是否安装成功

brew doctor

然后更新、升级下 brew 源

brew update && brew upgrade

PHP-FPM

添加必须的 tap

brew tap homebrew/dupes
brew tap homebrew/php

安装 php5.6

brew install --without-apache --with-fpm --with-mysql php56

自动启动 php

mkdir -p ~/Library/LaunchAgents
ln -sfv /usr/local/opt/php56/homebrew.mxcl.php56.plist ~/Library/LaunchAgents/

启动 php

launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.php56.plist

MySQL

安装

brew install mysql

自动启动 MySQL

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

启动 MySQL

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

配置密码

mysql_secure_installation

Nginx

安装

brew install nginx

设置自动启动

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

配置

创建 nginx 文件夹及配置文件

mkdir -p /usr/local/etc/nginx/logs
mkdir -p /usr/local/etc/nginx/sites-available
mkdir -p /usr/local/etc/nginx/sites-enabled
mkdir -p /usr/local/etc/nginx/conf.d

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

user Kotori staff;#修改对应用户名,如果你想把Document Root放在家目录下的话,反之注释
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;

    autoindex on;
    autoindex_exact_size off;
    autoindex_localtime on;

    index index.html index.php;

    include /usr/local/etc/nginx/sites-enabled/*;
}

修改 /usr/local/etc/nginx/conf.d/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/sites-available/default

server {
    listen       80;
    server_name  localhost;
    root       /Users/Kotori/Documents/htdocs;#这是自定义目录

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

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

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

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

软链接配置

ln -sfv /usr/local/etc/nginx/sites-available/default /usr/local/etc/nginx/sites-enabled/default

启动

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

一些便利的 alias

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.php56.plist"
alias php-fpm.stop="launchctl unload -w ~/Library/LaunchAgents/homebrew.mxcl.php56.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'

参考 & 精简自 frdmn’s notes