在 Linux 服务器上部署 Laravel 项目时,权限设置是非常关键的一步,否则容易出现 500 错误、日志无法写入、缓存无法生成 等问题。
下面按最常见、最安全的方式来说明。
Laravel 运行时需要写权限的目录主要有:
storage
bootstrap/cache
具体包括:
storage/appstorage/frameworkstorage/logsbootstrap/cache通常你的环境是这样的:
www-datawww-datadeploy 或 ubuntu✅ 最佳实践:你和 Web 服务器属于同一个用户组
cd /var/www/your-laravel-project
假设 Web 用户是 www-data,你的用户是 deploy:
sudo chown -R deploy:www-data .
sudo find . -type d -exec chmod 755 {} \;
sudo find . -type f -exec chmod 644 {} \;
sudo chgrp -R www-data storage bootstrap/cache
sudo chmod -R 775 storage bootstrap/cache
确认 PHP-FPM 运行用户:
ps aux | grep php-fpm
一般是:
www-data
确保:
sudo chown -R www-data:www-data storage bootstrap/cache
⚠️ 仅用于本地测试:
chmod -R 777 storage
chmod -R 777 bootstrap/cache
❌ 生产环境绝对不要这样做
如果是 CentOS 并启用了 SELinux:
sudo setsebool -P httpd_read_user_content 1
sudo setsebool -P httpd_unified 1
或:
sudo chcon -R -t httpd_sys_rw_content_t storage
composer install
php artisan config:cache
php artisan route:cache
php artisan view:cache
storage 可写bootstrap/cache 可写php artisan 能正常运行如果你愿意,可以告诉我:
我可以给你更精确的命令。