1,515 瀏覽數

Nginx (Web server) + PHP-FPM

工作平台為 CentOS 7.2 最小安裝. Nginx (音同 Engine X)
前一篇 http://benjr.tw/95761 使用 Nginx 來架設 Web (HTTP) 伺服器,但 nginx 並不認識 PHP ,這時候開啟 PHP 檔案 Nginx 會把它當作一般檔案來下載,所以需要透過另外一個服務 PHP-FPM (FastCGI Process Manager) 將所有 PHP 導向給他 .

[root@localhost ~]# yum install php-fpm
Dependencies Resolved

================================================================================================
 Package               Arch              Version                       Repository          Size
================================================================================================
Installing:
 php-fpm               x86_64            5.4.16-36.3.el7_2             updates            1.4 M
Installing for dependencies:
 libzip                x86_64            0.10.1-8.el7                  base                48 k
 php-common            x86_64            5.4.16-36.3.el7_2             updates            563 k

Transaction Summary
================================================================================================
Install 1 Package (+2 Dependent packages)

確認一下 php-fpm 是否開啟 #systemctl start , 開機是否啟動 #systemctl enable

[root@localhost ~]# systemctl start php-fpm
[root@localhost ~]# systemctl enable php-fpm
Created symlink from /etc/systemd/system/multi-user.target.wants/php-fpm.service to /usr/lib/systemd/system/php-fpm.service.
[root@localhost ~]# systemctl status php-fpm
● php-fpm.service - The PHP FastCGI Process Manager
   Loaded: loaded (/usr/lib/systemd/system/php-fpm.service; enabled; vendor preset: disabled)
   Active: active (running) since 四 2016-10-13 21:10:52 CST; 12s ago
 Main PID: 2417 (php-fpm)
   Status: "Processes active: 0, idle: 5, Requests: 0, slow: 0, Traffic: 0req/sec"
   CGroup: /system.slice/php-fpm.service
           ├─2417 php-fpm: master process (/etc/php-fpm.conf)
           ├─2418 php-fpm: pool www
           ├─2419 php-fpm: pool www
           ├─2420 php-fpm: pool www
           ├─2421 php-fpm: pool www
           └─2422 php-fpm: pool www

10月 13 21:10:52 localhost.localdomain systemd[1]: Starting The PHP FastCGI Process Manager...
10月 13 21:10:52 localhost.localdomain systemd[1]: Started The PHP FastCGI Process Manager.
Hint: Some lines were ellipsized, use -l to show in full.

預設的 nginx 並不會把 PHP 導向給 php-fpm ,下面是 nginx 的原始 default.conf 設定檔,我們只需要修改 location ~ \.php$ 這一部分的內容即可.

[root@localhost ~]# vi /etc/nginx/conf.d/default.conf
server {
    listen       80;
    server_name  localhost;

    #charset koi8-r;
    #access_log  /var/log/nginx/log/host.access.log  main;

    location / {
        root   /usr/share/nginx/html;
        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   /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$ {
    #    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;
    #}
}

修改過後的 location ~ \.php$ 的內容.

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
#    root           html;
     root   /usr/share/nginx/html;
     fastcgi_pass   127.0.0.1:9000;
     fastcgi_index  index.php;
#    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
     fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
     include        fastcgi_params;
}

有修改過的部分為,主要都是跟 Document Root 對應的位址有關的設定.

  1. root /usr/share/nginx/html;
    這一行就是定義了 Document Root 對應的位址.
  2. fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    $document_root 也就是剛剛定義的 /usr/share/nginx/html
[root@localhost ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@localhost ~]# systemctl restart nginx.service

試一下 PHP 功能運作是否正常.用編輯器來鍵入下面的內容.

[root@benjr ~]# vi /usr/share/nginx/html/phpinfo.php
<?php
phpinfo();
?>

透過瀏覽器 http://yourIP/phpinfo.php 正確會看到如下的畫面:
php-fpm01

如果出現 File not found. 檢查一下 /var/log/nginx/error.log ,基本上都是 root 設定上的問題.

[root@localhost ~]# cat /var/log/nginx/error.log
FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream, client: 172.16.15.1, server: localhost, request: "GET /phpinfo.php HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "172.16.15.192"
FastCGI sent in stderr: "Unable to open primary script: /etc/nginx/html/info.php (No such file or directory)"

3 個網友的想法 “Nginx (Web server) + PHP-FPM

  1. 自動參照通知: WordPress 固定網址設定(Nginx) – Benjr.tw

  2. 自動參照通知: CentOS 7 + wordpress – Benjr.tw

  3. 自動參照通知: Nginx (Web server) – Benjr.tw

發表迴響