1. 程式人生 > >Mac OSX 10.9搭建nginx+mysql+php-fpm環境

Mac OSX 10.9搭建nginx+mysql+php-fpm環境

安裝homebrew

homebrew是mac下非常好用的包管理器,會自動安裝相關的依賴包,將你從繁瑣的軟體依賴安裝中解放出來。 
安裝homebrew也非常簡單,只要在終端中輸入:

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

homebrew的常用命令:

<!-- lang: shell -->
brew update #更新可安裝包的最新資訊,建議每次安裝前都執行下
brew search pkg_name #搜尋相關的包資訊
brew install pkg_name #安裝包

想了解更多地資訊,請參看homebrew

安裝nginx

安裝

<!-- lang: shell -->
brew search nginx
brew install nginx

當前的最新版本是1.4.4

配置

<!-- lang: shell -->
cd /usr/local/etc/nginx/
mkdir conf.d
vim nginx.conf
vim ./conf.d/default.conf

nginx.conf內容,

<!-- lang: shell -->
worker_processes  1;  

error_log       /usr/local
/var/log/nginx/error.log warn; pid /usr/local/var/run/nginx.pid; events { worker_connections 256; } 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/var/log/nginx/access.log main; port_in_redirect off; sendfile on; keepalive_timeout 65; include /usr/local/etc/nginx/conf.d/*.conf; }

default.conf檔案內容,

<!-- lang: shell -->
server {
    listen       8080;
    server_name  localhost;

    root /Users/user_name/nginx_sites/; # 該項要修改為你準備存放相關網頁的路徑

    location / { 
        index index.php;
        autoindex on; 
    }   

    #proxy the php scripts to php-fpm  
    location ~ \.php$ {
        include /usr/local/etc/nginx/fastcgi.conf;
        fastcgi_intercept_errors on; 
        fastcgi_pass   127.0.0.1:9000; 
    }   

}

安裝php-fpm

Mac OSX 10.9的系統自帶了PHP、php-fpm,省去了安裝php-fpm的麻煩。 
這裡需要簡單地修改下php-fpm的配置,否則執行php-fpm會報錯。

<!-- lang: shell -->
sudo cp /private/etc/php-fpm.conf.default /private/etc/php-fpm.conf
vim /private/etc/php-fpm.conf

修改php-fpm.conf檔案中的error_log項,預設該項被註釋掉,這裡需要去註釋並且修改為error_log = /usr/local/var/log/php-fpm.log。如果不修改該值,執行php-fpm的時候會提示log檔案輸出路徑不存在的錯誤。

安裝mysql

安裝

<!-- lang: shell -->
brew install mysql

常用命令

<!-- lang: shell -->
mysql.server start #啟動mysql服務
mysql.server stop #關閉mysql服務

配置 
在終端執行mysql_secure_installation指令碼,該指令碼會一步步提示你設定一系列安全性相關的引數,包括:設定root密碼關閉匿名訪問不允許root使用者遠端訪問移除test資料庫。當然執行該指令碼前記得先啟動mysql服務。

測試nginx服務

在之前nginx配置檔案default.conf中設定的root項對應的資料夾下建立測試檔案index.php:


<!-- ~/nginx_sites/index.php -->
<?php phpinfo(); ?>

啟動nginx服務,sudo nginx; 
修改配置檔案,重啟nginx服務,sudo nginx -s reload 
啟動php服務,sudo php-fpm; 
在瀏覽器位址列中輸入localhost:8080,如果配置正確地話,應該能看到PHP相關資訊的頁面。