Installing a Laravel app on Heroku

Posted on April 30, 2014 | By Matt Stauffer


Warning: This post is over a year old. I don't always update old posts with new information, so some of this information may be out of date.

Heroku has had PHP support for a while, but it's been a definitively second-class citizen. Yesterday Heroku announced a huge boost in their PHP support, including out-of-the-box Composer support and an intention to become a legitimate destination for hosting modern PHP apps.

So, let's take a look at the fastest and simplest way to get a stock Laravel install up and running on Heroku.

Pre-requisite:

Sign up for a Heroku account and install the Heroku toolbelt, a command-line toolkit for managing your Heroku apps.

Create the project

However you prefer, get your Laravel project initialized.

$ laravel new laravel-heroku
$ cd laravel-heroku

Add your Procfile

Heroku knows which processes to run for your app based on a configuration file called a Procfile. The default apache2 process (if you don't use a Procfile) points to the web root, not to /public... so we need to create a custom Procfile to serve the site from /public.

Add a file with the name Procfile (capitalization matters) that contains this line:

web: vendor/bin/heroku-php-apache2 public/

(more details: https://devcenter.heroku.com/articles/custom-php-settings#setting-the-document-root)

Initialize the git repo

OK, our code is ready to go. Let's get it into git.

$ git init
$ git add .
$ git commit -m "Initial commit of stock Laravel install."

Create the Heroku app

Since you have the Heroku Toolbelt installed, you can create and modify your apps directly from the command line.

$ heroku create

The output/prompt should look something like this:

± heroku create
Creating app... !
 ▸    Invalid credentials provided.
heroku: Press any key to open up the browser to login or q to exit:
Logging in... done
Logged in as me@email.com
Creating app... done, ⬢ app-name-here
https://app-name-here.herokuapp.com/ | https://git.heroku.com/app-name-here.git

Write down or just remember the "app-name-here"; this is the unique identifier for the Heroku app you just created. The app will run on the Heroku Cedar stack by default.

Add an APP_KEY

Let's deploy an environment key for our encryption key now.

Generate a new key:

php artisan key:generate --show

Copy the output of that, and then run this command:

heroku config:set APP_KEY=the_key_you_copied_here

You should see this output:

Setting config vars and restarting ⬢ app-name-here... done, v3
APP_KEY: the_key_you_copied_here

Deploy your code to the Heroku app

With Heroku, you push new code to your site by pushing to the heroku git remote.

$ git push heroku master

Look for this at the end:

----> Launching... done, v3
       http://app-name-here.herokuapp.com/ deployed to Heroku

Celebrate!

Check it: Laravel install success graphic

Notes

Heroku's PHP support is not the only thing that has gotten an upgrade; their PHP support documentation is now fantastic. Check it out for many more tutorials and much more in-depth introductions. Heroku - Getting Started With PHP

They've also, since I wrote this article, added Laravel-specific documentation: Heroku - Getting Started with Laravel

Postscript

If you have any issues with this walkthrough, please let me know on Twitter so i can keep this up-to-date. Thanks!


Comments? I'm @stauffermatt on Twitter


Tags: php  •  laravel  •  heroku