nginx + PHP-FPM = “permission denied” error 13 in nginx log; configuration mistake?

出至:http://serverfault.com/questions/170192/nginx-php-fpm-permission-denied-error-13-in-nginx-log-configuration-mista

因為新建立的目錄無法讓 nginx 服務進入到該目錄(沒有[x]的權限屬性),所以會找不到指定路徑的檔案(在新建目錄下的檔案)。雖然我確定這些檔案路徑是存在的,但網頁服務卻無法取得檔案。

解決方法:將新建立的每一層目錄都設定能讓nginx服務可以進入,nginx就不會再找不到檔案了。

You need to ensure you have +x on all of the directories in the path leading to the site’s root – so /home, /home/noisepages and /home/noisepages/www

nginx codeigniter fast-cgi + virtual-host(它因擾我至少兩天)

/etc/nginx/conf.d/virt.conf

#
# The default server
#
server {
listen 80;
server_name carbonfootprint.toyoink-ct.com.tw;

index carbonfootprint.php;
if (-e $request_filename) {
break;
}
if (!-e $request_filename) {
rewrite ^/(.*)$ /carbonfootprint.php/$1 last;
break;
}
#charset koi8-r;

#access_log logs/host.access.log main;

location / {
root /usr/share/nginx/html;
index carbonfootprint.php ;
try_files $uri $uri/ /carbonfootprint.php;
}

error_page 404 /404.html;
location = /404.html {
root /usr/share/nginx/html;
}

# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~* \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index carbonfootprint.php;
fastcgi_split_path_info ^(.+\.php)(.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
# deny access to .htaccess files, if Apache’s document root
# concurs with nginx’s one
#
#location ~ /\.ht {
# deny all;
#}
}

為什麼安裝完 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).