1. 程式人生 > >LNMP1.3一鍵安裝Linux環境,配置Nginx運行ThinkPHP3.2

LNMP1.3一鍵安裝Linux環境,配置Nginx運行ThinkPHP3.2

搜索 最終 ati 兼容 access eal inf pic enter

LNMP1.3一鍵安裝Linux環境,配置Nginx運行ThinkPHP3.2

技術分享

你是否遇見過:安裝LNMP1.3環境後,運行ThinkPHP 3.2,只能打開首頁,不能訪問控制器,報404錯誤。

按照以下3步設置,即可解決。

ThinkPHP支持的URL模式有四種:普通模式、PATHINFO、REWRITE和兼容模式,系統默認的PATHINFO模式。

LNMP1.3 一鍵安裝完成後,默認支持REWRITE,需要手動開啟 PATHINFO。

第1步修改:php.ini文件
位置:/usr/local/php/etc/php.ini

搜索查找到:cgi.fix_pathinfo 配置項,默認為0,修改為1,開啟 pathinfo 選項。

如圖1:

技術分享

第2步修改:nginx的配置文件 (筆者使用的是虛擬域名配置文件:/usr/local/nginx/conf/vhost/*.conf)

找到 server 的配置選項:

默認只有 include enable-php.conf,請註釋掉;
然後添加一行:include enable-php-pathinfo.conf

如:

1
2
3
#error_page   404   /404.html;
#include enable-php.conf;             # 註冊這一行
include enable-php-pathinfo.conf;     # 加入這行

如圖2:

技術分享

繼續修改,在添加下面配置信息:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
location ~ .php
{
    set $path_info "";
    set $real_script_name $fastcgi_script_name;
    #如果地址與引號內的正則表達式匹配
    if ($fastcgi_script_name ~ "^(.+?\.php)(/.+)$") {
    #將文件地址賦值給變量 $real_script_name
    set $real_script_name $1;
    #將文件地址後的參數賦值給變量 $path_info
    set $path_info $2;
    }
    
    #配置fastcgi的一些參數
    fastcgi_param SCRIPT_FILENAME $document_root$real_script_name;
    fastcgi_param SCRIPT_NAME $real_script_name;
    fastcgi_param PATH_INFO $path_info;
}

在Nginx,可以通過在Nginx.conf中配置轉發規則實現,解決其他不支持PATHINFO的WEB服務器環境。

#如果請求既不是一個文件,也不是一個目錄,則執行一下重寫規則

1
2
3
4
5
6
7
if (!-e $request_filename)
{
	#地址作為將參數rewrite到index.php上。
	rewrite ^/(.*)$ /index.php/$1;
	#若是子目錄則使用下面這句,將subdir改成目錄名稱即可。
	#rewrite ^/subdir/(.*)$ /subdir/index.php/$1;
}

官方出處:http://document.thinkphp.cn/manual_3_2.html#url_rewrite

第3步:重啟LNMP環境,配置生效。

最終效果測試:
1. 去掉了 index.php
2. 可以訪問控制器下的方法。
3. U 方法正確。

如圖3:
技術分享

參考:筆者配置文件示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
server
    {
        listen 80;
        #listen [::]:80;
        server_name tp32.com;
        index index.html index.htm index.php default.html default.htm default.php;
        root  /home/wwwroot/tp32.com;

        include other.conf;
        #error_page   404   /404.html;
        #include enable-php.conf;
        include enable-php-pathinfo.conf; #加入這行


        location ~ .php
        {
            set $path_info "";
            set $real_script_name $fastcgi_script_name;
            #如果地址與引號內的正則表達式匹配
            if ($fastcgi_script_name ~ "^(.+?\.php)(/.+)$") {
            #將文件地址賦值給變量 $real_script_name
            set $real_script_name $1;
            #將文件地址後的參數賦值給變量 $path_info
            set $path_info $2;
            }
            
            #配置fastcgi的一些參數
            fastcgi_param SCRIPT_FILENAME $document_root$real_script_name;
            fastcgi_param SCRIPT_NAME $real_script_name;
            fastcgi_param PATH_INFO $path_info;
        }

        #如果請求既不是一個文件,也不是一個目錄,則執行一下重寫規則
        if (!-e $request_filename)
        {
            #地址作為將參數rewrite到index.php上。
            rewrite ^/(.*)$ /index.php/$1;
            #若是子目錄則使用下面這句,將subdir改成目錄名稱即可。
            #rewrite ^/subdir/(.*)$ /subdir/index.php/$1;
        }

        access_log  /home/wwwlogs/tp32.com.log;
    }

入口文件index.php

1
2
//nginx環境下防止U方法輸出錯誤
define(‘__APP__‘, ‘‘);

參考:
http://www.thinkphp.cn/topic/3138.html

LNMP1.3一鍵安裝Linux環境,配置Nginx運行ThinkPHP3.2