Here is the step-by-step guide to deploying the Laravel application to Heroku.
Step 1: Install the Heroku CLI
You can download it from this link, depending on your OS. I am using a Mac, so I am installing it via homebrew.
brew install heroku/brew/heroku
After that, verify it using the following command.
heroku --version
Depending on your Node.js version, you will get something like this:
As you can see, we have already installed the Laravel application, but if you have not, you can install it using the following steps.
Step 2: Install the 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.
Go inside the “herokularavel” project:
cd herokularavel
Step 3: Create a Procfile
Inside the Laravel Root folder, make a file called Procfile.
Add the following line inside the Procfile.
web: vendor/bin/heroku-php-apache2 public/
Step 4: Initialize the project as a Git repo
Okay, initialize the Git repo using the following command.
git init
Step 5: Logging Into Heroku From the 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’s 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; you need to copy it now.
The next step is to type the following command and not hit enter.
heroku config:set APP_KEY={Your copied key}
Hit 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.
git push heroku master
You will see something like this.
Step 9: Start the Application
Type the following command to start the application on Heroku.
heroku open
Step 10: Configure the Database on Heroku
I have used the Postgres Database because it is accessible on Heroku. So, we need to use an add-on database provision provided by Heroku. You can find more here.
Type the following command to create a PGSQL database.
heroku addons:create heroku-postgresql:hobby-dev
You can learn more about the data store on this URL.
Now, you can see your Database URL using the following command.
heroku config
It will give us two values.
- APP_KEY
- DATABASE_URL
Now, copy the DATABASE_URL and open your config >> database.php file.
First, change the default database to pgsql from MySQL.
At the top, you must 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.
Step 11: Add the file to Heroku and run migrations
Now, add the file to Heroku.
git add . git commit -m "added postgres database configuration" git push heroku master
Now, run the migrations.
heroku run php artisan migrate
That’s it.
pan muangsaen
I followed your steps, but when i entered the command ‘git push heroku master’, this error pops up
remote: ! Push failed: cannot parse Procfile.
remote: ! Please try pushing again.
remote: ! If the problem persists, see http://help.heroku.com/ and provide Request ID 1f78e2e8-6a8a-917f-61ff-5cd9aac36b26.
remote:
remote:
remote: Verifying deploy…
remote:
remote: ! Push rejected to warm-chamber-31572.
remote:
it doesn’t seem to be able to register the Procfile, can you help me?
Diyata Diallo
Thank you
Matheus Campos
Hello Krunal,
Thank you for shared your knowledge with us … My application is ON perfecty !
Regards
arturas
In Connection.php line 669:
Database hosts array is empty. (SQL: select * from information_schema.tables where table_schema = public and table_
name = migrations and table_type = ‘BASE TABLE’)
In ConnectionFactory.php line 205:
Database hosts array is empty.
Moammer Farshid Enan
Hi, I have got an server error while uploading images. Any idea about this?
masoud
i tried but when i migrate it leads to the follwoing error:
C:xampphtdocsCEMS>heroku run php artisan migrate
Running php artisan migrate on ⬢ frozen-lake-11851… done
Error: connect ETIMEDOUT 44.193.194.211:5000
masoud
PLEASE HELP ME HERE EXPLANATION FOR LITTLE BIT:
‘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’,
Nikunj Padhiyar
Really helpful. Thanks!
athos francisco
Cara, muito obrigado.
Salvou o trabalho aqui com Laravel no Heroku.
Estava quase saindo da Heroku, mas só falvara o Procfile