本文目錄一覽:
如何正確配置Nginx + PHP
2.在nginx.conf中加入下面一段配置
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html/$fastcgi_script_name;
include fastcgi_params;
}
/usr/local/nginx/html/$fastcgi_script_name; 這裡我寫的是php的絕對路徑,你可以修改成相應的。
如何正確配置Nginx+PHP
其實沒多複雜
1. 將nginx和php都裝好了
2. 然後配置nginx,將php請求分發給php-fpm處理
linux下的配置文件一般在/usr/local/nginx/conf/nginx.conf
找到下面字樣,並取消注釋,且注意這個$document_root這個地方(原本應為$script***的,改成$document_root)
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
3.啟動nginx和php-fpm,然後寫個phpinfo腳本測試下成功與否就可以了
如何正確配置 Nginx 和 PHP
直接貼上代碼逐行進行講解,此處貼出一個能正常啟動php腳本的最簡nginx vhost配置:
[plain] view plain copy
server {
listen 8011;
server_name test.cn;
location ~ \.php?.*$ {
root /share/test;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
1、第一個大括弧 server{ }:不必多說,代表一個獨立的server,
2、listen 8011:代表該server監聽8011埠
3、location ~ \.php?.*${
}:代表一個能匹配對應uri的location,用於匹配一類uri,並對所匹配的uri請求做自定義的邏輯、配置。這裡的location,匹配了所有帶.php的uri請求,例如:
等
4、root /share/test:請求資源根目錄,告訴匹配到該location下的uri到/share/teset文件夾下去尋找同名資源。
5、fastcgi_pass 127.0.0.1:9000:這行開始是本文的重點:這行代碼的意思是,將進入到該location內的uri請求看做是cgi程序,並將請求發送到9000埠,交由php-fpm處理。
6、fastcgi_param SCRIPT_FILENAME
$document_root$fastcgi_script_name;
:這行配置意思是:動態添加了一行fastcgi配置,配置內容為SCRIPT_FILENAME,告知管理進程,cgi腳本名稱。由於我的nginx中只有fastcgi_params文件,沒有fastcgi.conf文件,所以要使php-fpm知道SCRIPT_FILENAME的具體值,就必須要動態的添加這行配置。
7、include fastcgi_params; 引入fastcgi配置文件
以上就是最簡潔版的nginx啟動php腳本的最簡配置,當重啟nginx之後,在/share/test目錄下創建一個xx.php文件,輸入?php
echo “hello world”; ?保存,然後在瀏覽器中訪問localhost:8011/xx.php
就可以在網頁上顯示hello world了。
linux下nginx 需要配置php路徑嗎
需要。
如果是nginx+php配置,也可以通過查找php執行路徑
ps
aux
|
grep
php
如,路徑為
/usr/local/nginx/sbin/php-fpm
然後執行以下命令
/usr/local/nginx/sbin/php-fpm
-i
|
grep
「Loaded
Configuration
File」
即可看到php載入的配置文件
nginx配置支持php
nginx本身不支持php解析,需要配合php-fpm來配置。
location ~ \.php$ {
root /var/www; #指定php的根目錄
fastcgi_pass 127.0.0.1:9000;#php-fpm的默認埠是9000
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
centos下安裝php-fpm (php及其它組件已經安裝過的情況)
yum install php-fpm
啟動php-fpm 並設置開機啟動 (centos 7)
systemctl start php-fpm
systemctl enable php-fpm
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/229168.html