Laravel文件缓存:能否配置权限掩码使新缓存文件以664权限创建?
Got it, let's fix this permission issue you're facing with Laravel's cache files. You absolutely can configure the framework to generate new cache files with a 664 permission mask instead of the default 644—here's how to do it properly:
Step 1: Update Your Cache Configuration
Open your config/cache.php file, find the file driver section (since you're using file-based caching), and add a new permission key set to 0664.
Your modified configuration block should look like this:
'file' => [ 'driver' => 'file', 'path' => storage_path('framework/cache/data'), 'permission' => 0664, // Add this line to set the desired permission mask ],
Important: Don't skip the leading 0 before 664—this tells PHP to treat the number as an octal value, which is required for correct permission handling. Omitting it will result in wrong permissions being applied.
Step 2: Fix Existing Cache Files
The new permission setting only affects newly created cache files. To fix the existing 644 permission files that your shell user can't delete, run this command in your project root:
chmod -R g+w storage/framework/cache/data
This grants write access to the www-data group (which your shell user belongs to) for all existing cache files and directories.
A Quick Breakdown of 664 Permissions
This permission mask gives exactly what you need:
- Owner (typically the web server's
www-datauser): Read and write access - Group (your shell user is part of this): Read and write access
- Others: Read-only access
This setup lets both the web server process and your shell user manage cache files without permission conflicts.
内容的提问来源于stack exchange,提问作者realtebo

