DEV Community

Cover image for Deploy Laravel on Heroku
M. Akbar Nugroho
M. Akbar Nugroho

Posted on

Deploy Laravel on Heroku

TLDR

To deploy Laravel project on Heroku, we just need to create new app on Heroku dashboard, add environment variables on settings, connect GitHub repository, configure Profile and push the project to GitHub. Heroku will re-deploy the app whenever we push code to connected repository.

Prerequisite

I expect you already have GitHub and Heroku account. If you don't, please register and comeback again.

Setup Laravel

Here I use fresh Laravel project. To create the project, use command below.

laravel new l8-on-laravel
Enter fullscreen mode Exit fullscreen mode

HINT
I expect you was installed composer and laravel/installer. If you don't, please install it first.

Create a file named Procfile. Place it in your root project directory. This file is responsible to run our migration tables, caching events, optimization command and configure the web server that we want to use.

HINT
Heroku has a feature called Releases. The advantages of this feature are we can use a pre-build script to run migration tables, caching file, etc.

Read more about releases and its phase.

Put this into the Procfile.

release: ./scripts/release.sh
web: vendor/bin/heroku-php-nginx -C nginx_app.conf public/
Enter fullscreen mode Exit fullscreen mode

Here I use NGINX as web server because for me it's easier to configure. I also pass a custom NGINX configuration to prevent 404 error.

Create a new file called nginx_app.conf and put this into the file.

location / {
    # try to serve file directly, fallback to rewrite
    try_files $uri @rewriteapp;
}

location @rewriteapp {
    # rewrite all to index.php
    rewrite ^(.*)$ /index.php/$1 last;
}

location ~ ^/index\.php(/|$) {
    try_files @heroku-fcgi @heroku-fcgi;
    # ensure that /index.php isn't accessible directly, but only through a rewrite
    internal;
}
Enter fullscreen mode Exit fullscreen mode

Last, create a shell file named scripts/release.sh to run optimization on framework level.

#!/bin/sh

php artisan migrate --force;
php artisan optimize;
php artisan event:cache;
Enter fullscreen mode Exit fullscreen mode

And make sure to mark the file as executable (for public) otherwise Heroku can not run the file for us.

Setup GitHub repository

Create a new repository. Give it a name as you like. I use l8-on-heroku for the repository name.

After that, commit and push the project to the repository.

git init
git add .
git commit -m "initial commit"
git remote add origin git@github.com:thexdev/l8-on-heroku.git
git push origin main
Enter fullscreen mode Exit fullscreen mode

HINT
You can check this post if you don't know how to use ssh for your repository.


Setup and deploying to Heroku

First we need to create a new app on Heroku.
Create new app

Give our app a name. Here I use l8-on-heroku. You can use any name you like, but ensure it uses lowercase letters and doesn't contain any special characters except dashes (-).
Give the app name

After that, add all environment variables that exist in your Laravel .env file.

Go to settings...
Go to settings

Click reveal config vars...
Reveal config vars

Fill the config vars.
Filling config vars

Now, connect our app with the project repository. This feature will build and re-deploy the app whenever we push changes to the repository.

Choose GitHub

Select the repository. For my case the repository is l8-on-heroku.
Select repository

Enable auto deploys...
Enable auto deploys

And finally deploy the app.
Deploy

Now, check the app and here my result l8-on-heroku.
App

References

Top comments (0)