Deploy Laravel application with database to Heroku
Tags: laravel, heroku, tutorial, devops
Published: 2020-05-16 12:05:41

<a href="https://www.buymeacoffee.com/jsafe00" target="_blank"><img src="https://cdn.buymeacoffee.com/buttons/default-green.png" alt="Buy Me A Coffee" style="height: 51px !important;width: 217px !important;" ></a>

<b>Why Heroku? </b>

I would like to start my blog with the question “why” as there are lots of technologies to choose from that are maybe better and more convenient to use but after all, it’s all about personal preferences.

So why Heroku? 

Heroku is a public PaaS (Platform as a Service) provider that makes it easy to deploy applications written using several technology stacks at no cost. Application developers all around the globe use Heroku-like services to implement and test their applications remotely without any pain.
It’s free and easy to use. 


<b><u>Prepare your Laravel app</u></b> 

On this tutorial, I will use the repo : https://github.com/rappasoft/laravel-boilerplate.  Laravel Boilerplate provides you with a massive head start on any size web application. It comes with a full featured access control system out of the box with an easy to learn API and is built on a Bootstrap foundation with a front and backend architecture. A big thanks to all who made contributions to this open source laravel app. Go ahead, fork and clone that repo or you can use your own depending on your preference.

I assume you tested your app on local and everything worked fine before proceeding to set up the heroku.

<b><u>Prepare your Heroku CLI</u></b>
You can download heroku-cli at: 

https://devcenter.heroku.com/articles/heroku-cli#download-and-install.
 
Download and install Heroku according to your OS.
Once installation is finished, go back to your terminal and try to execute:

<mark>heroku login</mark>

The heroku login will appear. Click login. 
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/lksjuuq7f3bjf9zvm573.JPG)

When you go back to your terminal you will see something like this:
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/07mogoui8s7b3pbpx4s6.JPG)

<b><u>Create Procfile inside your main Laravel</u><b> 

Inside your Procfile, put this code line.
web: vendor/bin/heroku-php-apache2 public/

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/66vxqqtzppvf2s1wxjxj.JPG)

Go back to your terminal and type:

<mark>git init</mark> - This is for git initialization 
<mark>heroku create</mark> - This command will create your app and  repository name in Heroku

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/5u1l9b9efxd4tt6t8zre.JPG)


After that, commit and push your project to Heroku.

<mark>git add .</mark>
<mark>git commit –m “first commit”</mark>
<mark>git push heroku master<mark>
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/cgkey4eaxqao9spec0rq.JPG)

Now, go to heroku dashboard, you will see something like this.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/dd8znapw10jajefyqo3i.JPG)

Try to access your app by clicking Open app button.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/yiyorwkfjsb09stmztf7.JPG)

This will give you a 500 server error because we have not set any configuration from our .env file on our Laravel app into Heroku.

<b><u>Add Config Vars setttings in Heroku</u></b>

Click settings then add the config vars like below. Follow some config in your .env file. The APP_ENV is production while APP_URL is your heroku url. Please refer below my configuration.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/44h1uv33kjd0cxtyk82q.JPG)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/fe4bx0x912bo9i96la54.JPG)

Try to access your app. You should see your app is successfully deployed now. 
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/tzmbgpesvov3tcrnxbn3.JPG)

<b><u>Configure database</u></b>
Lastly, we will configure our database so that we can run migrations and seeders in Heroku. Actually, we can use MySQL but we need to upgrade our membership. That’s why in this tutorial, we will be using PostgreSQL because it’s free. :) 

Click on Resources Menu. Then search for Postgres. 
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/d7ins5gvsjoke1yc1mp8.JPG)

Click install Heroku Postgres. After successful installation, heroku postgres is available in our dashboard resources just like below.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/ku9bsj588ojcqa26mw9h.JPG)

After that, go to your terminal and type:

<mark>heroku config</mark>
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/46exbxu97qw3pbg71fpt.JPG)

Take note on your <b>DATABASE_URL</b>.

Change DATABASE_URL to the url on your heroku config.

Sample Heroku config setup:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2qcispfm5zmfwglfq9kc.PNG)

Set pgsql as your default database setup.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7bgmpxwjcwjkpdjxto11.PNG)

So far, this is the best practice I know. Please read comments section for details or share better practice you know :). 

Now, we will commit and push our changes in heroku repo. In your terminal run these commands below. 

<mark>git add .</mark>
<mark>git commit –m “update db connection”</mark>
<mark>git push heroku master</mark>

Then, run migration in heroku. 

<mark>heroku run php artisan migrate</mark>
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/6ifzwx38tplqhwcxn73z.JPG)

After migration, don’t forget to execute
<mark>heroku run php artisan db:seed</mark>
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/f8056jlo4ebxljl0wwqo.JPG)

Then access your heroku app 

http://whispering-wave-57771.herokuapp.com/.
Use the email and password below to explore laravel-boilerplate.
email: admin@admin.com
password: secret
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/23qxyxxuzlqv8d55kxwe.JPG)

To rename your app, execute the ff:

<mark>heroku apps:rename newname</mark>

There you have it! You now have a running laravel-boilerplate app with database deployed in heroku.

Hopefully this tutorial is useful to you. 


Ending this tutorial with something I read last night which I think is worth sharing. 🙂

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/50t89fxoom8l55ulxagg.JPG)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/hy5bumtnljwz4pm84qco.JPG)

https://stackoverflow.blog/2020/05/14/the-most-successful-developers-share-more-than-they-take/