樹梅派 Raspberry Pi 安裝 NGINX 與 PHP 網頁伺服器(PHP-FPM)

這裡介紹如何在樹梅派(Raspberry Pi)上安裝 NGINX 與 PHP,打造輕量級的網頁伺服器。

NGINX 是目前很熱門的網頁伺服器,它靠著 Non-blocking 與 epoll(Linux 2.6 以後支援)等技術,讓服務連線數與處理效能大幅提升,比起傳統的 Apache 伺服器更節省系統資源,因此近年來一推出就馬上成為市場上的焦點。


以下是在 Raspberry Pi 上安裝 NGINX 伺服器與 PHP 的步驟。
STEP 1
使用 apt 安裝基本的 NGINX 網頁伺服器:

sudo apt-get install nginx

STEP 2
正常來說安裝好 NGINX 之後,會自動啟動,我們可以用 service 檢查一下這個服務的狀態:

service nginx status

輸出會像這樣

● nginx.service - A high performance web server and a reverse proxy server
   Loaded: loaded (/lib/systemd/system/nginx.service; enabled)
   Active: active (running) since Fri 2015-10-09 16:53:34 CST; 23s ago
 Main PID: 1731 (nginx)
   CGroup: /system.slice/nginx.service
           ├─1731 nginx: master process /usr/sbin/nginx -g daemon on; master_...
           ├─1732 nginx: worker process
           ├─1733 nginx: worker process
           ├─1734 nginx: worker process
           └─1735 nginx: worker process

STEP 3
另外也可以直接打開瀏覽器,輸入 Raspberry Pi 的位址來看看網頁是否可以正常打開。

raspberry-pi-install-nginx-lightweight-web-server-1.png

NGINX 伺服器初始網頁

如果只是要一般靜態的網頁伺服器,這樣就完成了,以下是 PHP 的安裝步驟,請繼續閱讀下一頁。


STEP 4
用 apt 安裝 PHP-FPM:

sudo apt-get install php5-fpm

STEP 5
設定 NGINX 伺服器,開啟 /etc/nginx/sites-available/default 這個設定檔,找到這一段:

# Add index.php to the list if you are using PHP
index index.html index.htm index.nginx-debian.html;

加入 index.php,修改成這樣:

# Add index.php to the list if you are using PHP
index index.php index.html index.htm index.nginx-debian.html;

STEP 6
同樣在 /etc/nginx/sites-available/default 這個設定檔,找到這一段:

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
#       include snippets/fastcgi-php.conf;
#
#       # With php5-cgi alone:
#       fastcgi_pass 127.0.0.1:9000;
#       # With php5-fpm:
#       fastcgi_pass unix:/var/run/php5-fpm.sock;
#}

把這一段的註解拿掉,而 fastcgi_pass 的部分則選擇 php5-fpm 的設定,php5-cgi 的版本要註解起來,變成這樣:

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000

location ~ \.php$ {
       include snippets/fastcgi-php.conf;

       # With php5-cgi alone:
       # fastcgi_pass 127.0.0.1:9000;
       # With php5-fpm:
       fastcgi_pass unix:/var/run/php5-fpm.sock;
}

STEP 7
讓 NGINX 重新設定檔:

sudo service nginx reload

STEP 8
NGINX 預設的網頁放置位置是 /var/www/html,在這個位置新增一個測試的index.php 網頁,內容如下:

<?php echo phpinfo(); ?>

STEP 9
打開瀏覽器,輸入 Raspberry Pi 的位址,正常的話應該就可以看到 PHP 的相關資訊了。

raspberry-pi-install-nginx-lightweight-web-server-2

phpinfo 畫面


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值