How to Deploy Laravel on Heroku with Database

WebSolutionStuff | Feb-12-2021 | Categories : Laravel PHP

In this example we will see How to Deploy Laravel on Heroku with Database, Heroku is a cloud platform as a service (PaaS) supporting several programming languages.

Developers use Heroku to deploy, manage, and scale modern apps. heroku platform is elegant, flexible, and easy to use, offering developers the simplest path to getting their apps to market.

Heroku offers a free plan to help you learn and get started on the platform. Heroku Buttons and Buildpacks are free, and many Heroku Add-ons also offer a free plan.

Here i will show you how to host laravel project on heroku or deploy laravel to heroku with database here we will use postgresql heroku database.

Step 1 :  Install/download Heroku CLI

Step 2 : Install Laravel Application

Step 3 : Create a proc file

Step 4 : Initialize git repo

Step 5 : Login into the Heroku  and create App

Step 6 : Push Laravel changes to Heroku

Step 7 : Configure the postgresql Database on Heroku

Step 8 : Add the project files and run the migration

 

Step 1 :  Install/download Heroku CLI

First of all you need to download or install Hiroku CLI as per your opreating system, here i have window so i have download Hiroku-x64.exe from official website.

You can also download from here : Download Heroku CLI 

This installation method is required for users on ARM and BSD. You must have node and npm installed already.

npm install -g heroku

Now, we need to check and verify whether heroku is installed or not , so, copy below command in your terminal.

heroku --version

 

Step 2 : Install Laravel Application
composer create-project laravel/laravel Laravel_8_Auth --prefer-dist

 

Step 3 : Create a proc file

Inside the Laravel main folder create file and set name Procfile. add following line inside the Procfile.

web: vendor/bin/heroku-php-apache2 public/

 

 

Procfile

 

Step 4 : Initialize git repo

 Initialize Git repository using the below command.

git init

 

Step 5 : Login into the Heroku  and create App

 Now, login into heroku app using web and create new app file, here i have created new app with name laravel-8-auth

create_app

Step 6 : Push Laravel changes to Heroku

 Now, add below code for add file on git ,commitand  push changes as below.

git add .
git commit -m "laravel 8 auth heroku"
$ git push heroku master

 

Step 7 : Configure the postgresql Database on Heroku

we have used Postgres Database because it is free on Heroku. So we need to use an add-on database provision provided by Heroku. You can find more postgresql.

Type the following command to create a PGSQL database.

heroku addons:create heroku-postgresql:hobby-dev

 

Now, we need to config database on heroku.

heroku config

after that we will find 2 value as below.

  1. APP_KEY
  2. DATABASE_URL

Now, copy the DATABASE_URL and open your config  >>  database.php file.

First, change the default database to pgsql from MySQL.

Now, at the top, you need to define $DATABASE_URL = parse_url(“Your generated database URL, copy here”).

 configuration

 

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

now,your pgsql database config array looks like this. 

 

 

<?php

use Illuminate\Support\Str;
$DATABASE_URL=parse_url('your database URL key');
return [


    'default' => env('DB_CONNECTION', 'pgsql'),
    


    'connections' => [

        'sqlite' => [
            'driver' => 'sqlite',
            'url' => env('DATABASE_URL'),
            'database' => env('DB_DATABASE', database_path('database.sqlite')),
            'prefix' => '',
            'foreign_key_constraints' => env('DB_FOREIGN_KEYS', true),
        ],

        'mysql' => [
            'driver' => 'mysql',
            'url' => env('DATABASE_URL'),
            'host' => env('DB_HOST', '127.0.0.1'),
            'port' => env('DB_PORT', '3306'),
            'database' => env('DB_DATABASE', 'forge'),
            'username' => env('DB_USERNAME', 'forge'),
            'password' => env('DB_PASSWORD', ''),
            'unix_socket' => env('DB_SOCKET', ''),
            'charset' => 'utf8mb4',
            'collation' => 'utf8mb4_unicode_ci',
            'prefix' => '',
            'prefix_indexes' => true,
            'strict' => true,
            'engine' => null,
            'options' => extension_loaded('pdo_mysql') ? array_filter([
                PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
            ]) : [],
        ],

        'pgsql' => [
            'driver' => 'pgsql',
            'url' => env('DATABASE_URL'),
            'host' => env('DB_HOST', '127.0.0.1'),
            'port' => env('DB_PORT', '5432'),
            'database' => env('DB_DATABASE', 'laravel_8_auth'),
            'username' => env('DB_USERNAME', 'root'),
            'password' => env('DB_PASSWORD', ''),
            'charset' => 'utf8',
            'prefix' => '',
            'prefix_indexes' => true,
            'schema' => 'public',
            'sslmode' => 'prefer',
        ],

        'sqlsrv' => [
            'driver' => 'sqlsrv',
            'url' => env('DATABASE_URL'),
            'host' => env('DB_HOST', 'localhost'),
            'port' => env('DB_PORT', '1433'),
            'database' => env('DB_DATABASE', 'forge'),
            'username' => env('DB_USERNAME', 'forge'),
            'password' => env('DB_PASSWORD', ''),
            'charset' => 'utf8',
            'prefix' => '',
            'prefix_indexes' => true,
        ],

    ],


    'migrations' => 'migrations',

 

    'redis' => [

        'client' => env('REDIS_CLIENT', 'phpredis'),

        'options' => [
            'cluster' => env('REDIS_CLUSTER', 'redis'),
            'prefix' => env('REDIS_PREFIX', Str::slug(env('APP_NAME', 'laravel'), '_').'_database_'),
        ],

        'default' => [
            'url' => env('REDIS_URL'),
            'host' => env('REDIS_HOST', '127.0.0.1'),
            'password' => env('REDIS_PASSWORD', null),
            'port' => env('REDIS_PORT', '6379'),
            'database' => env('REDIS_DB', '0'),
        ],

        'cache' => [
            'url' => env('REDIS_URL'),
            'host' => env('REDIS_HOST', '127.0.0.1'),
            'password' => env('REDIS_PASSWORD', null),
            'port' => env('REDIS_PORT', '6379'),
            'database' => env('REDIS_CACHE_DB', '1'),
        ],

    ],

];

 

Step 8 : Add the project files and run the migration

 Now, add file to the heroku and run below command.

git add .
git commit -m "laravel 8 auth heroku"
$ git push heroku master

Run migration.

heroku run php artisan migrate

now, enter yes if ask and you will be able to create the tables in the database. and finally you will get output of authentication user like below image.

 

registerheroku

 

Featured Post




Follow us
facebooklogo github instagram twitter