在 Debian 下运行 Laravel 常见报错很多,需要先看具体报错信息才能精确定位。下面我先给你一份最常见问题 + 对应解决办法,你可以对照看看。
Laravel 对 PHP 版本有要求(如 Laravel 10 需要 PHP 8.1+)
php -v
解决:
sudo apt install php8.1 php8.1-cli php8.1-fpm php8.1-mysql php8.1-xml php8.1-mbstring php8.1-curl php8.1-zip
常见报错:
Class 'XXX' not found
安装常用扩展:
sudo apt install php-mysql php-xml php-mbstring php-curl php-zip php-gd php-bcmath
然后:
sudo systemctl restart php8.1-fpm
composer -v
安装 / 升级:
sudo apt install composer
或官方方式:
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
php composer-setup.php
sudo mv composer.phar /usr/local/bin/composer
Permission denied(storage / bootstrap/cache)报错示例:
The stream or file "/var/www/laravel/storage/logs/laravel.log" could not be opened
解决:
sudo chown -R www-data:www-data /var/www/laravel
sudo chmod -R 755 /var/www/laravel
sudo chmod -R 777 storage bootstrap/cache
APP_KEY 不存在报错:
No application encryption key has been specified.
解决:
cp .env.example .env
php artisan key:generate
报错:
SQLSTATE[HY000] [2002] Connection refused
检查:
sudo systemctl status mysql
.env 配置是否正确DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel
DB_USERNAME=root
DB_PASSWORD=
Nginx 示例:
location / {
try_files $uri $uri/ /index.php?$query_string;
}
php artisan config:clear
php artisan route:clear
php artisan cache:clear
php artisan view:clear
php artisan --version
php artisan serve
tail -f storage/logs/laravel.log
请把下面信息发我:
cat /etc/os-release)php artisan --version)我可以直接告诉你具体是哪一行配置或权限错了。