在 Debian 下运行 Laravel 报错,原因非常多,不能一概而论。下面我按 最常见 → 较少见 的顺序,帮你系统排查。你可以对照看看,也可以把具体报错信息贴出来,我可以精准定位。
Laravel 对 PHP 版本要求很严格:
| Laravel 版本 | PHP 最低版本 |
|---|---|
| 8.x | PHP 8.0+ |
| 9.x | PHP 8.0+ |
| 10.x | PHP 8.1+ |
| 11.x | PHP 8.2+ |
✅ 检查 PHP 版本:
php -v
❌ 如果版本太低,Debian 默认仓库可能不够新,需要:
sudo apt install software-properties-common
sudo add-apt-repository ppa:ondrej/php
sudo apt update
sudo apt install php8.2 php8.2-cli php8.2-common php8.2-mbstring php8.2-xml php8.2-curl php8.2-zip php8.2-mysql
Laravel 必装扩展:
php-mbstring
php-xml
php-curl
php-zip
php-tokenizer
php-fileinfo
✅ 安装:
sudo apt install php-mbstring php-xml php-curl php-zip
典型报错:
The stream or file could not be opened
✅ 解决:
sudo chown -R www-data:www-data /var/www/laravel-project
sudo chmod -R 755 /var/www/laravel-project
sudo chmod -R 777 storage bootstrap/cache
⚠️ 生产环境不建议 777,可改为:
sudo setfacl -R -m u:www-data:rwx storage bootstrap/cache
报错示例:
No application encryption key has been specified
✅ 解决:
cp .env.example .env
php artisan key:generate
✅ 重新安装依赖:
composer install --no-dev --optimize-autoloader
❌ 如果报错:
composer install --ignore-platform-reqs
报错:
404 Not Found
✅ 解决:
sudo a2enmod rewrite
sudo systemctl restart apache2
并确认 .htaccess 存在。
典型错误:
404 / index.php 不生效
✅ 正确示例:
location / {
try_files $uri $uri/ /index.php?$query_string;
}
报错:
SQLSTATE[HY000] [2002] Connection refused
✅ 检查:
sudo systemctl status mysql
.env 中:
DB_HOST=127.0.0.1
DB_PORT=3306
sudo systemctl status php8.2-fpm
sudo systemctl start php8.2-fpm
tail -f storage/logs/laravel.log
journalctl -xe
APP_DEBUG=true
请直接贴出:
1️⃣ 完整报错信息(文字或截图)
2️⃣ Laravel 版本
3️⃣ Debian 版本(cat /etc/debian_version)
4️⃣ PHP 版本
5️⃣ Web 服务器(Apache / Nginx)
✅ 如果你愿意,我可以 一步一步带你从 0 在 Debian 上跑起 Laravel。