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:

    [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

Possible Errors

Error not enough volumes named `<volume_name>` (1) to run `(<n>)` processes

The above error can come up after configuring your volume in fly.toml and executing fly deploy.

It can mean that there are <n> processes configured in your fly.toml trying to use the volume! Take note however, that a Volume can only be used by one at any given time.

One way to fix this issue is to separate each process into different Fly.io apps. Of course, separate application per process might require inter-communication between the applications.

Fly.io applications can easily communicate with each other over a private network. Not only that, but Fly.io also offers the fly-replay response header which can be used to "redirect" request from one application to another, and return response from the correct application.