Deploy Laravel on Heroku
Tags: laravel
Published: 2021-08-13 03:07:19

## 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.

- [Register GitHub here](https://github.com/join)
- [Register Heroku here](https://signup.heroku.com/)

## Setup Laravel
Here I use fresh Laravel project. To create the project, use command below.
```bash
laravel new l8-on-laravel
```

> **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](https://devcenter.heroku.com/articles/releases) and its [phase](https://devcenter.heroku.com/articles/release-phase).

Put this into the `Procfile`.
```env
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.
```conf
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.
```sh
#!/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.

```bash
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.
> {% post https://dev.to/thexdev/add-github-ssh-key-19pe %}

## Setup and deploying to Heroku
First we need to create a new app on Heroku.
![Create new app](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b77hwwl39yvydcrqg20x.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vaak97ky4814f1bbb26y.png)

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

Go to settings...
![Go to settings](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ss3thnsyv01defi1k6ur.png)

Click reveal config vars...
![Reveal config vars](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zowg51fwh96nijorsezn.png)

Fill the config vars.
![Filling config vars](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hfnwxg4m6chvk2igkl7s.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5sio12uabj903e826opi.png)

Select the repository. For my case the repository is `l8-on-heroku`.
![Select repository](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b0rwmo79wc280mv6erkf.png)

Enable auto deploys...
![Enable auto deploys](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3kuaa8lqrrd43hh4ejb6.png)

And finally deploy the app.
![Deploy](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l4e09alvrnmln7b1bmyb.png)

Now, check the app and here my result [l8-on-heroku](https://l8-on-heroku.herokuapp.com/).
![App](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7ebqww2f230eze11u85a.png)

## References
- {% github thexdev/l8-on-heroku no-readme %}