macos10.9 配置nginx,php-fpm

參考文獻:php

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

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

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

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

1.安裝homebrewgithub

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

 

2.安裝nginxsql

用 brew 一鍵安裝 nignx:vim

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)端口值。ruby

默認的文件訪問目錄(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 ?>
相關文章
相關標籤/搜索