Laravel部署cPanel遇权限错误:file_put_contents无法打开流求助
file_put_contents() Permission Denied Error on cPanel Hey there, I’ve run into this exact issue a bunch of times with Laravel on cPanel—setting folders to 777 actually backfires more often than not (it’s a huge security risk too!), so let’s walk through the proper, safe fixes:
First, Roll Back the Unsafe 777 Permissions
777 gives full access to everyone, and many cPanel servers have security measures (like suPHP or mod_security) that block these permissions entirely. Let’s reset to secure, functional defaults first:
# Fix file permissions find storage bootstrap/cache -type f -exec chmod 644 {} \; # Fix directory permissions find storage bootstrap/cache -type d -exec chmod 755 {} \;
Set the Correct File Owner
On cPanel, the web server process (Apache/Nginx) typically runs under your cPanel username (not nobody or apache like on some standalone servers). Update the ownership of your Laravel project to match:
chown -R your_cpanel_username:your_cpanel_username /home/your_cpanel_username/public_html/your_laravel_project
Make sure to replace your_cpanel_username and the project path with your actual details.
Adjust SELinux Context (If Applicable)
If your server uses SELinux (common on CentOS-based cPanel setups), you’ll need to mark the storage and bootstrap/cache directories as writable by the web server:
chcon -R -t httpd_sys_rw_content_t storage bootstrap/cache
Check Your PHP Handler in cPanel
Sometimes the issue comes down to how PHP is configured in cPanel:
- Head to Select PHP Version in your cPanel dashboard.
- Try switching your PHP handler (e.g., from CGI to SuPHP or LSAPI). Different handlers have different permission rules, so this can resolve access issues.
Clear Laravel’s Caches
Finally, clear out any cached data that might be holding onto old permission references:
php artisan cache:clear php artisan config:clear php artisan view:clear
If you still run into issues, double-check that your public_html directory has the correct permissions (usually 755) and that your Laravel project isn’t outside the cPanel user’s home directory (some servers restrict this).
内容的提问来源于stack exchange,提问作者Eleg Uni

