Deployment
Getting your Leaf app live should be as simple as building it. Whether you're deploying to shared hosting, VPS, or platforms like DigitalOcean and Vercel, Leaf makes the process smooth and hassle-free. This guide walks you through setting up your server, configuring URL rewriting, and making sure your app runs efficiently in production.
Production Checklist
Before deploying your app, make sure you’ve covered the following:
- Environment Variables: Set up your environment variables for production.
- Debug Mode: Turn off debug mode and disable Leaf DevTools.
These are meant to ensure your app runs smoothly in production, without exposing sensitive information or running unnecessary debugging tools.
URL Rewriting
URL rewriting maps all requests to a single entry point—usually index.php
—so Leaf’s router can handle them dynamically. Instead of serving files directly, web servers like Apache and Nginx can be configured to route all traffic through your app, so Leaf can handle requests cleanly and efficiently.
try_files $uri /index.php?$query_string;
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . index.php [L]
Without this, things like routing, request handling, and error pages won’t work as expected. Make sure to set up URL rewriting correctly on your server to ensure your Leaf app runs smoothly.
Deployment Guides
Okay, now let’s get your app live! 🚀
Provider | Description |
---|---|
Digital Ocean | Deploying LeafMVC projects to a new Digital Ocean droplet |
Heroku | Deploying a base Leaf project to Heroku using the Leaf CLI |
Fly.io | Deploying a base Leaf application to Fly.io |
Deploying Queues/Workers
When deploying your application with queues, Leaf takes care of setting up the necessary files and commands based on your chosen queue driver. However, once deployed, you’ll need to set up your server to keep your workers running continuously.
For smaller applications, you can keep the queue worker running in the background with:
php leaf queue:work &
This command will set up your queue and start a worker to process jobs. Leaf includes safeguards to prevent excessive memory usage, long-running processes, or crashes from failed jobs. However, for larger applications, this setup may not be enough. In such cases, using a process manager like Supervisor is recommended to ensure your workers run smoothly and restart automatically if needed:
sudo apt update && sudo apt install supervisor -y
Create a new configuration file for your Leaf queue worker:
sudo nano /etc/supervisor/conf.d/leaf-queue.conf
Add your Supervisor configuration:
[program:leaf-queue]
process_name=%(program_name)s_%(process_num)02d
command=php leaf queue:work
autostart=true
autorestart=true
numprocs=1
redirect_stderr=true
stdout_logfile=/var/log/leaf-queue.log
Save and exit, then update Supervisor and start the worker:
sudo supervisorctl reread
sudo supervisorctl update
sudo supervisorctl start leaf-queue
This will start a worker that will process jobs in the queue. You can check the status of the worker using the following command:
sudo supervisorctl status leaf-queue
And that's it! Your worker is now running and processing jobs in the queue.