macos10.9 配置nginx,php-fpm

参考文献:

http://dhq.me/mac-apt-get-homebrew

http://www.xiaoche.me/blog/2012/02/01/homebrew-install/

http://dhq.me/mac-install-nginx-mysql-php-fpm

http://www.cnblogs.com/zhongyuan/p/3313106.html

1.安装homebrew

ruby -e "$(curl -fsSL https://raw.github.com/Homebrew/homebrew/go/install)"

2.安装nginx

用 brew 一键安装 nignx:

brew install nginx

 这样就安装好了nginx,可以使用以下命令操作nginx:

#打开 nginx
sudo nginx
#重新加载配置|重启|停止|退出 nginx
sudo nginx -s reload|reopen|stop|quit
#测试配置是否有语法错误
sudo nginx -t

打开 nginx 后,默认的访问端口 8080,如果要改为常用的 80 端口,则要修改 "/usr/local/etc/nginx/nginx.conf" 下监听(listen)端口值。

默认的文件访问目录(root)是 "/usr/local/Cellar/nginx/1.2.6/html"(这里的1.2.6是安装的nginx的版本,文件夹名以安装的nginx版本为准)。

3.安装php-fpm

Mac是预装了php,不过很多扩展都没安装,目测最多只能在终端里执行下php指令,所以我选择重新安装php。由于 brew 默认是没有 php 安装,所以要使用 “brew tap” 来安装 brew 的第三方程序包,这里使用 josegonzalez 的php安装包,具体操作如下:

brew tap homebrew/dupes
brew tap josegonzalez/homebrew-php

执行完后,就可以用 brew 安装php了。这里php有几个版本可以安装,具体可以执行 "brew search php" 查看一下有什么php版本可以安装.这里我选择php5.5,并且附带安装php-fpm等等,都是可选的,比如冲突时也可选择不安装--without 某些选项,命令如下:

brew install php55 --with-imap --with-tidy --with-debug --with-pgsql --with-mysql --with-fpm

更多的安装选项可以通过 "brew options php54" 查看。指令执行完后,php 跟 php-fpm 就安装好了。

由于是重装php,之前系统预装的php还没卸载,因此在终端调用php时,还是以之前系统的php版本做解析,所以这里需要修改path,指定 php 的解析路径。这里我选择的是修改系统级的path顺序。打开/etc/path,用vim对之进行修改:将/usr/local/sbin 放在/usr/sbin 前边(英文为安装完php-fpm命令行的提示)。

1 Mountain Lion comes with php-fpm pre-installed, to ensure you are using the brew version you need to make sure /usr/local/sbin is before /usr/sbin in your PATH:
4 
5   PATH="/usr/local/sbin:$PATH"
6 
9 You may also need to edit the plist to use the correct "UserName".

如下为更改后的顺序。

1 cat /etc/paths
2 /usr/local/sbin
3 /usr/local/bin
4 /usr/bin
5 /bin
6 /usr/sbin
7 /sbin

可以选择开机启动php-fpm:

 1 To launch php-fpm on startup:
 2 
 3     * If this is your first install:
 4 
 5         mkdir -p ~/Library/LaunchAgents
 6 
 7         cp /usr/local/Cellar/php55/5.5.20/homebrew.mxcl.php55.plist ~/Library/LaunchAgents/
 8 
 9         launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.php55.plist
10 
11 
12 
13     * If this is an upgrade and you already have the homebrew.mxcl.php55.plist loaded:
14 
15         launchctl unload -w ~/Library/LaunchAgents/homebrew.mxcl.php55.plist
16 
17         cp /usr/local/Cellar/php55/5.5.20/homebrew.mxcl.php55.plist ~/Library/LaunchAgents/
18 
19         launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.php55.plist

至此,php-fpm安装完成。

要启动php-fpm?直接:

1 php-fpm

会出错(一般为 fail to open error_log等),这是因为要打开的是内置等php-fpm,这时有两种方式打开:

第一种:用超级用户打开,应该是按照系统级等path顺序来的

1 sudo -s  #开启root
2 php-fpm

第二种:直接在脚本所在位置(/usr/local/Cellar/php55/5.5.20/sbin/)打开:

1 The control script is located at /usr/local/Cellar/php55/5.5.20/sbin/php55-fpm
cd /usr/local/Cellar/php55/5.5.20/sbin
php55-fpm

4.配置nginx php-fpm

打开 nginx 默认注释掉的php location设置,修改如下(具体配置参数,例如路径,这里以我本地安装为准,注意修改你的对应的版本号,这里是1.6.2): 

1 location ~ .php$ {
2     fastcgi_intercept_errors on;
3     fastcgi_pass   127.0.0.1:9000;
4     fastcgi_index  index.php;
5     fastcgi_param  SCRIPT_FILENAME  /usr/local/Cellar/nginx/1.6.2/html$fastcgi_script_name;
6     include        /usr/local/etc/nginx/fastcgi_params;
7 }

5.测试

在nginx的默认发布根目录' /usr/local/Cellar/nginx/1.6.2/html '下写一个test.php,然后在浏览器输入url:localhost:8080/test.php,出现php的信息页面说明成功配置!

1 #test.php
2 <?php
3     phpinfo();    
4 ?>
原文地址:https://www.cnblogs.com/sungyouyu/p/4206817.html