這裡介紹如何在樹梅派(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 的位址來看看網頁是否可以正常打開。
如果只是要一般靜態的網頁伺服器,這樣就完成了,以下是 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 的相關資訊了。
本文介绍如何在树莓派上安装NGINX网页伺服器与PHP,通过详细步骤实现轻量级网页伺服器搭建。文章涵盖从安装NGINX到配置PHP-FPM的全过程。


1900

被折叠的 条评论
为什么被折叠?



