在 Ubuntu 上运行 ThinkPHP 报错,原因通常和环境配置、权限、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 ...
访问:
http://ip/index.php
✅ 能访问
http://ip/
❌ 404
Ubuntu + Apache 解决:
sudo a2enmod rewrite
sudo systemctl restart apache2
并确认 .htaccess 存在(ThinkPHP 自带)
报错示例:
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 必须可写)
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"
在 .env 中:
APP_DEBUG = true
或 config/app.php:
'app_debug' => true,
如果你是 Nginx + PHP-FPM,常常是因为:
location / {
try_files $uri $uri/ /index.php?$query_string;
}
sudo systemctl status php8.1-fpm
tail -f runtime/log/*.log
tail -f /var/log/apache2/error.log
tail -f /var/log/nginx/error.log
请贴出:
我可以直接告诉你是哪一行配置错了。