Laravel on Fly.io

Getting an application running on Fly.io is essentially working out how to package it as a deployable image. Once packaged, it can be deployed to the Fly.io platform.

In this guide we’ll learn how to deploy a Laravel application on Fly.io.

For an overview of how to configure your application with services like caching, databases, and queues, reading through our Full Stack Laravel post is the best place to start.

Prepare a Laravel app

Bring your own Laravel app, or create a new one!

If you want to start fresh, here’s how to set up a new application. You’ll need PHP 8+ and composer installed locally. You can check your PHP version using php --version.

composer create-project laravel/laravel fly-laravel
cd fly-laravel
php artisan serve

You should be able to visit http://localhost:8000 and see the home page.

Deploy to Fly.io

Install Fly

First, install flyctl, the Fly.io CLI, and sign up to Fly.io if you haven’t already.

Launch

Next, we’ll use the launch command to automagically configure your app for Fly.

The launch command adds a few files to your code base. Don’t worry, it will ask before overwriting anything.

If you haven’t already, go ahead and run fly launch!

fly launch

When asked if you want to deploy now, say No.

If you have other environment variables to set, you can edit the fly.toml file and add them.

[env]
# Set any env vars you want here
# Caution: Don't add secrets here
APP_URL = "https://fly-hello-laravel.fly.dev"

Replace this with the URL your app will be served on (by default, "https://<your-app-name>.fly.dev").

For sensitive data, you can set secrets with the fly secrets set command:

fly secrets set SOME_SECRET_KEY=<the-value-from-your-env-file>

The fly launch command will generate a secret with a valid, random value for APP_KEY.

Deploy

Finally, run fly deploy to build and deploy your application!

You should be able to visit https://your-app-name.fly.dev and see the Laravel demo home page.

That’s it! Run fly apps open to see your deployed app in action.

Try a few other commands:

Some Notes

The fly launch adds some files to your code base.

Here is what gets added:

  1. Dockerfile - Used to build a container image that is run in fly
  2. .dockerignore - Used to ensure certain files don’t make its way into your repository
  3. fly.toml - Configuration specific to hosting on Fly
  4. .fly (formerly docker) - A directory containing configuration files for running Nginx/PHP in a container

Running fly launch (and later fly deploy) uses the Dockerfile to build a container image, copying your application files into the resulting image.

Fly doesn’t care about the state of your git repository - it copies whatever files are present (except for files ignored by .dockerignore).