AppDividend
Latest Code Tutorials

How To Deploy Laravel Project On Heroku

0 959

Get real time updates directly on you device, subscribe now.

In this tutorial, I will show you How To Deploy Laravel Project On Heroku. In this small tutorial, I will guide you how to deploy Laravel application to the Heroku. Heroku Cloud Application Platform is a Platform as a Service (PaaS) that enables developers to run, build, and operate applications entirely in a cloud.

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.

Deploy Laravel Project On Heroku

Now, let us start our tutorial by installing Heroku CLI.

Step 1: Install Heroku CLI.

You can download from this link depends on your OS. I am using Mac, so I am installing via homebrew.

brew install heroku/brew/heroku

After that, verify it by the following command.

heroku --version

You will get something like this depending on your Node.js version. “heroku-cli/6.16.12-04b3626 (darwin-x64) node-v9.10.1“.

Step 2: Install Laravel Application.

Go to your terminal and install it using the following command.

composer create-project laravel/laravel herokularavel --prefer-dist

It will install the project.

Step 3: Create a Procfile.

Inside the Laravel Root folder, make a file called Procfile.

Write the following line inside the Procfile.

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

Step 4: Initialize project as Git repo.

Okay, initialize Git repo using the following command.

git init

Step 5: Logging Into Heroku From Terminal.

Go to your terminal and type the following command to enter your Heroku Credentials.

heroku login

Step 6: Create a heroku application.

Type the following command.

heroku create

It will create an application with a weird name. Mine name is mysterious-waters-55261.

Step 7: Setting a Laravel encryption key.

Type the following command to see the key.

php artisan key:generate --show

It will show the key, and you need to copy it for now.

Next step is to type the following command and does not hit the enter.

heroku config:set APP_KEY={Your copied key}

Now, hit the enter and you are set up with the Application key.

Step 8: Push the changes to Heroku.

After that add the files on Git using the following command.

git add .

Commit the changes.

git commit -m "first laravel deployment to heroku"

Push the changes to the Heroku server.

Related Posts
1 of 33
git push heroku master

You will see something like this.

Laravel Heroku Deployment

 

Step 9: Start the Application.

Type the following command to start the application on Heroku.

heroku open

This is my application Live URL: https://mysterious-waters-55261.herokuapp.com/

Step 10: Configure the Database on Heroku.

I 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 on here.

Type the following command to create PGSQL database.

heroku addons:create heroku-postgresql:hobby-dev

You can more about data store on this URL.

Now, you can see your Database URL using the following command.

heroku config

It will give us two values.

  1. APP_KEY
  2. DATABASE_URL

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

First, change the by default database to pgsql from mysql.

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

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

Next, your pgsql database config array looks 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',
        ],

So, your complete database.php file looks like this.

<?php

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

return [

    /*
    |--------------------------------------------------------------------------
    | Default Database Connection Name
    |--------------------------------------------------------------------------
    |
    | Here you may specify which of the database connections below you wish
    | to use as your default connection for all database work. Of course
    | you may use many connections at once using the Database library.
    |
    */

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

    /*
    |--------------------------------------------------------------------------
    | Database Connections
    |--------------------------------------------------------------------------
    |
    | Here are each of the database connections setup for your application.
    | Of course, examples of configuring each database platform that is
    | supported by Laravel is shown below to make development simple.
    |
    |
    | All database work in Laravel is done through the PHP PDO facilities
    | so make sure you have the driver for your particular database of
    | choice installed on your machine before you begin development.
    |
    */

    'connections' => [

        'sqlite' => [
            'driver' => 'sqlite',
            'database' => env('DB_DATABASE', database_path('database.sqlite')),
            'prefix' => '',
        ],

        'mysql' => [
            'driver' => 'mysql',
            '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' => '',
            'strict' => true,
            'engine' => null,
        ],

        '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',
        ],

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

    ],

    /*
    |--------------------------------------------------------------------------
    | Migration Repository Table
    |--------------------------------------------------------------------------
    |
    | This table keeps track of all the migrations that have already run for
    | your application. Using this information, we can determine which of
    | the migrations on disk haven't actually been run in the database.
    |
    */

    'migrations' => 'migrations',

    /*
    |--------------------------------------------------------------------------
    | Redis Databases
    |--------------------------------------------------------------------------
    |
    | Redis is an open source, fast, and advanced key-value store that also
    | provides a richer set of commands than a typical key-value systems
    | such as APC or Memcached. Laravel makes it easy to dig right in.
    |
    */

    'redis' => [

        'client' => 'predis',

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

    ],

];

Step 11: Add the file to the heroku and run migrations.

Now, add the file to the Heroku.

git add .
git commit -m "added postgres database configuration"
git push heroku master

Now, run the migrations.

heroku run php artisan migrate

It will ask to run the migration on production and enter yes.

If all the configurations are right then you will be able to create the tables in the database.

 

Laravel Deployment On Heroku

Step 12: Create Authentication system.

Type the following command to generate Authentication scaffold.

php artisan make:auth

Add the files on the Heroku.

git add .
git commit -m "added auth scaffold"
git push heroku master

You can see that our scaffold is generated successfully. 

Okay, now you can able to register a user, and that is cool.

We have successfully deployed Laravel on Heroku and set up a database on Heroku.

That is it for deploy laravel project on heroku. Thanks for taking this tutorial.

Leave A Reply

Your email address will not be published.