mac os nginx php mysql

starsfun發表於2016-04-12

最近mac Air重做了系統後,想配置lnmp環境,但是搜尋了很多頁面都以失敗告終,在這裡特別感謝http://www.zhoujiping.com/archives/2016/01/mnmp.html,他給我提供了很多的幫助。當然還有其它很多的朋友,這裡就不細描,但這個列的確實比較詳細。


準備工作


  1. 進入終端,鍵入gcc,如沒裝xcode命令列工具,點選安裝即可。

  2. ruby -e "$(curl -fsSL https://raw.github.com/Homebrew/homebrew/go/install)"安裝HomeBrew。(HomeBrew詳細用法見官網


安裝nginx


  1. brew install nginx

  2. nginx -v(看到nginx版本安裝)

  3. sudo nginx(啟動nginx)


安裝php56


  • 安裝php ,php-fpm

brew tap homebrew/dupes

brew tap homebrew/versions

brew tap homebrew/php

brew install php56 
--without-snmp 
--without-apache 
--with-debug 
--with-fpm 
--with-intl 
--with-homebrew-curl 
--with-homebrew-libxslt 
--with-homebrew-openssl 
--with-imap 
--with-mysql 
--with-tidy
  • 新增系統環境變數PATH來替代自帶PHP版本


echo `export PATH="$(brew --prefix php56)/bin:$PATH"` >> ~/.bash_profile
 
echo `export PATH="$(brew --prefix php56)/sbin:$PATH"` >> ~/.bash_profile
 
echo `export PATH="/usr/local/bin:/usr/local/sbin:$PATH"` >> ~/.bash_profile

source ~/.bash_profile
  • 修改php-fpm配置檔案

vim /usr/local/etc/php/5.6/php-fpm.conf

找到;pid = run/php-fpm.pid,去掉註釋(去掉前面的;),然後測試下php-fpm

php-fpm -t

除錯php-fpm程式碼

php-fpm -D

啟動php-fpm

lsof -Pni4 | grep LISTEN | grep php

執行監聽9000埠

ln -sfv /usr/local/opt/php56/*.plist ~/Library/LaunchAgents
launchctl load ~/Library/LaunchAgents/homebrew.mxcl.php56.plist

開機啟動

配置nginx,讓其支援php

Nginx本身不會對PHP進行解析,終端對PHP頁面的請求將會被Nginx交給FastCGI程式監聽的IP地址及埠(這就是為什麼我們啟動php-fpm時,要檢視下9000埠是否被監聽的原因),由php-fpm作為動態解析伺服器處理,最後將處理結果再返回給nginx。其實,Nginx就是一個反向代理伺服器。Nginx通過反向代理功能將動態請求轉向後端php-fpm,從而實現對PHP的解析支援,這就是Nginx實現PHP動態解析的原理。所以現在我們要做的就是讓nginx和php-fpm建立關係。如何建立關係呢? 主要是在nginx.conf檔案中加入下面這樣的程式碼。

location ~ .php$ {
            root           html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
            include        fastcgi_params;
        }

但是為了方便管理以後新建的網站,我們不會把所有的配置都放置在nginx.conf中,我們來規劃下:

mkdir -p /usr/local/var/logs/nginx
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全域性配置

vim /usr/local/etc/nginx/nginx.conf

輸入內容

worker_processes  1;
error_log   /usr/local/var/logs/nginx/error.log debug;
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" $host $request_time $upstream_response_time $scheme `
        `$cookie_evalogin`;
    access_log  /usr/local/var/logs/access.log  main;
    sendfile        on;
    keepalive_timeout  65;
    port_in_redirect off;
    include /usr/local/etc/nginx/sites-enabled/*.conf;
}

把一些可複用配置獨立出來放在/usr/local/etc/nginx/conf.d下,比如fastcgi的設定

vim /usr/local/etc/nginx/conf.d/php-fpm.conf

輸入內容

location ~ .php$ {
    try_files                   $uri = 404;
    fastcgi_pass                127.0.0.1:9000;
    fastcgi_index               index.php;
    fastcgi_intercept_errors    on;
    include /usr/local/etc/nginx/fastcgi.conf;
}

以後要增加新域名,只要在/usr/local/etc/nginx/sites-enabled/目錄下可以一個檔案對應一個域名的配置,我們試著來建立一個預設網站,預設網站的根目錄放在/var/www/default下面,在/var/www/中建立default資料夾,並在default中建立info.php,在其中輸入內容

mkdir -p /var/www/default
vim /var/www/default/info.php

然後在/usr/local/etc/nginx/sites-enabled/下面建立個配置檔案default.conf

vim /usr/local/etc/nginx/sites-enabled/default.conf

輸入

server {
    listen       8080;
    server_name  localhost;
    root         /var/www/default;
    location / {
        index  index.html index.htm index.php;
        include     /usr/local/etc/nginx/conf.d/php-fpm.conf;
    }
}
  • restart nginx*

sudo nginx -s reload 

相關文章