為什麼安裝完 nginx + php5-fpm 無法執行php腳本?

因為預設 php-fpm 是監聽 tcp:9000 。但是我看了某個網站的安裝教學,他的「/etc/nginx/site-available/default」設定是採用sock的設定方式並非預設值 localhost:9000 ,所以必須將它改為 unix:path.sock up 可以正常啟動執行PHP腳本。

/etc/nginx/sites-available/default

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    location ~ \.php$ {
        try_files $uri = 440;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini

        # With php5-cgi alone:
        fastcgi_pass 127.0.0.1:9000;
        # With php5-fpm:
        #fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
    }

由上面的設定檔片段可得知「fastcgi_pass」有兩種設定方式「php5-cgi」、「php5-fpm」。

/etc/php5/fpm/pool.d/www.conf

; The address on which to accept FastCGI requests.
; Valid syntaxes are:
;   'ip.add.re.ss:port'    - to listen on a TCP socket to a specific address on
;                            a specific port;
;   'port'                 - to listen on a TCP socket to all addresses on a
;                            specific port;
;   '/path/to/unix/socket' - to listen on a unix socket.
; Note: This value is mandatory.
listen = 127.0.0.1:9000
;listen = /var/run/php5-fpm.sock

而 nginx 設定檔片段顯示兩種方式的設定,「listen=localhost:9000」或是「listen=/var/run/php5-fpm.sock」。

僅需將兩邊的設定檔fastcgi-pass」與「listen」參數值設定相同即能正常運作。/etc/nginx/sites-available/default 的「fastcgi-pass」決定系統連接端口,而/etc/php5/fpm/pool.d/www.conf 的「listen」僅僅是設定對應的連接端口罷了

On a default installation, PHP FPM is only started for listening on TCP port 9000 on address 127.0.0.1. This can be changed in /etc/php5/fpm/pool.d/www.conf.

Look for the line listen = 127.0.0.1:9000 and change it to something like listen = /var/run/php5-fpm.sock. After doing so, restart PHP FPM:

sudo /etc/init.d/php5-fpm restart
(note: reload is broken on Oneiric as it sends a HUP signal to php5-fpm which is wrong. It’s fixed in Precise by sending USR2, bugreport).

發表迴響

在下方填入你的資料或按右方圖示以社群網站登入:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / 變更 )

Twitter picture

You are commenting using your Twitter account. Log Out / 變更 )

Facebook照片

You are commenting using your Facebook account. Log Out / 變更 )

Google+ photo

You are commenting using your Google+ account. Log Out / 變更 )

連結到 %s