EC2 Laravel Storage Writable
March 18, 2025 · AI Conversations
### **1. Check Ownership & Permissions**
Run the following commands to set the correct ownership and permissions for your Laravel `storage` and `bootstrap/cache` folders.
cd /var/www/share # Navigate to your Laravel project directory
# Set ownership to the web server user
sudo chown -R www-data:www-data storage bootstrap/cache
# Set proper permissions
sudo chmod -R 775 storage bootstrap/cache
### **2. Check Your Web Server User**
Verify which user is running Apache:
ps aux | grep apache
or
ps aux | grep httpd
If Apache is running under a different user (e.g., `ubuntu`), update the ownership:
sudo chown -R ubuntu:www-data storage bootstrap/cache
### **3. Verify SELinux (If Enabled)**
If your instance has SELinux enabled, update the security context:
sudo chcon -R -t httpd_sys_rw_content_t storage bootstrap/cache
### **4. Restart Apache**
After making the changes, restart Apache to apply them:
sudo systemctl restart apache2
### **5. Verify Writable Status**
Run the following command to test if the `storage` folder is writable:
sudo -u www-data touch storage/testfile
If no errors appear, it means the issue is resolved. Remove the test file after verification:
rm storage/testfile
### **6. Double-Check Laravel Configuration**
Ensure that your Laravel application recognizes the storage changes:
php artisan cache:clear
php artisan config:clear
php artisan storage:link
Now your `storage` folder should be writable. If the issue persists, let me know the specific error message you are getting. 🚀
—
## **1. Understanding Why It Happens**
– When you upload files via **SFTP/FTP** or **SSH**, the user performing the upload (e.g., `ubuntu`) becomes the file owner.
– Some deployment scripts may override permissions.
– If you've used `chmod -R 777`, it's **insecure**, and you should reset permissions properly.
—
## **2. Reset Correct Ownership & Permissions**
After transferring files, run the following to **reset ownership and permissions**:
### **A. Reset Ownership to Apache (www-data)**
sudo chown -R www-data:www-data /var/www/share/storage /var/www/share/bootstrap/cache
### **B. Secure Folder Permissions**
sudo chmod -R 775 /var/www/share/storage /var/www/share/bootstrap/cache
This ensures:
– **775**: Read & execute for the group, full access for the owner.
– **No 777**, making it more secure.
—
## **3. Automate Fix After File Transfer**
If file permissions keep resetting after file transfer, you can create an **automatic fix** using a script.
### **A. Create a Reset Script**
Create a script to **fix permissions after file transfer**:
“`
—
## **1. Change Permissions for File Transfer**
Before transferring files, you temporarily change the permissions to allow full access:
sudo chmod -R 777 /var/www/share/storage /var/www/share/bootstrap/cache
This ensures that the file transfer process does not run into permission issues.
—
## **2. Revert Permissions After Transfer**
Once the transfer is complete, revert the permissions to a secure state.
### **A. Reset Ownership to `www-data` (Apache)**
sudo chown -R www-data:www-data /var/www/share/storage /var/www/share/bootstrap/cache
### **B. Restore Secure Folder Permissions**
sudo chmod -R 775 /var/www/share/storage /var/www/share/bootstrap/cache
This will:
– Ensure Laravel can write to `storage` and `bootstrap/cache`.
– Keep it secure without `777` access.
—
## **3. Automate Reverting Permissions After Transfer**
To make this process easier, you can create a script.
### **A. Create a Shell Script**
Create a file named **`reset_permissions.sh`**:
sudo nano /var/www/share/reset_permissions.sh
Add the following content:
#!/bin/bash
# Revert permissions after file transfer
echo "Reverting file permissions..."
# Set correct ownership
sudo chown -R www-data:www-data /var/www/share/storage /var/www/share/bootstrap/cache
# Set secure permissions
sudo chmod -R 775 /var/www/share/storage /var/www/share/bootstrap/cache
echo "Permissions successfully reverted!"
Save the file (`CTRL + X`, then `Y`, then `Enter`).
—
### **B. Make the Script Executable**
Run the following command:
sudo chmod +x /var/www/share/reset_permissions.sh
—
### **C. Run the Script After File Transfer**
After transferring files, execute:
sudo /var/www/share/reset_permissions.sh
—
## **4. Automate Using Cron Job (Optional)**
If you frequently transfer files, you can **automate the permission reset** using a cron job.
Open the crontab editor:
crontab -e
Add this line at the end:
*/30 * * * * /bin/bash /var/www/share/reset_permissions.sh
This will automatically **reset permissions every 30 minutes**.
—
## **Conclusion**
– Use `chmod 777` **only for file transfer**.
– Run the script manually **after each transfer** or set up **cron job automation**.
– This ensures your Laravel storage remains writable but not **overexposed (777)**.
Let me know if you need modifications! 🚀