安裝
安裝相關軟體:
- php:PHP 程式語言
- php-fpm:也就是 FastCGI,透過它來讓 Nginx 與 PHP 之間交互連動
# yum install php php-fpm
PHP-FPM
設定
修改 php-fpm 的配置
;listen = 127.0.0.1:9000
listen =
/var/run/php-fpm/php-fpm
.sock
;user = apache
user = nginx
;group = apache
group = nginx
; 預設帳戶、群組,為正在運作的帳戶
;listen.owner = nobody
;listen.group = nobody
listen.owner = nginx
listen.group = nginx
; 權限(預設為 0666)
;listen.mode = 0666
; session 的路徑
php_value[session.save_path] =
/var/lib/php/session
修改 session 路徑的擁有者、群組為 nginx
# chown nginx:nginx /var/lib/php/session/
Nginx 與 PHP 連動設定
Nginx 針對 PHP 的設定,這樣才可透過 php-fpm 讓 Nginx 與 PHP 之間交互連動
server{
listen 80;
server_name yunkus.com; # 網站網址
root /home/www/nginx.yunkus.com; # 網站的根目錄位置
index index.php index.html; # 網站讀取的預設文件,按照順序
location / {
# This is cool because no php is touched for static content.
# include the "?$args" part so non-default permalinks doesn't break when using query string
try_files $uri $uri/ /index.php?$args; # Enable joomla SEF URL's inside Nginx
}
location = /favicon.ico {
log_not_found off;
access_log off;
}
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}
location ~ \.php$ { # 讀 php 檔,以下4行都要加進去
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
修改網站根目錄的擁有者、群組為 nginx
# chown -R nginx:nginx /home/web/html/
重啟 Nginx 服務
# systemctl restart nginx
啟用 PHP-FPM 服務並開機自動啟用
# systemctl start php-fpm
# systemctl enable php-fpm
在網頁根目錄新增一個顯示 php 資訊的檔案
/home/web/html/index.php
程式碼:
<?php
phpinfo();
?>
打開瀏覽器輸入網址 http://Server IP/index.php,沒有問題就會看到 php 資訊
參考:https://smalljacky.com/linux/centos/centos7-nginx-mariadb-php-phpmyadmin/