温馨提示×

ubuntu thinkphp为啥报错

小樊
32
2026-09-16 18:24:13
栏目: 编程语言

在 Ubuntu 上运行 ThinkPHP 报错,原因通常和环境配置、权限、PHP 版本、路径或配置有关。下面我按最常见原因给你排查清单,你可以对照看。


一、最常见报错原因(90% 在这里)

1️⃣ PHP 版本不兼容

ThinkPHP 不同版本对 PHP 要求不同:

ThinkPHP PHP 版本
TP 5.0 PHP ≥ 5.6
TP 5.1 PHP ≥ 7.0
TP 6.0 PHP ≥ 7.1
TP 8.0 PHP ≥ 8.0

✅ 检查:

php -v

❌ 示例错误:

Parse error: syntax error, unexpected ...

2️⃣ 没有开启重写(伪静态)

访问:

http://ip/index.php
✅ 能访问
http://ip/
❌ 404

Ubuntu + Apache 解决:

sudo a2enmod rewrite
sudo systemctl restart apache2

并确认 .htaccess 存在(ThinkPHP 自带)


3️⃣ 目录权限问题(非常常见)

报错示例:

failed to open stream: Permission denied

解决:

sudo chown -R www-data:www-data /var/www/html
sudo chmod -R 755 /var/www/html
sudo chmod -R 777 runtime

(runtime 必须可写)


4️⃣ 没有安装必要 PHP 扩展

ThinkPHP 常用扩展:

sudo apt install php-mbstring php-xml php-curl php-mysql php-zip
sudo systemctl restart apache2

检查:

php -m | grep -E "mbstring|curl|xml"

5️⃣ 没有开启调试,看不到真实错误

.env 中:

APP_DEBUG = true

config/app.php

'app_debug' => true,

二、Nginx 用户特别注意

如果你是 Nginx + PHP-FPM,常常是因为:

❌ 伪静态没配

location / {
    try_files $uri $uri/ /index.php?$query_string;
}

❌ PHP 没跑起来

sudo systemctl status php8.1-fpm

三、快速定位错误的方法

  1. 看错误页面(开 debug)
  2. 看日志:
tail -f runtime/log/*.log
  1. 看 Apache / Nginx 错误日志:
tail -f /var/log/apache2/error.log
tail -f /var/log/nginx/error.log

四、你可以这样发给我,我直接帮你定位

请贴出:

  1. 完整报错信息
  2. ThinkPHP 版本
  3. Ubuntu + Apache 还是 Nginx
  4. 访问方式(IP / 域名 / 是否带 index.php)

我可以直接告诉你是哪一行配置错了

0