在 Linux 上 Laravel 无法访问(打不开页面 / 403 / 404 / 500)通常和权限、Web 服务器配置、路由或环境有关。下面按最常见原因逐一排查,你可以对照看是哪一种。
cd /var/www/你的项目
sudo chown -R www-data:www-data storage bootstrap/cache
sudo chmod -R 775 storage bootstrap/cache
Apache 用户一般是
www-data
Nginx + PHP-FPM 也可能是nginx或php-fpm
sudo a2enmod rewrite
sudo systemctl restart apache2
必须指向 public 目录
DocumentRoot /var/www/项目/public
<Directory /var/www/项目/public>
AllowOverride All
Require all granted
</Directory>
然后:
sudo systemctl restart apache2
示例:
server {
root /var/www/项目/public;
index index.php index.html;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php8.2-fpm.sock;
}
}
改完:
sudo nginx -t
sudo systemctl restart nginx
cp .env.example .env
php artisan key:generate
php artisan config:clear
php artisan route:clear
php artisan cache:clear
php -v
Laravel 10+ 需要 PHP 8.1+
sudo ufw allow 80
sudo ufw allow 443
你可以按顺序看:
http://IP/项目/publicpublic请告诉我:
/var/www/laravel)我可以直接给你一份可用配置文件。