Raspberry Pi 的實作 - 在 Nginx 網頁伺服器上提供 PHP 的支援

by 10月 23, 2014 2 意見            

Nginx 提供了基本的 HTML 靜態網頁的服務,我們要讓網頁有動態處理的能力時,就必須依賴其他程式語言提供動態解析的能力了,例如常見的 ASP、ASP .Net、PHP、JSP、Perl ... 等等,而在 Linux 平台上,PHP 絕對是第一首選。
最後更新日期 - 2015/01/22

事前準備


開始安裝


Nginx 要提供 PHP 的支援,一般都會配合 PHP-FPM (FastCGI Process Manager) 來運作。

直接從 Raspbian 套件庫安裝即可。
sudo apt-get -y install php5-fpm

而目前 PHP 最新的穩定版是 5.6.4,不過不建議自行編譯,因為 Raspberry pi 的硬體等級太低,須花費太多時間。

有興趣的朋友請參考 Raspberry Pi台灣樹莓派 - [進階] 安裝 Raspberry Pi 的 Toolchain 或 google 關鍵字「cross compiler」。

也可以依照 Raspberry Pi 的基礎 - 系統設定的調教 裡,「修改套件庫的來源」的步驟,將套件來源改成 testing。


調整 Nginx 設定


編輯 nginx 的主站台設定檔。
sudo vi /etc/nginx/sites-enabled/default

找到下列的文字。
########## 我是分隔線 ##########
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {

#       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;
#}
########## 我是分隔線 ##########

改成這樣。
########## 我是分隔線 ##########
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {

       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;
}
########## 我是分隔線 ##########


調整 PHP-FPM 設定


Nginx 配合 PHP-FPM 時,有一個漏洞會導致在瀏覽器輸入類似 http://raspberrypi/123.jpg/bug.php 的 URL 時,PHP 檔案會被成功執行。

打開 PHP-FPM 的主設定檔。
sudo vi /etc/php5/fpm/php.ini

在設定檔裡找到 cgi.fix_pathinfo=1,改成 cgi.fix_pathinfo=0。如果沒有找到的話,也請加上這個參數。


啟動服務


重新啟動 Nginx 及 PHP 服務。
sudo service nginx restart

sudo service php5-fpm restart


新增一個 PHP 測試頁。
sudo vi /usr/share/nginx/html/info.php

輸入下列文字。
########## 我是分隔線 ##########
<?php
  phpinfo();
?>

########## 我是分隔線 ##########

用瀏覽器打開 http://Raspberry Pi 的 IP/info.php,出現下列的 PHP 資訊頁面時,就代表設定成功了。


系列文章


參考資料


更新紀錄

  • 2014/10/23 撰文。
  • 2015/01/22 更新 PHP-FPM 的版號。

Heracles Jam,江湖人稱「海公公」

IT 技術家 - 創站部落客

一個常用 Windows、慣用 macOS、愛用 Linux 的 3C 阿宅
現職 MIS / 業界講師 / 資訊顧問 / 部落客

2 則留言 :

  1. 大大,那個漏洞在新版的PHP裡面已經沒有了~
    因為在 www.conf 中有個 security.limit_extensions ,參數為php,也就是說只能連結到副檔名為php的文件

    P.S. OpenSSL 不支援 1.0.1了,我自己是灌了1.0.2o~不過步驟差不多,所以沒關係

    回覆刪除
  2. Unlike the widespread themes, you can see unique items, such as classic literature, TV exhibits, and more. Fairground Gaming was an internet gaming company traded on the Alternative Investment Market of the London Stock Exchange as FGH.L. The only drawback appears to be that Kiwis casino.edu.kg should enroll as members to entry this glorious service. Ideally, Spin Casino would have gifted this assist earlier than New Zealand gamblers turn out to be a member as a result of|as a outcome of} they may encounter issues resulting in an indication in.

    回覆刪除