DEV Community

loading...
Cover image for Deploy Laravel application with database to Heroku

Deploy Laravel application with database to Heroku

safventure profile image Saf Venture Updated on ・4 min read

Buy Me A Coffee

Why Heroku?

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.

Prepare your Laravel app

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.

Prepare your Heroku CLI
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:

heroku login

The heroku login will appear. Click login.
Alt Text

When you go back to your terminal you will see something like this:
Alt Text

Create Procfile inside your main Laravel

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

Alt Text

Go back to your terminal and type:

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

Alt Text

After that, commit and push your project to Heroku.

git add .
git commit –m “first commit”
git push heroku master
Alt Text

Now, go to heroku dashboard, you will see something like this.
Alt Text

Try to access your app by clicking Open app button.
Alt Text

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.

Add Config Vars setttings in Heroku

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
Alt Text

Try to access your app. You should see your app is successfully deployed now.
Alt Text

Configure database
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

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

Alt Text

After that, go to your terminal and type:

heroku config
Alt Text

Take note on your DATABASE_URL.

On your laravel app go to config/database.php file, and put this code on the top.

$DATABASE_URL=parse_url(‘DATABASE_URL’);
Change DATABASE_URL to the url on your heroku config.
Alt Text

Then set the pgsql part like below.
Alt Text

Set pgsql as your default database setup.
Alt Text

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

git add .
git commit –m “update db connection”
git push heroku master

Then, run migration in heroku.

heroku run php artisan migrate
Alt Text

After migration, don’t forget to execute
heroku run php artisan db:seed
Alt Text

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

To rename your app, execute the ff:

heroku apps:rename newname

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
Alt Text

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

Discussion (18)

pic
Editor guide
Collapse
moghwan profile image
Marwane

Hi and thanks, this worked like a charm in laravel 7. just a quick note in database.php I changed only the default cnnection to pgsql and added DB_DATABASE as env variable in heroku dashboard. thanks again!

Collapse
safventure profile image
Saf Venture Author

Happy to help! :) and ohh I'll take note on the db settings. Thank you so much :)

Collapse
imanservisi profile image
Isabelle Manservisi

Thanks for this tutorial. It helped me a lot for my first deployment !

Some things to note :

  • with mySQL, it's free, even if heroku require to enter your credit card (no charge at all).
  • don't forget to go in your database Dashboard (in Heroku, click on your addon) and fill your .env with the correct values of host/user... For the password, put it in your heroku config var, so no one can catch it. And do the same in your database.php
Collapse
safventure profile image
Saf Venture Author

Nice! Happy to know that. And thank you for sharing your notes. :) Happy new year!

Collapse
mmimonir profile image
Md. Monirul Islam

Build Failed
show this message-
There are no commands defined in the "ide-helper" namespace.

   Script @php artisan ide-helper:generate handling the post-autoload-dump event returned with error code 1

! WARNING: A post-autoload-dump script terminated with an error
! ERROR: Dependency installation failed!
!

! The 'composer install' process failed with an error. The cause
! may be the download or installation of packages, or a pre- or
! post-install hook (e.g. a 'post-install-cmd' item in 'scripts')
! in your 'composer.json'.
!

! Typical error cases are out-of-date or missing parts of code,
! timeouts when making external connections, or memory limits.
!

! Check the above error output closely to determine the cause of
! the problem, ensure the code you're pushing is functioning
! properly, and that all local changes are committed correctly.
!

! For more information on builds for PHP on Heroku, refer to
! devcenter.heroku.com/articles/php-...
!

! REMINDER: the following warnings were emitted during the build;
! check the details above, as they may be related to this error:
! - A post-autoload-dump script terminated with an error
! Push rejected, failed to compile PHP app.
! Push failed

Collapse
safventure profile image
Saf Venture Author

Hi,

Try these steps:

  1. Remove the line about "barryvdh/laravel-ide-helper" from require-dev on composer.json file.
  2. Then in your terminal execute : composer require barryvdh/laravel-ide-helper
Collapse
beingwaqas profile image
beingwaqas

Hey,

When I run the migration command

heroku run php artisan migrate

I get the following error:

In Connection.php line 671:

SQLSTATE[22023]: Invalid parameter value: 7 ERROR: invalid value for param

eter "client_encoding": "utf8mb4" (SQL: select * from information_schema.ta

bles where table_schema = public and table_name = migrations and table_type

= 'BASE TABLE')

In PostgresConnector.php line 68:

SQLSTATE[22023]: Invalid parameter value: 7 ERROR: invalid value for param

eter "client_encoding": "utf8mb4"

Collapse
safventure profile image
Saf Venture Author

hmmm. did you set pgsql as your default database? Alt Text

Collapse
ellis22 profile image
Ellis

Nice article, straightforward and easy to understand.
I also suggest to watch this video as well: Deploy Laravel App on Heroku

Collapse
safventure profile image
Saf Venture Author

Glad you liked it. :) I will check this video. Thanks.

Collapse
tareq12345 profile image
tareq12345

How to populate the heroku database with data on your local device

Collapse
safventure profile image
Saf Venture Author

hmmm.. I haven't tried that. But maybe this could help devcenter.heroku.com/articles/hero...

Collapse
didin1453fatih profile image
Didin 👨‍💻

I think you can combine Laravel and dbdesigner.id as your database designer. This can make your project readable and clear documentation.

Collapse
safventure profile image
Saf Venture Author

Thanks. i will look into that. :)

Collapse
mmimonir profile image
Md. Monirul Islam

Thanks a lot!
Create Procfile inside your main Laravel.
Where is the Profile file? In my local repo or heroku account?

Collapse
safventure profile image
Collapse
garvitjoshi profile image
Garvit Joshi

Hy, This was Really Informative .... Just a quick question: Should DATABASE_URL be kept secret?

Collapse
safventure profile image
Saf Venture Author

Yes, you’re right. The suggestion of @marwane on the above comment is more appropriate. In database.php, just change the default connection to pgsql and the database is configured in heroku config vars. Sorry I have not updated my tutorial and thanks for pointing that out.