Deploy Laravel Apps to production. #1 Heroku

James Falola
Hacktive Devs
Published in
4 min readApr 20, 2019

After writing your code and you feel your app is complete enough, the next thing is to put it online for your friends and family to see( show off!). JK! (actually not, we all tend to show off on our first app).

But apart from showing off, it’s quite important to have the live version of your app online, when you apply for jobs and projects, the app on your 127.0.0.1 won’t really matter.

You need something live to show for your knowledge and experience over the course of writing your app. Personally, while learning a new framework, language or tool I do that by making an app out of it then it goes to my portfolio(on second thought, actually most people do that).

Take your practice app serious they come in handy. My whole point is when your app is done, you deploy.

The following steps are required in getting your app safely deployed to production.

  • Create your app
    Initialize your Laravel application and set up your database correctly, I’ll be using MySQL, you can use any other DB you like.
    Still confused setting up your app? Check the official documentation here.
  • Build something amazing
    Code your app, I’m going to be using the age returning app I used to demo Web feedback with toastr.js.
  • Pick an online server to host your app
    I’ll be demonstrating with Heroku and a Shared hosting account with 000webhost.com (Cpanel control panel). First, let’s discuss deploying to Heroku then we’ll talk about shared hosting with Cpanel.

Deploying to Heroku

Not familiar with Heroku? well, here you go…

Heroku is a container-based cloud Platform as a Service (PaaS). Developers use Heroku to deploy, manage, and scale modern apps. Heroku is fully managed, giving developers the freedom to focus on their core product without the distraction of maintaining servers, hardware, or infrastructure.

I hope that makes sense. Now let’s start working with Heroku,

  • Setup Heroku on your PC.
    By that, I mean create an account here, download Heroku CLI here and log in to the Heroku CLI with your account details.
    After we download and install the Heroku CLI, open your terminal and see if it’s properly installed. Run the command Heroku. If you’re on windows, kindly try to stay away from PowerShell, wasted my time once ‘cos it wasn’t recognizing a character I typed(they might have fixed that by now). Use CMD or download a third-party console software for windows, I used Cmder.
$ heroku

If properly installed, this should be the output of the command, windows users might need to add Heroku CLI to path before it works. This post should help with Heroku Windows installation (Click here).
To log in to our Heroku CLI open a terminal and run the command

$ heroku login

This will redirect to a browser and we can authorize with just a button click. Now that we have the Heroku CLI all set up, let’s proceed to deploy our app to Heroku

  • Add Procfile to our app
    Procfile
    in Heroku is a file containing a code executed on our app startup.
    To create your app Procfile, run this terminal command.
$ echo web: vendor/bin/heroku-php-apache2 public/ > Procfile
  • Create a Heroku app.
$ heroku create your-app-name
  • Add Heroku Remote
    We’ll be pushing our code to Heroku with Git, If you already have git initialized on your app, proceed to run the commands from line 3, if not start from line 1(enter project directory) and initialize Git.
$ cd my-project/
$ git init
$ heroku git:remote -a lrv-tst
  • Push to Heroku
    We’re almost done deploying to Heroku, but before we do that, let’s configure our environment variables for production.

Show our app key

$ php artisan key:generate --show

Copy the key and add it to our Heroku app config variable running the command below

$ heroku config:set APP_KEY=your-app-key
$ heroku config:set APP_URL=your-app-name.herokuapp.com

The second line adds our app production URL.
We can go ahead to adding other environment variables like APP_NAME.

$ heroku config:set APP_NAME=your-app-name

Now we’re good to deploy.

$ git add .
$ git commit -m "Adding Procfile"
$ git push heroku master

By default Heroku runs our app on https:// and this might cause a mixed content error. Let’s fix that now by ensuring our assets to load over https://.
Open App/Providers/AppServiceProvider.php and add the code below.

Since we just made a change to our app, don’t forget to commit again and push to Heroku.

Open our app on a web browser and boom! We’re live!

We’ll discuss Cpanel and other production servers in follow up series.
Got any questions? leave a comment below.
Thanks for your time.

--

--