How to Deploy A Laravel Application to Heroku with Database?

Harish Kumar · · 5001 Views

In this post, I'll show you the best way to deploy your current Laravel application from local to Heroku with the Postgres database. It's quick and easy.

Prerequisites:

  1. Git version control

  2. Register for a Heroku account and install the Heroku toolbelt, a command-line tool for managing Heroku apps.

Create the project

Get your Laravel project ready any way you like. For our tutorial, let's create a fresh Laravel app.

laravel new laravel-heroku
cd laravel-heroku

Add your Procfile

Heroku knows which processes to run for the application based on a configuration file that is Procfile. So, we need to create a Procfile to serve the site from /public.

echo web: heroku-php-apache2 public/ > Procfile

Initialize the git repository:

Let's get it into git.

git init
git add .
git commit -m "Initial commit with Procfile."

Create the Heroku app

After installing the Heroku CLI, you need to login to your Heroku account. You can use the following command to login:

heroku login

Next, to create a new Heroku application that you can push to, use the heroku create command:

heroku create

The output of that command would be something like this:

Creating app... done, ⬢ app-name-here
https://app-name-here.herokuapp.com/ | https://git.heroku.com/app-name-here.git

Setting environmental variables

By default, Laravel offers the environmental variables in the .env file. We should send the data of .env to the Heroku servers.

You can use the following command to set .env variables to Heroku. 

heroku config:set $(cat .env | sed '/^$/d; /#[[:print:]]*$/d')

Configuring the Database

First, add the Heroku add-on for Postgresql. 

heroku addons:create heroku-postgresql:hobby-dev

you should see an output like this:

Adding heroku-postgresql:hobby-dev on app-name-here... done, v14 (free)
Attached as HEROKU_POSTGRESQL_COLOR_URL
Database has been created and is available
 ! This database is empty. If upgrading, you can transfer
 ! data from another database with pgbackups:restore.
Use `heroku addons:docs heroku-postgresql` to view documentation.

At this point your database should be up and running. you can see your Database URL using the following command.

heroku config
# or
heroku pg:credentials:url

It will give us two values, APP_KEY and DATABASE_URL.

You can grab the database credentials from APP_KEY and add that to the .env file, or copy the DATABASE_URL and open your config >> database.php file

Now, at the top, you need to define:

$DATABASE_URL=parse_url('Your database URL');

Next, you need to modify pgsql database config array like this.

// database.php

'pgsql' => [
            'driver' => 'pgsql',
            'host' => $DATABASE_URL["host"],
            'port' => $DATABASE_URL["port"],
            'database' => ltrim($DATABASE_URL["path"], "/"),
            'username' => $DATABASE_URL["user"],
            'password' => $DATABASE_URL["pass"],
            'charset' => 'utf8',
            'prefix' => '',
            'schema' => 'public',
            'sslmode' => 'require',
        ],

Next, don't forget to add DB_CONNECTION to pgsql in the Heroku environmental variables.

heroku config:set DB_CONNECTION=pgsql

Push the changes to Heroku and run migrations.

Commit the changes.

git add .
git commit -m "first laravel deployment to heroku"

Push the changes to the Heroku server.

git push heroku master

Now, run the migrations.

heroku run php artisan migrate

If your app environment is in production then it will ask to run the migration on production and enter yes.

Now, we have successfully deployed Laravel on Heroku and set up a database on Heroku.

0

Please login or create new account to add your comment.

0 comments
You may also like:

Building a Real-Time Chat App with Laravel Reverb and Nuxt 3

Building a real-time chat application is a great way to understand the power of WebSockets and real-time communication. In this tutorial, we will walk through creating a Real-Time (...)
Harish Kumar

How to Set Up Nuxt 3 Authentication with Laravel Sanctum (Step-by-Step Guide)

In modern web development, securing your application’s authentication process is a top priority. For developers building Single Page Applications (SPA) or Server-Side Rendered (...)
Harish Kumar

Laracon US 2024: Laravel 11 Minor Features That Enhance Performance

At Laracon US 2024, Taylor Otwell and the Laravel team introduced a series of "minor" features for Laravel 11 that are anything but minor. These enhancements, while not headline-grabbing (...)
Harish Kumar

PHP OPCache: The Secret Weapon for Laravel Performance Boost

OPCache, a built-in PHP opcode cache, is a powerful tool for significantly improving Laravel application speed. This guide will demonstrate how to effectively utilize OPCache to (...)
Harish Kumar

How to Use DTOs for Cleaner Code in Laravel, Best Practices and Implementation Guide

When developing APIs in Laravel, ensuring your responses are clear, concise, and consistent is crucial for creating a maintainable and scalable application. One effective way to (...)
Harish Kumar

Data Type Validation in Laravel Collections with the `ensure()` Method

Before moving on to the ensure() method, let us first know what Laravel Collections actually are. These are wrappers of PHP arrays, offering a fluent and helpful interface in interacting (...)
Harish Kumar