Using Laravel on Fly.io

I am a PHP/Laravel developer. I currently run my apps on a traditional cloud VPS provider but really want to get away from managing servers. I have not been able to find any guides on your site or even external blog posts about getting Laravel running on Fly.

Doing some digging I found this post about multi process apps so it looks like that would be an easy way to run queue workers by defining a worker process and having it run Laraverl’s queue worker command php artisan queue:work

I am not 100% sure what to do for the command for the web process. Would I need to start nginx or something?

I am also not sure what to do for scheduled tasks. It seems a year ago you said cron tasks were coming in a few months But that was almost exactly a year ago and I have not seen any more recent updates on that. For Laravel specifically we would normally schedule a cron to run php artisan schedule:run every minute and the Laravel code would handle the rest.

I also can’t seem to find a good source for how to scale processes. Like if my queue is backing up to add more workers or if I am getting alot of web requests to scale up more web processes.

Hi,

Yep, it’s not quite as simple as running e.g a Node app because PHP doesn’t include its own web server. You need to run Apache/Nginx in front to proxy requests to it. And so to run both in a single app/vm, you need a supervisor (or equivalent) process to keep both PHP and Nginx running. But it’s absolutely do-able.

Take a look at GitHub - fly-apps/fly-hello-laravel: Sample Laravel app for deployment on Fly.io

That’s the demo Laravel app with its default home page (the example app the Laravel CLI makes). But the changes made to that app (and so the changes needed to deploy your own app) are listed too, from this bit onwards GitHub - fly-apps/fly-hello-laravel: Sample Laravel app for deployment on Fly.io

It uses pretty default settings e.g for PHP conf files and so those can be modified as needed. Hopefully the repo goes into sufficient detail on that.

You make a good point about workers. Indeed, Fly doesn’t provide a cron-as-a-service. There is a brief reference to how that might be done within that repo. I didn’t get as far as actually trying it though :slight_smile: Hence the blocks are commented out. But in theory you should be able to get supervisor to run child workers within the same app to run workers to process the queue behind the scenes. I’d strongly recommend experimenting with that on a demo app first. You don’t want the workers to go sending out alerts or whatever it is yours do unexpectedly. But if you look from this line onwards it should be reasonably clear by the task name what it would do so you can uncomment each block as needed: fly-hello-laravel/supervisor.conf at main · fly-apps/fly-hello-laravel · GitHub

And then you’d adjust the tasks the supervisor runs to match that. Currently you’ll see it is running just the nginx and php-fpm tasks (as mentioned above). This line: fly-hello-laravel/supervisor.conf at main · fly-apps/fly-hello-laravel · GitHub

Hopefully that should get you started.

There are certainly other approaches to doing it, like using a base image that includes nginx+php already. But I figured using an empty base OS gives total freedom to install whatever is needed and do other stuff. Like if you want to install Redis, or do other things.

We’d love to offer scheduled tasks. We haven’t done it yet beause we’re focusing first on parts of the platform that users can’t manage themselves, but that affect everyone, like stability, performance and support for regional deployments.

Meanwhile, here’s another example on how to use supercronic to run jobs on your Fly VM. By cobbling this together with the example posted by @greg, we could make this work. Happy to help!

Side note: We’ve just hired a Laravel expert to focus full-time on making Laravel great on Fly :slight_smile:

Further to that from @jsierles, until Fly do provide scheduled tasks, there are other managed schedulers. Like Cloud Scheduler  |  Google Cloud . I’ve used that to initiate http requests to my apps. Seems to work pretty well.

Here’s the correct link: GitHub - fly-apps/supercronic: Run periodic jobs on Fly with supercronic

Thanks for the suggestion @jsierles, I successfully set up supercronic with supervisord and Laravel, so the scheduler seems to run fine!