温馨提示×

ubuntu如何设置thinkphp伪静态

小樊
41
2026-09-28 17:54:10
栏目: 编程语言

在 Ubuntu 上设置 ThinkPHP 伪静态,通常取决于你使用的 Web 服务器(Apache 或 Nginx)。下面分别说明。


一、使用 Apache(最常见)

1. 开启 rewrite 模块

sudo a2enmod rewrite
sudo systemctl restart apache2

2. 修改虚拟主机配置

编辑你的站点配置(如 /etc/apache2/sites-enabled/000-default.conf):

<VirtualHost *:80>
    DocumentRoot /var/www/html/public
    <Directory /var/www/html/public>
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>
</VirtualHost>

⚠️ 关键点是 AllowOverride All

3. ThinkPHP 根目录添加 .htaccess

如果是 ThinkPHP 5 / 6,在 public 目录下已有 .htaccess,内容一般是:

<IfModule mod_rewrite.c>
  Options +FollowSymlinks -Multiviews
  RewriteEngine On

  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteRule ^(.*)$ index.php/$1 [QSA,PT,L]
</IfModule>

4. 重启 Apache

sudo systemctl restart apache2

二、使用 Nginx

1. 修改 Nginx 站点配置

编辑 /etc/nginx/sites-enabled/default 或你的站点配置:

server {
    listen 80;
    server_name yourdomain.com;
    root /var/www/html/public;

    index index.php index.html;

    location / {
        if (!-e $request_filename) {
            rewrite ^(.*)$ /index.php?s=/$1 last;
            break;
        }
    }

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
    }
}

ThinkPHP 6 推荐用 ?s= 方式

2. 测试并重载 Nginx

sudo nginx -t
sudo systemctl reload nginx

三、ThinkPHP 配置确认

确保 config/app.php 中:

'url_rewrite' => true,

(ThinkPHP 6 默认已开启)


四、常见坑

✅ 访问不了?

  • Apache:检查 AllowOverride All
  • Nginx:检查 root 是否指向 public

✅ 出现 No input file specified

  • Nginx 伪静态写法不对,改用 ?s=

如果你能告诉我:

  • ThinkPHP 版本(5 / 6 / 8)
  • Web 服务器(Apache / Nginx)

我可以给你 精确配置。

0 踩