Laravel App In Heroku

Deploy Laravel App In Heroku Like A Piece Of Cake

Cool name. Heroku right? It sounds like a superhero as it has a hero in it. Well, it is a superhero actually. Heroku is a platform as a service (PaaS) that enables developers to build, run, and operate applications entirely in the cloud.
Cool name. Heroku right? It sounds like a superhero as it has a hero in it. Well, it is a superhero actually. Heroku is a platform as a service (PaaS) that enables developers to build, run, and operate applications entirely in the cloud.

Cool name. Heroku right? It sounds like a superhero as it has a hero in it. Well, it is a superhero actually. Heroku is a platform as a service (PaaS) that enables developers to build, run, and operate applications entirely in the cloud. Hey, I can run and test my app on my local machine. Well, it is to test how our app reacts to production. For example, you can test Mollie payment gateway redirect URL in the localhost. You must have a server right. So to test that do you need to buy an entire server or domain and host? No. We have our superhero Heroku. And it’s free at a certain level. At least you can have 10,000 rows in the database for free. Which will be enough for testing and even for a portfolio. Nice, let’s deploy your Laravel app.

[Step 01] Install Heroku CLI

Well, I like to say that hate Windows OS. So this instruction is only for Unix. To install Heroku CLI run the below command if you are using Linux.

sudo snap install heroku --classic

For mac,

brew install heroku/brew/heroku

Now verify the version.

heroku --version

You will find something similar like this one.

heroku/7.42.0 linux-x64 node-v12.16.2

[Step 02] Create Your Awesome Laravel Application

Let’s install our Laravel application with the below command.

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

Now, as our Laravel application has been installed, we need to create a Procfile.

Wait, what the hell is Procfile? Well,

Procfile that specifies the commands that are executed by the app on startup. You can use a Procfile to declare a variety of process types, including: Your app’s web server. Multiple types of worker processes. A singleton process, such as a clock.

Create a file called Procfile in your root directory. And add the below line in it.

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

Now, go on. Develop your project. Whatever you want just as old time. And then we need to init git.

[Step 03] Initialize Git

Initialize your git with the below command

git init

And commit your changes as much as you need.

git add .

git commit -m “Initial commit”

 

[Step 04] Create Heroku Application

Ok, to create an app in Heroku, you need to login first.

heroku login

You will see something like the below image.

After pressing any key, you will find yourself in your browser like this.

Press the login button and you are now ready to create an app in Heroku. Just run the below command to do that.

heroku create

And you will see something like this.

Let’s open the URL. You can click on it or can run the below command.

heroku open

Ow no. Did you get an error? Like this one.

Sit back and relax. Everything is solvable within a logical range. So what we need to do? Well, let’s set the APP_KEY first.

php artisan key:generate --show

Now copy the key. We need to set that in Heroku as well.

heroku config:set APP_KEY={Your copied APP_KEY}

Now, of course, your project has some migrations which I still don’t have one. So let me do the auth command.

php artisan make:auth

Well, it will generate some migrations for me. But we need a SQL database in Heroku right? Don’t worry PostgreSQL in Heroku is free. But has some limitations. Let’s add the add-on.

heroku addons:create heroku-postgresql:hobby-dev

And here is the output.

Now we need a database URL. To get that run the below command.

heroku config

And you will find a similar output like mine.

Don’t worry. I will delete this app anyway. Now set the database URL in Heroku config.

heroku config:set POSTGRESQL_DATABASE_URL={The DATABASE_UTL you got from heroku config command}

Now add the below line at the top of your config/database.php file.

$DATABASE_URL=parse_url(env('POSTGRESQL_DATABASE_URL'));

And replace these codes,

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

to

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

And this line,

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

to

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

 

Now commit again as we changed in our code.

git add .

git commit -m "Changed database configuration"

And push them to Heroku master.

git push heroku master

And end output will be something like this one.

 

Now we need to do the migrations in Heroku. You can do that in two ways. Either you can run,

heroku run php artisan migrate

Or,

heroku run bash

php artisan migrate

You can see that your migrations have been migrated.

Now run the below command to open your application URL in the browser.

heroku open

And you will find the beautiful fresh Laravel application face.

Happy deployment.