Laravel and Redis Fly App
You can set up your own Redis Fly App either through Fly.io’s Redis Docker Image, or through Redis’ Official Docker Image.Fly.io Redis Docker Image
-
First create a new directory and initialize it with
fly launch, using Fly.io’s Redis Image for the build:- Pull the flyio/redis:6.2.6 image from the Docker repository through
--imageargument. - You can specify your Redis Fly App name through the
--nameargument - You can specify your region code through the
--regionattribute - You can opt not to immediately deploy by adding
--no-deployargument
- Pull the flyio/redis:6.2.6 image from the Docker repository through
-
Afterwards, add a volume that will persist your Redis data
- You can specify any volume name to replace redis_server above
- You can specify your preferred volume size in GB through the
--sizeargument
-
Then, attach your volume to your Redis Fly App by revising its
fly.tomlto include[[mounts]]:- Under
[[mounts]]attach the volume created in step #2, make sure that the name specified for the created volume is exactly the same string specified for itssourceattribute
- Under
-
Next, set up the Redis Fly App password through
fly secrets -
Finally, deploy your Redis Fly App!
Redis Official Docker Image
If you would like to use Redis’ official docker image to set up your Redis Fly App, the steps to get up and running are almost identical to the previous guide. There is just one minor revision on the image pulled and an additional step to set the Redis Fly App’s password.-
Follow the steps above, with a revision to the value passed for
--image- Pull the redis image from the Docker repository through the
--imageargument.
- Pull the redis image from the Docker repository through the
-
Before deploying, make sure to revise your
fly.tomlfile to specify your Redis Fly App’s password. Add the[processes]section for the defaultappprocess and add the following commands:- Use the
fly secretREDIS_PASSWORDyou’ve configured earlier - If this passphrase is not set and your Laravel configuration sends a REDIS_PASSWORD during its connection to your Redis Fly App, watch out! You’ll get the infamous “ERR Client sent AUTH, but no password is set” error
- Use the
Connect from a Laravel Fly App
Now that you have your Redis Fly App running, the next step is to connect it with your Laravel Fly App! Follow the steps below:- First, retrieve your Redis Fly App’s Fly .internal Address.
-
Next, make sure you move to your Laravel Fly App’s directory for better navigation:
-
Then revise your Laravel Fly App’s
fly.toml[env]configuration with the retrieved Fly .internal Address: -
Make sure you set your Laravel Fly App’s
REDIS_PASSWORDthrough thefly secretscommand: -
Finally, deploy!
Connect from a local environment
The simplest way to connect your local Laravel application to a Redis Fly App is to usefly proxy:
-
Open your Redis Fly App’s local directory
-
Then run the
fly proxyfor port 6379 or whichever REDIS_PORT you have configured: -
Next, revise your local Laravel application’s .env file to update your Redis connection:
Make sure the REDIS_PORT in your .env configuration matches the port used in
fly proxyThat’s it! Your local Laravel application should now be connected with your Redis Fly App.
Laravel with Upstash Managed Redis Fly App
Want a fully-managed Redis Fly App? Try Upstash Redis! To set up, you can follow our-in-depth guide here Once you’ve configured the necessary details through the flyctl prompts, and the app deployment completes, you should get a summary of the Redis cluster you’ve just deployed, like so:Connect From a Laravel Fly App
To test whether your Laravel Fly App can successfully connect with your Upstash Redis database, you can use your Upstash Redis Fly App as your Cache driver, then perform a quick cache:clear command.-
Revise the
[env]configuration in your Laravel Fly App’sfly.tomlfile:- Set the Laravel cache driver to redis
- Using
REDIS_URLshould overrideREDIS_HOST, add here theREDIS_URLreceived duringfly redis create - Upstash only supports “database 0”, so update your
REDIS_CACHE_DBconfig to 0. Otherwise, you will receive an error :SELECT failed: ERR Only 0th database is supported! Selected DB: 1...
-
Then deploy your
fly.tomlchanges:
Testing Connection
In all examples above, Redis was used as our Laravel application’s Cache Driver. To test if this connection is working or not, a simplephp artisan cache:clear should let you know.