How to deploy a Laravel App into Heroku
Today I want to share about how to deploy a Laravel App into Heroku. In this tutorial, I assume that you are quite familiar with Laravel, because I will not discuss about Laravel basic, just to the point like in the title of this tutorial. Also you need to create an account into Heroku before following this tutorial.
Step 1 — Prepare your Laravel App or install new Laravel project
First step, you need to prepare your existing Laravel project or you can create new laravel project. To install new Laravel project you can type like this in your terminal.
composer create-project — prefer-dist laravel/laravel newProject
After your Laravel project is ready, enter into your main project directory.
Step 2 — Prepare your Heroku CLI
Second step, you need to prepare your Heroku CLI in your computer. Heroku supported various operating system, you can take a look at this https://devcenter.heroku.com/articles/heroku-cli#download-and-install
Download and install Heroku installer base on your operating system.
After Heroku installation finish, you can back to your terminal and you can try to access Heroku in there, try to login into Heroku from your terminal
heroku login
The popup of login will appear like this
After login successful, back into your terminal and you will see something like this
Step 3 — Create Procfile inside your main Laravel project directory and push your project into Heroku
In this step, you need to create Procfile (Heroku configuration file) inside your main Laravel project directory. Then inside your Procfile, you can put this code line
web: vendor/bin/heroku-php-apache2 public/
After that, you can save your Procfile, then back into your terminal and type
git init
for git initiation, then type
heroku create
You will get your app name created by Heroku and your repository name, like this
After that, you need to commit and push your project into Heroku by type
git add .
git commit -m “Initial Commit”
git push heroku master
Successful! Now our Laravel App already deployed into Heroku, you can take a look at your Heroku dashboard, and you will look this
You can try to enter into your app, just click into your app, then we can try to click “Open app” button to look at your Laravel App
You will redirecting into your app, but you will see an 500 — Server Error like this
The error is because we still not set any configuration from our .env file inside our Laravel project into Heroku.
Step 4 — Adding some configuration in Heroku base on .env file
In this step, we will set some configuration inside Heroku, click “Settings” menu
Then click, “Reveal Config Vars”
We need to add some configuration from our .env file into that field
After we add the configs into Heroku, will be like this
Now, we can try to open the app again with click “Open app” button and we can look the result is successful :)
Step 5 — Database configuration
The last step is about database configuration, so we can running our migrations in Heroku. Actually to using MySQL service in Heroku we need to upgrade our membership. In this tutorial we will using PostgreSQL because it’s free :D
First, click “Resources” menu, then in the “Add-ons” side, click “Find more add-ons”. You’ll redirect into Heroku Add-ons page.
In the Heroku Add-ons page, we click Heroku Postgres.
After that, we’ll redirect into Heroku Postgres page, and we need to click “Install Heroku Postgres” button in the right side.
After click the installation button, we will look a view like that, and we need to click “Provision add-on”.
After installation is finish, we can look the Heroku Postgres is available in our dashboard resources, that mean our Heroku Postgres installation is successful.
After that, we go back into terminal, and type
heroku config
We will got some configuration value, for this moment we focus on “DATABASE_URL” value to setup our Laravel App database.
Now, open your config/database.php file, and in the top of file, put this code
$DATABASE_URL=parse_url(‘DATABASE_URL’);
Change the ‘DATABASE_URL’ with your “DATABASE_URL” in the terminal previously. After that, setting your pgsql part became like this
Then, set the pgsql became your default database setup, like this
Ok, now we need to commit and push our config/database.php update into Heroku repository. Back into terminal and running this
git add .
git commit -m “Update database connection”
git push heroku master
After that, now you can running your migration in the terminal, like this
Successful, now you are ready to running your Laravel App with Heroku. Hopefully this tutorial is useful and help you all guys :)
Thanks