在 Ubuntu 上使用 ThinkPHP 报错的原因有很多,下面我按最常见情况给你系统梳理一下,并告诉你怎么快速定位。
ThinkPHP 需要写日志、缓存、runtime 目录。
典型报错:
failed to open stream: Permission deniedmkdir(): Permission denied解决办法:
sudo chown -R www-data:www-data /var/www/your_project
sudo chmod -R 755 /var/www/your_project
sudo chmod -R 777 /var/www/your_project/runtime
⚠️ 生产环境不要把所有目录设 777,仅 runtime / storage 需要写权限
典型现象:
No input file specified确保开启:
sudo a2enmod rewrite
sudo systemctl restart apache2
项目根目录 .htaccess 存在(ThinkPHP 默认有)
配置示例:
location / {
try_files $uri $uri/ /index.php?s=$uri;
}
ThinkPHP 对 PHP 版本有要求:
| ThinkPHP | PHP |
|---|---|
| TP 5.1 | ≥ 7.1 |
| TP 6.0 | ≥ 7.2 |
| TP 8.0 | ≥ 8.0 |
检查:
php -v
常见缺扩展报错:
Class 'mysqli' not foundCall to undefined function mb_strlen()安装常用扩展:
sudo apt install php-mysql php-mbstring php-xml php-curl php-zip
sudo systemctl restart apache2
报错示例:
Class 'Think\App' not found解决:
cd /var/www/your_project
composer install
ThinkPHP 默认:
开启调试(开发时):
// .env
APP_DEBUG = true
cat runtime/log/2026xxxxx.log
# Apache
tail -f /var/log/apache2/error.log
# Nginx + PHP
tail -f /var/log/nginx/error.log
tail -f /var/log/php8.x-fpm.log
如果你愿意,可以把 完整报错信息(或截图文字)发给我,例如:
我可以直接帮你 精准定位并给命令解决 ✅