網(wǎng)上找到的大部分教程均已過時,自己總結(jié)了當(dāng)前(2018年6月)在樹莓派上可行的 Nginx + PHP 的安裝方法
-
安裝 Nginx
sudo apt install nginx
至此靜態(tài)網(wǎng)頁已經(jīng)支持番川,可以嘗試進(jìn)行訪問箫荡,正常將會出現(xiàn)“Welcome to nginx!”頁面
-
安裝 PHP-FPM
sudo apt install php-fpm
網(wǎng)上找到的很多教程這里還是 php5-fpm树肃,現(xiàn)在的 php 早已更新至 7.0
-
修改 Nginx 的配置文件
找到如下內(nèi)容
# Add index.php to the list if you are using PHP index index.html index.htm index.nginx-debian.html;
根據(jù)注釋的說明,加上
index.php
來更好地支持 php修改后
# Add index.php to the list if you are using PHP index index.html index.htm index.nginx-debian.html index.php;
接下來找到如下內(nèi)容
# pass PHP scripts to FastCGI server # #location ~ \.php$ { # include snippets/fastcgi-php.conf; # # # With php-fpm (or other unix sockets): # fastcgi_pass unix:/var/run/php/php7.0-fpm.sock; # # With php-cgi (or other tcp sockets): # fastcgi_pass 127.0.0.1:9000; #}
這部分的內(nèi)容是選擇當(dāng)遇到 php 腳本時應(yīng)該如何處理
由于剛剛安裝的是
php-fpm
考余,所以去掉部分注釋即可修改后(注意整塊內(nèi)容的完整饲窿,不要缺少了大括號)
# pass PHP scripts to FastCGI server # location ~ \.php$ { include snippets/fastcgi-php.conf; # # # With php-fpm (or other unix sockets): fastcgi_pass unix:/var/run/php/php7.0-fpm.sock; # # With php-cgi (or other tcp sockets): # fastcgi_pass 127.0.0.1:9000; }
也可以選擇刪除多余內(nèi)容,只保留如下內(nèi)容
location ~ \.php$ { include snippets/fastcgi-php.conf; fastcgi_pass unix:/var/run/php/php7.0-fpm.sock; }
修改完畢,重啟 Nginx 服務(wù)
systemctl restart nginx
-
測試
向 Nginx 的網(wǎng)頁存放位置(默認(rèn)是
/var/www/html
)新建一個index.php
淑翼,內(nèi)容如下<?php echo phpinfo(); ?>
正常情況訪問網(wǎng)站將出現(xiàn) PHP 的信息頁面