Deploy Laravel on Heroku
M. Akbar Nugroho
Posted on August 13, 2021
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
HINT
I expect you was installedcomposer
andlaravel/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.
Put this into the Procfile
.
release: ./scripts/release.sh
web: vendor/bin/heroku-php-nginx -C nginx_app.conf public/
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;
}
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;
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
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.
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 (-
).
After that, add all environment variables that exist in your Laravel .env
file.
Now, connect our app with the project repository. This feature will build and re-deploy the app whenever we push changes to the repository.
Select the repository. For my case the repository is l8-on-heroku
.
Now, check the app and here my result l8-on-heroku.
References
-
thexdev / l8-on-heroku
Deploy Laravel on Heroku
Posted on August 13, 2021
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.