In Laravel, there may be instances where you need to change your storage path to accommodate specific requirements. For example, in my case, I encountered an issue with insufficient disk space for my projects. To resolve this, I purchased a DigitalOcean Volume to expand my storage capacity. In this article, I will demonstrate two methods to modify your storage path effectively.
Creating the Volume
To begin, log in to your DigitalOcean account and navigate to the Droplets section. Select your droplet, then go to the Volumes tab. From there, create a new volume, specifying the size according to your storage requirements.
Server Configuration
Since I am using Laravel Forge, I need to connect to the server via SSH to verify that the volume has been successfully attached to my droplet. Use the following command:
ssh forge@[YOUR_IP_ADDRESS]
lsblk
Find the disk you added from the list. In our setup, the disk appears as volume_fra1_01
, and its mount path is /mnt/volume_fra1_01
.
Next, create a storage folder for your application within the new volume:
sudo mkdir /mnt/volume_fra1_01/[app_name]_storage
If you already have an existing storage folder, copy its contents to the newly created folder:
sudo cp -r /var/www/your_laravel_project/storage /mnt/volume_fra1_01/[app_name]_storage
As a precaution, in case anything goes wrong, rename the original storage folder:
mv storage backup_storage
Since the newly created folder resides under the root user, update its permissions to those recommended by Laravel, and change the owner to forge:
sudo chown -R forge:forge /mnt/volume_fra1_01/[app_name]_storage
sudo chmod -R 755 /mnt/volume_fra1_01/[app_name]_storage
Application Configuration
At this stage, you have two options:
- Update without modifying the application by creating a symlink.
- Change the storage path within the application itself.
Option 1: Using a Symlink (The Most Stable Method)
Navigate to the root directory of your application and create a symlink for the new storage directory:
cd [your_laravel_project_path]
ln -s /mnt/volume_fra1_01/[app_name]_storage/ storage
Option 2: Modifying the Application
To update the storage path within the application, add the following code to the boot method of your AppServiceProvider.php
file:
$this->app->useStoragePath(config('app.app_storage_path'));
Then, add this line to your app.php
configuration file:
]
// Your other configurations
'app_storage_path' => env('APP_STORAGE_PATH', base_path().'/storage'), // Add this line
];
One of the most common mistakes made during this process is directly using the env()
method in the application without adding the corresponding value to app.php
. If you do this, when you run config:cache
, the env()
method will always return null
, causing your application to malfunction. Therefore, I highly recommend that for every value you add to the .env
file, you also define a corresponding entry in a configuration file to ensure proper functionality.
If you've added the above code, you can now dynamically change your application's storage path by adding the APP_STORAGE_PATH
value to your .env
file. If you don't add this value, the application will default to using the storage folder in your root directory.
APP_STORAGE_PATH="/mnt/volume_fra1_01/[app_name]_storage" # <-- Use this value to change your storage path
Note: Option 2 may cause errors depending on the packages, code structure, or features you use in your application. If you choose this method, I strongly recommend running your tests and manually checking your application afterward.
Conclusion
To finalize, ensure that you clear all cached data by running:
php artisan optimize:clear
By following these steps, you now have the flexibility to update your storage path as needed, allowing your application to leverage any desired disk for file storage. This method provides enhanced adaptability and scalability, ensuring your Laravel application can continue to grow alongside your storage requirements.
Top comments (0)