在 Ubuntu 上运行 Laravel 常见的报错有很多,下面我按最常见问题 → 原因 → 解决办法给你一个排查清单。你可以对照看看是哪一种。
Permission denied(权限问题,最常见)报错示例:
The stream or file "/var/www/laravel/storage/logs/laravel.log" could not be opened
原因: storage 或 bootstrap/cache 目录没有写权限
解决:
cd /var/www/你的项目
sudo chown -R www-data:www-data storage bootstrap/cache
sudo chmod -R 775 storage bootstrap/cache
如果你用 CLI 运行:
sudo chmod -R 777 storage
PHP Version 不满足报错:
Your requirements could not be resolved to an installable set of packages.
原因: Laravel 版本要求 PHP 版本过高
解决: 查看 PHP 版本:
php -v
安装高版本 PHP(如 8.2):
sudo add-apt-repository ppa:ondrej/php
sudo apt update
sudo apt install php8.2 php8.2-cli php8.2-fpm php8.2-mysql php8.2-mbstring php8.2-xml php8.2-curl
Composer install 报错常见错误:
ext-xxx missingmemory limit解决:
composer install --ignore-platform-reqs
或提高内存:
php -d memory_limit=-1 composer install
.env 不存在报错:
No application encryption key has been specified.
解决:
cp .env.example .env
php artisan key:generate
报错:
SQLSTATE[HY000] [2002] Connection refused
原因:
解决:
sudo systemctl start mysql
sudo systemctl enable mysql
检查 .env:
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel
DB_USERNAME=root
DB_PASSWORD=
原因: 没配置 rewrite
Nginx 示例:
location / {
try_files $uri $uri/ /index.php?$query_string;
}
storage/logs 无法写入(CLI 和 Web 用户不同)sudo chown -R $USER:www-data storage
sudo chmod -R 775 storage
php artisan config:clear
php artisan cache:clear
php artisan route:clear
php artisan view:clear
然后:
tail -f storage/logs/laravel.log
你可以直接把完整报错信息发给我,例如:
我可以直接告诉你具体是哪一行出问题 + 怎么改。