Deploy a Laravel 5 app to Heroku

Heroku is an awesome platform originally built on top of AWS and currently owned by Salesforce. Laravel is a powerful PHP web framework for building applications.

In this short tutorial we’re going to generate an app from laravel-5-boilerplate and deploy it to Heroku.

By the end of the tutorial we will have a user management and permission system built with Laravel, Bootstrap and live to the internets powered by Heroku.

If you prefer to have a generic Laravel 5 app instead of a massive boilerplate follow this gist. Besides step 1 everything else will be the same.

Without further ado, let’s get started.

Steps:

  • Download Laravel 5 Boilerplate according to the Quick Start Documentation. Also git init for the project.

  • Create a local MySQL database for development:

1
2
3
$ mysql -uroot -p
> create database MY_DATABASE_NAME;
Ctrl-C to exit
  • Create a Procfile to teach Heroku how to boot up our app:
1
$ echo web: vendor/bin/heroku-php-apache2 public/ > Procfile
  • Create a Heroku app. You will need the Heroku CLI installed.
1
$ heroku create YOUR_APP_NAME
  • Add a PHP buildpack:
1
$ heroku buildpacks:set https://github.com/heroku/heroku-buildpack-php
  • Add MySQL to our Heroku project:
1
$ heroku addons:add cleardb

This creates a config variable called CLEARDB_DATABASE_URL which you can view using the following command. Add CLEARDB_DATABASE_URL to our .env file.

1
$ heroku config | grep CLEARDB_DATABASE_URL

Modify config/database.php so that we connect to our Heroku database.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<?php
$url = parse_url(getenv("CLEARDB_DATABASE_URL"));
$host = $url["host"];
$username = $url["user"];
$password = $url["pass"];
$database = substr($url["path"], 1);
return [
...
'connections' => [
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', $host),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', $database),
'username' => env('DB_USERNAME', $username),
'password' => env('DB_PASSWORD', $password),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
],
  • Set an app key:
1
2
$ php artisan key:generate --show
$ heroku config:set APP_KEY=APP_KEY_GOES_HERE
  • Load assets over HTTPS in production. Heroku uses https:// to host websites. If we try to load or files over http:// then we will get a Mixed Content, insecure stylesheet or insecure script error. Therefore, head into App/Providers/AppServiceProvider.php and configure to load everything over HTTPS in production:
1
2
3
4
5
6
7
8
use Illuminate\Support\Facades\URL;
...
// Force SSL in production
if ($this->app->environment() == 'production') {
URL::forceScheme('https');
}
  • Push to Heroku.
1
2
3
4
$ git add -A
$ git commit -m 'heroku deployment stuff'
$ git push heroku master
$ heroku open

Congrats! You’ve deployed your Laravel 5 application to the internet using Heroku.