Persisting the Storage Folder

The storage folder by default, holds several "generated" essentials of your Laravel application.

It's the default burrow for session, cache, and file data amongst others. If you'd opt to persist data on this folder, you'll have to mount a volume to it.

The Steps

  1. Make sure you are in your Laravel Fly App's directory

    cd <laravel-fly-configured-app>
    
  2. Create a volume, you'll be attaching this later to your Laravel Fly App's storage directory

    fly volumes create storage_vol --region ams --size 20 
    
  3. Revise your Laravel Fly App's fly.toml file to mount the volume created for your storage directory, and connect with an sqlite database you'll be configuring further down below:

    [mounts]
    source="storage_vol"
    destination="/var/www/html/storage"
    
  4. To fix the little storage-content-erasure issue as stated in the callout above, please go ahead and make a copy of your storage folder in a "backup" folder. You can name this directory "storage_".

    cp -r storage storage_
    

    You'll later use this folder to copy over its contents to the volumized storage folder.

  5. Next create a Startup Script that will initialize the volumized storage folder's contents.

    touch .fly/scripts/1_storage_init.sh
    

    Side Note: Start up scripts are run in numeric-alphabetical order. Naming 1_storage_init.sh makes sure it is the first script run. Otherwise, naming the file as storage_init.sh alone would've moved the caches.sh script above it, and would've executed before storage initialization happened. One of the commands in the caches.sh will not have worked properly, due to a lack of properly initialized storage directory.

On to the content of the Start Up script:

FOLDER=/var/www/html/storage/app
if [ ! -d "$FOLDER" ]; then
    echo "$FOLDER is not a directory, copying storage_ content to storage"
    cp -r /var/www/html/storage_/. /var/www/html/storage
    echo "deleting storage_..."
    rm -rf /var/www/html/storage_
fi

So what happened above?

  • The condition statement checks if the app folder does not exist in the volumized storage folder. If it does not exist, it copies over the contents of the storage_ folder to the volumized storage folder.

Finally, deploy your Laravel Fly App!

fly deploy