How to Deploy A Laravel App to Heroku

Elusoji Sodeeq
3 min readJan 9, 2018

--

In one of my previous posts, I talked about how to deploy laravel app to shared hosting, and I mentioned you could also deploy your app to a cloud hosting solution like heroku.

In this post, I’ll show you how to deploy your existing laravel app from local to heroku. It’s quite easy.

Things needed:

  • Php and Laravel knowledge
  • Heroku user account
  • Heroku toolbet (Heroku CLI)
  • Git

This tutorial assumes you already have your laravel app created on your local server, so I won’t go through those steps anymore.

Step1: Initialize a git repository in your project working directory git init

Step2: Run heroku create [your_app_name] replace “your_app_name” with your preferred app name or leave it blank for heroku to generate one for you

Step3: Go to your heroku dashboard, in the Settings tab of your app, copy the git url for your project e.g https://git.heroku.com/yourappname.git

Step4: From your cli, run git remote add heroku [your_app_git_url]

Step5: Inside your project directory, create a new file called Procfile (with no extension) and add the following line: web: vendor/bin/heroku-php-apache2 public/

Step6: Commit your changes (with git) and push to heroku git push heroku master

Step7: Copy the APP_KEY from your .env file, then go to the heroku dashboard for you app -> Settings -> Reveal Config Vars. add new key pair values like APP_KEY : base64:…….

Step8: now, navigate to the address for your app in your browser your_app_name.herokuapp.com, your app should be up and running now.

There are couple other things you might want to set up, based on the requirements of your app.

For your Laravel app to work properly, you need to set certain environment variables with heroku (which is an equivalent of your local .env file).

There are two ways you can set env variables on heroku:
1. From the heroku cmd, by running :heroku config:set VAR_NAME=VAR_VALUE

2. From the heroku dashboard, in the Settings tab, click on Reveal Config Vars and set your variables there.

3. [shameful plugin — I wrote a small python library that can help you set all your Heroku Config Vars from your .env file once. Check out the post here or check out the repo directly.]

Heroku Settings Tab

How to configure Database (MySQL) for your laravel-heroku app

Heroku provides you with option to use posgres sql out of the box, but for other database systems, you have to use a third-party add on. Since we are talking about MySQL here, cleardb is my favorite.

From the Resources tab of your app on your heroku dashboard, you can search for the cleardb addon and add to your app. After adding the addon, a config (env) variable will be set for you. You can copy the details from this variable to set up your database.

For example, from this url: mysql://b6de34rj38adfad3:a10cd36db@us-cdbr-iron-east-05.cleardb.net/heroku_c40fa7a488382ef?reconnect=true

the string between mysql:// and : is your DB_USERNAME i.e b6de34rj38adfad3

the string between : and @ is your DB_PASSWORD i.e a10cd36db

the string between @ and the next / is your DB_HOST i.e us-cdbr-iron-east-05.cleardb.net

and the string between / and ? is your DB_DATABASE i.e heroku_c40fa7a488382ef

You can set the above in your env variable, and your laravel app should be connected to the database just fine.

You can use a tool like HeidiSQL or MySQL Workbench to view and manage your database.

That’s it. Now, anytime you make changes to your app locally, updating your heroku app is as easy as:

git add .

git commit -m "commit message"

git push heroku master

and voila!!, your online app is updated.

Note: On heroku, you won’t have access to your *larave.log* file, so in order to be able to see your app-generated logs, you’ll need to direct your app’s logs to heroku’s logs streams. You can do that by setting the .env variable APP_LOG to errorlog. i.e

heroku config:set APP_LOG=errorlog

Then you’d be able to view your logs by running:

heroku logs

Friendly warning: the heroku logs can be sometimes overwhelming, so you just have to take your time to sieve through and find what you’re looking for 😁

Update: To run your regular artisan commands on heroku, precede all statements with heroku run e.g heroku run php artisan migrate

--

--