How to Change Local Storage Path in Laravel

Süleyman Özgür Özarpacı - Oct 2 - - Dev Community

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

As a precaution, in case anything goes wrong, rename the original storage folder:

mv storage backup_storage
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Application Configuration

At this stage, you have two options:

  1. Update without modifying the application by creating a symlink.
  2. 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
Enter fullscreen mode Exit fullscreen mode

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'));
Enter fullscreen mode Exit fullscreen mode

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
];
Enter fullscreen mode Exit fullscreen mode

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

Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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.

. . . . . . . . . . . . . .
Terabox Video Player