How to deploy a Laravel App to Heroku
Last updated: 2. December 2022
This article describes how to setup a Laravel web application on Heroku with database, e-mail and GitHub integration. As a result, your web app will be available online, with automatic updates whenever a change is made to your code.
Laravel is a web application framework with expressive, elegant syntax for the PHP programming language.
Heroku is a platform as a service (PaaS) that enables developers to build, run, and operate applications entirely in the cloud.
Prerequisites
- The code of your Laravel application should be stored in a Git repository.
- Heroku uses PostgreSQL as database server, so your database queries must be database-agnostic or compatible with PostgreSQL.
- Make sure Git is installed locally.
- Make sure Heroku CLI is installed locally.
Application setup
Create a file named Procfile
in the root folder of your web application, with the following content:
web: vendor/bin/heroku-php-apache2 public/
worker: php artisan migrate --force
and add the file to your version control system:
git add Procfile
git commit -m "Added Procfile for Heroku"
Create an app on Heroku:
heroku create --region eu <APPNAME>
This will also create a Git repository at Heroku, and automatically add a new remote heroku
to your local git repository.
Set application key as Heroku environment variable:
php artisan key:generate --show
heroku config:set APP_KEY=...
Configure logging:
heroku config:set LOG_CHANNEL=errorlog
Configure load-balancer / proxy settings so Larael receives the correct IP addresses of clienst. Change the middleware App\Http\Middleware\TrustProxies
so that it looks like this:
<?php
namespace App\Http\Middleware;
use Illuminate\Http\Request;
use Fideloper\Proxy\TrustProxies as Middleware;
class TrustProxies extends Middleware
{
protected $proxies = '*';
protected $headers = Request::HEADER_X_FORWARDED_AWS_ELB;
}
Save and commit your changes.
Push your code to the Heroku git repository:
git push heroku HEAD:master
Database configuration
Add a database (PostgreSQL) (here we use the free hobby-dev
plan, but other plans are available too):
heroku addons:create heroku-postgresql:hobby-dev
heroku config:set DB_CONNECTION=pgsql
Run database migrations, if needed:
heroku run php artisan migrate --force
The application will run per default in production
mode, and no development dependencies will be installed. Therefore, database seeders with model factories cannot be run. If you have seeders for production environments, you can run them with:
heroku run php artisan db:seed
Mail delivery
To setup e-mail delivery, a service like Mailgun can be used.
To send e-mails via Mailgun, create a Mailgun account, register a domain and note the API Key and the domain name, Configure the following settings in Heroku:
heroku config:set MAIL_MAILER=mailgun
heroku config:set MAILGUN_DOMAIN=...
heroku config:set MAILGUN_SECRET=...
View application
Open your application in the browser:
heroku open
Observe your application logs in case you run into errors:
heroku logs --tail
If the application shows a 404 error, you might have to set the app URL manually:
heroku config:set APP_URL=...
Configure Github integration
You can configure GitHub integration in the Deploy tab of apps in the Heroku Dashboard. You need to authenticate with Github, and can then enable automatic deploys. This will update your application on Heroku whenever changes are pushed to your Github repository.
Custom domains
You can assign a custom domain to your app with the following CLI command:
heroku domains:add www.example.com
The output of the command will tell you what settings need to be added to your DNS server.