参照博文作者: http://blog.liuweifeng.net/author/liuweifeng/
首先,为了不出现各种神奇的错误,请安装 XCode。Yosemite 发布后,XCode 的最新版本是 6.1.0(6A1052d)。然后,不可忽略的一步,请在终端里执行
- xcode - select--install
来安装最新的 XCode 命令行工具(Xcode Command Line Tools)。
如果没有安装,请移步官网 →_→ Homebrew 官方网站
如果你已经安装了 Homebrew,请在终端执行 brew doctor 检查各种可能的冲突和问题。然后执行
升级 Homebrew 自身和自带的 formulas
- brew update && brew upgrade
Homebrew 的 formulas 里默认木有 PHP-FPM,我们需要手动添加:
- brew tap homebrew / php
然后开始安装:
- brew search php //choose php version
- brew install--without - apache--with - fpm--with - mysql php71
更改为新安装的 php, 打开~/.bash_profile
- export PATH="/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin" //可以采取下面的方式
因为我们打算安装 Nginx,Apache 就不需要了(貌似 OS X 自带了 Apache?)。
如果你想在命令行里使用 PHP 的话,你需要在你 shell 的 Profile 里设置下 $PATH:
- # If you use Bash
- echo 'export PATH="$(brew --prefix homebrew/php/php71)/bin:$PATH"' >> ~/.bash_profile && . ~/.bash_profile
- # If you use ZSH
- echo 'export PATH="$(brew --prefix homebrew/php/php71)/bin:$PATH"' >> ~/.zshrc && . ~/.zshrc
设置开机启动:
- mkdir -p ~/Library/LaunchAgents
- cp /usr/local/Cellar/php71/7.1.5_17/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 78168 liuweifeng 7u IPv4 0x9c66922b37bcbcb7 0t0 TCP 127.0.0.1:9000 (LISTEN)
- php-fpm 78170 liuweifeng 0u IPv4 0x9c66922b37bcbcb7 0t0 TCP 127.0.0.1:9000 (LISTEN)
- php-fpm 92277 liuweifeng 0u IPv4 0x9c66922b37bcbcb7 0t0 TCP 127.0.0.1:9000 (LISTEN)
- php-fpm 94154 liuweifeng 0u IPv4 0x9c66922b37bcbcb7 0t0 TCP 127.0.0.1:9000 (LISTEN)
- brew install mysql
设置开机启动:
- ln -sfv /usr/local/opt/mysql/*.plist ~/Library/LaunchAgents
- mysql_secure_installation
可以设置 root 的密码、移除匿名用户、禁止 root 远程登录,等等。
首先需要安装
- autoconf
- brew install autoconf
设置
:
- $PHP_AUTOCONF
- # If you use Bash
- echo 'PHP_AUTOCONF="'$(which autoconf)'"' >> ~/.bash_profile && . ~/.bash_profile
- # If you use ZSH
- echo 'PHP_AUTOCONF="'$(which autoconf)'"' >> ~/.zshrc && . ~/.zshrc
开始安装 phpMyAdmin :
- brew install phpmyadmin
- brew install nginx
由于我们需要启动 80 端口,所以需要使用 sudo 来运行 nginx
- sudo cp -v /usr/local/opt/nginx/*.plist /Library/LaunchDaemons/
- sudo chown root:wheel /Library/LaunchDaemons/homebrew.mxcl.nginx.plist
- sudo launchctl load / Library / LaunchDaemons / homebrew.mxcl.nginx.plist
- sudo launchctl unload / Library / LaunchDaemons / homebrew.mxcl.nginx.plist
创建配置文件中需要用到的目录
- 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
- mkdir -p /usr/local/etc/nginx/ssl
- sudo mkdir -p /var/www
- sudo chown :staff /var/www
- sudo chmod 775 /var/www
移除默认的
(其实在
- nginx.conf
还有个备份,你可以看一眼,如果你想看的话。。。), 然后使用
- /usr/local/etc/nginx/nginx.conf.default
从 Github 上下载我的自定义版本:
- curl
- rm / usr / local / etc / nginx / nginx.conf curl - L https: //gist.github.com/frdmn/7853158/raw/nginx.conf -o /usr/local/etc/nginx/nginx.conf
这是一个尽可能简单和轻量级的配置文件。
从 Github 下载我的自定义配置文件:
- curl -L https://gist.github.com/frdmn/7853158/raw/php-fpm -o /usr/local/etc/nginx/conf.d/php-fpm
- curl -L https://gist.github.com/frdmn/7853158/raw/sites-available_default -o /usr/local/etc/nginx/sites-available/default
- curl -L https://gist.github.com/frdmn/7853158/raw/sites-available_default-ssl -o /usr/local/etc/nginx/sites-available/default-ssl
- curl -L https://gist.github.com/frdmn/7853158/raw/sites-available_phpmyadmin -o /usr/local/etc/nginx/sites-available/phpmyadmin
使用
从 Github 上克隆我的实例虚拟主机配置(包括默认的 404、403 页面,和一个
- Git
rewrite)。
- phpinfo()
- git clone http://git.frd.mn/frdmn/nginx-virtual-host.git /var/www
- rm -rf /var/www/.git
克隆完记得把 .git 目录删除掉,这样就不会被 Git 记录了。
译者注:其实吧,我赶脚这个 404 和 403 挺丑的。。。
创建需要的目录
- mkdir - p / usr / local / etc / nginx / ssl
创建 4096bit RSA keys 和自签名证书:
- openssl req -new -newkey rsa:4096 -days 365 -nodes -x509 -subj "/C=US/ST=State/L=Town/O=Office/CN=localhost" -keyout /usr/local/etc/nginx/ssl/localhost.key -out /usr/local/etc/nginx/ssl/localhost.crt
- openssl req -new -newkey rsa:4096 -days 365 -nodes -x509 -subj "/C=US/ST=State/L=Town/O=Office/CN=phpmyadmin" -keyout /usr/local/etc/nginx/ssl/phpmyadmin.key -out /usr/local/etc/nginx/ssl/phpmyadmin.crt
创建软链接:
- ln -sfv /usr/local/etc/nginx/sites-available/default /usr/local/etc/nginx/sites-enabled/default
- ln -sfv /usr/local/etc/nginx/sites-available/default-ssl /usr/local/etc/nginx/sites-enabled/default-ssl
- ln -sfv /usr/local/etc/nginx/sites-available/phpmyadmin /usr/local/etc/nginx/sites-enabled/phpmyadmin
再次启动 Nginx:
- sudo launchctl load / Library / LaunchDaemons / homebrew.mxcl.nginx.plist
基本上完事了,点击下面的链接来感受下吧:
以后你肯定会关闭或者重启上面设置的某个服务的,所以你可能需要设置一些别名。(需要修改 bash_aliases 中的 php 版本)
- # If you use Bash
- curl -L https://gist.github.com/frdmn/7853158/raw/bash_aliases -o /tmp/.bash_aliases
- cat /tmp/.bash_aliases >> ~/.bash_aliases
- echo "source ~/.bash_aliases" >> ~/.bash_profile
- # If you use ZSH
- curl -L https://gist.github.com/frdmn/7853158/raw/bash_aliases -o /tmp/.zshrc
- cat /tmp/.bash_aliases >> ~/.zshrc
- echo "source ~/.bash_aliases" >> ~/.zshrc
再打开一个终端窗口,来使上面的别名生效:
- source ~/.bash_profile
- # or
- source ~/.zshrc
现在你可以使用别名而不是
和那一长串参数了~
- launchctl
你可以这样来启动、关闭、重启 Nginx:
- nginx.start
- nginx.stop
- nginx.restart
快速 tail 访问日志和错误日志:
- nginx.logs.access
- nginx.logs.default.access
- nginx.logs.phpmyadmin.access
- nginx.logs.default-ssl.access
- nginx.logs.error
- nginx.logs.phpmyadmin.error
检查配置:
- sudo nginx - t
启动、关闭、重启 PHP-FPM:
- php-fpm.start
- php-fpm.stop
- php-fpm.restart
检查配置:
- php - fpm - t
启动、关闭、重启 MySQL:
- mysql.start
- mysql.stop
- mysql.restart
php+mysql+nginx+phpmyadmin 环境搭建
来源: http://www.bubuko.com/infodetail-2073557.html