If you’re thinking about launching new applications this year, there are many good reasons to choose Heroku as your hosting platform. In addition to its ability to make deployment and environment configuration easy, Heroku provides a well-designed platform, tools, documentation, and an extensive add-on community.
Heroku’s superpower is managing your…
Laravel by default will save sessions into a directory on disk, which isn’t ideal, because Heroku uses an ephemeral filesystem (explained in Tip #5).
You have to use another session driver. Laravel ships with several great drivers out of the box:
In this example, we use Redis…
Laravel by default will log errors and messages into a directory on disk, which isn’t ideal, because Heroku uses an ephemeral filesystem, that means that any changes to the filesystem whilst the dyno is running only last until that dyno is shut down or restarted. Each dyno boots with a clean copy of the filesystem from the most recent deploy. This is similar to how many container-based systems, such as Docker, operate.
Change the log destination on Heroku:
heroku config:set LOG_CHANNEL=errorlog
Now you can tail the logs live:
heroku logs -t
That’s it. 🚀
Laravel has great documentation about deployment.
The most important part is optimization, which includes 3 parts:
On Heroku, we also need these 3 optimizations.
We have to optimize Composer’s class autoloader with the following command:
composer install --optimize-autoloader --no-dev
Heroku has the free Heroku Scheduler, but this has two disadvantages:
Laravel comes with an amazing Task Scheduler.
To run Laravel scheduled jobs on Heroku you have to add a new command and run that as a process.
Create the Command app/Console/Commands/SchedulerDaemon.php:
You can generate this file with:
php artisan make:command SchedulerDaemon
Add the following line to your Procfile:
Don’t forget to enable this process in the Heroku Dashboard:
On Heroku the release phase allows you to run certain tasks before a new release of your app is deployed.
Release phase can be useful for tasks such as:
On Heroku, you get environment variables for PostgreSQL, Redis, e.g.
You shouldn't add manually environment variables on Heroku for Database (like DB_HOST, DB_PORT, e.g.) and Redis (REDIS_HOST, e.g.).
Because you have to change these if you upgrade/downgrade these Heroku add-ons.
And more importantly, Heroku can change these environment variables at any time.
A better solution would be, add the following lines at the beginning of config/database.php