LNMP1.3一键安装Linux环境,配置Nginx运行ThinkPHP3.2

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

原文地址:https://www.cnblogs.com/tangbomao/p/6882801.html