Craft 4 is a PHP application based on Yii2, and is installed via Composer.
This means that it’s well-suited for deployment to cloud-based platforms like Heroku. In this case, we’ve put together a barebones starter-kit for running a Craft application on Heroku.
There are a couple of limitations, here, that are important to note if it’s your first time using Heroku:
- The filesystem is ephemeral. This means that anything you store on your running dynos is considered disposable—so, if you want to persist data (like user uploads), you’ll have to use a cloud-based storage service like Amazon S3 or Google Cloud Storage. This also applies to Craft-specific features like Project Config, which relies on YAML files written to disk.
- Assume the app will scale. Design your formation and configuration assuming that you’ll (eventually) be running more than a single process. Your database, sessions, and shared caches should all be kept in a common location—but Heroku will help with this through “Addons,” configured with this
app.json
. - Logs are streams. Any logging that ends up in a file is as good as gone. There might be some clever workarounds here, but since the purpose of this repo is to demonstrate compatibility with first-party Heroku tools, Craft is configured to log to
php://stdout
andphp://stderr
, so the logs can be “Drained” anywhere you like.
Read more about 12-factor app architecture, the underlying principles of the Heroku platform.
It’s also worth mentioning: The traditional “queue” is handled here by a worker
dyno. In order to run the app locally, you’ll need to spin up a queue daemon to ensure your jobs are taken care of:
php craft queue/listen --verbose
☝️ This is the same worker command you see in the Procfile
in the project's root! It’ll get booted alongside your web
dyno.
Just click this button to set up your first app!
This script does a few things:
- Creates a new “app” in your Heroku account;
- Provisions addons (Redis and Postgres);
- Helps set up required Config Vars;
- Performs an initial build, including the installation of Composer dependencies;
- Configures “dyno” formation (with two
web
processes and oneworker
); - Redirects you to the Craft 4 installation screen. 🎊
You might encounter some visual issues with the installation process—this is because the
cpresources
are published into the public web root on a dyno-by-dyno basis, and the initial request for the installation page is not apt to have been hit on both servers. Subsequent requests for those assets could still be served by another dyno. Refresh the page a few times, and both dynos will begin to warm theircpresources
.
But what about the road to production? This repo is designed to work seamlessly with Craft’s recommended development environment, DDEV.
Install DDEV, clone the project, and run $ ddev start
. For more information, please refer to the official Craft installation instructions.
🌳