Development

紀錄一下最近在Mac上用到的開發環境 > Nginx + PHP + PHP-FPM (利用homebrew來安裝)


Homebrew

Installation

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

Useful Command

# list all your installed Homebrew packages.
brew list
# search packages version avaliable.
brew search

Nginx

Installation

brew install nginx 

Setting

nginx.conf

#user  nobody;
worker_processes  1;

error_log  /var/log/nginx/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;

events {
    worker_connections  1024;
}

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  logs/access.log  main;
    access_log /var/log/nginx/default.access.log  main;

    sendfile        on;
    charset utf-8;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    gzip  on;

    server {
        listen       8080;
        server_name  _;

        location / {
            root   /usr/local/var/www;
            index  index.html index.htm;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #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;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }

    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}
    
    include sites/*.conf;
}

做到這裡你可以先嘗試nginx有沒有正常啟動, 啟動nginx sudo nginx(在不同環境下啟動的指令會有點不太一樣, 請自行google). 請動後再瀏覽器上輸入 http://localhost:8080/會得到 **Welcome to nginx!**的畫面.

接著你必須創立一個資料夾mkdir sites 通常我的習慣是放VPS的config檔在這個資料夾下.

example.conf

server {
    listen              *:8080;
    server_name         your_server_name;
    access_log          /var/log/nginx/access.log  main;
    error_log           /var/log/nginx/error.log;

    root   /path/to/your/workspace;
    index  index.html index.htm index.php;


    # set expiration of assets to MAX for caching
    location ~* \.(ico|css|js|gif|jpe?g|png)(\?[0-9]+)?$ {
        expires max;
        log_not_found off;
    }

    # framework rewrite
    location / {
        try_files  $uri  $uri/  /index.php?$args;
    }

    location ~ \.php$ {

        fastcgi_param PATH_INFO $fastcgi_path_info;
        fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_intercept_errors on;
        
        include fastcgi_params;
    }
}

更改完conf檔後記得重新載入Nginx sudo nginx -s reload.

Host Name

然後修改本機端的host name. Path: /etc/hosts/

##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting.  Do not change this entry.
##
127.0.0.1   localhost
# add this line
127.0.0.1   server_name
255.255.255.255 broadcasthost
::1             localhost

p.s.

  • php 跟 nginx 的 user, group 記得從 nobody 更改為目標使用者
  • 如果端口不在 9000 請更改端口, 記得要跟PHP-FPM一致.
  • conf 中的 location 後面跟著是一個正規表達式,不一定是 / 開頭的.

PHP & PHP-FPM

Introduce to php-fpm

nginx本身不能處理PHP,它只是個web服務器,當接收到請求後,如果是php請求,則發給php解釋器處理,並把結果返回給客戶端. 所以nginx一般是把請求發fastcgi管理進程處理,fascgi管理進程選擇cgi子進程處理結果並返回被nginx. 那PHP-FPM就是一個FastCGI管理器.

FastCGI 的主要優點是把動態語言和HTTP Server分離開來,所以Nginx與PHP/PHP-FPM經常被部署在不同的服務器上,以分擔前端Nginx服務器的壓力,使Nginx專一處理靜態請求和轉發動態請求,而PHP/PHP-FPM服務器專一解析PHP動態請求.

Installation

打開Terminal輸入以下指令.

brew tap homebrew/dupes
brew tap homebrew/versions
brew tap homebrew/homebrew-php
# By default this formula comes with MySQL and Apache, 
brew install php56 --without-mysql --without-apache --with-fpm

Setting

php-fpm.conf

php-fpm 預設設定檔位於 /usr/local/etc/ 內, 以下幾件事要確認

  • listen = 127.0.0.1:9000, port與nginx的是否為一致.
  • php 跟 nginx 的 user, group 記得從 nobody 更改為目標使用者.

啟動php-fpm

sudo /usr/local/sbin/php56-fpm start

檢查 listening port

# you will see the listening info of nginx & php-fpm, it will take some time.
sudo lsof -i -P | grep -i "listen"

一切都沒問題了之後就打開瀏覽器輸入server_name:8080/index.html看一下結果.(記得要在你的workspace下建立index.html的檔案)

Reference