How To Deploy Laravel Project With MYSQL Database To Heroku
This tutorial will walk you through the configuration and deployment of your Laravel project with the integration of MySQL database on Heroku.
So what is Heroku?
Heroku is a container-based cloud Platform as a Service (PaaS). Developers use Heroku to deploy, manage, and scale modern apps. the platform is elegant, flexible, and easy to use, offering developers the simplest path to getting their apps to market.
- Your laravel project
- Heroku Account
- Heroku CLI – The Heroku CLI requires Git If you don’t already have Git installed, follow this link.
- Some minutes of adventure.
Deploy Laravel Project With MYSQL Database To Heroku
All set? Now let us get started!
Step 1: Install Heroku CLI
You can get it from here. I am using a mac so I’ll be installing with Homebrew.
brew install heroku/brew/heroku
To verify successful installation, run the following command
You should get something like this, depending on your node js version
heroku/7.40.0 darwin-x64 node-v12.16.2
Step 2: Create a Laravel Project
Laravel new project name
if you already have a project you want to deploy, you can move to step 3.
Step 3: Create a Procfile
In the laravel project root folder, create a file called procfile.
Procfile specifies the commands that are executed by the app on startup.
Type the command below to do create a procfile
echo "web: vendor/bin/heroku-php-apache2 public/" > Procfile
Step 4: Login into heroku from your terminal
Step 5: Create an Heroku app
heroku create foltestapp
Where “foltestapp” represents your project name
Step 6: Add Heroku Remote To Your Local Repo
First, you need to initialize git in your project
Next add a remote origin to your local repository with the Heroku git:remote command. All you need is your Heroku app’s name, in my case it will be
heroku git:remote -a foltestapp
Step 7: Set Your App Key
First, we need to create a buildpack that will specify PHP as the app language, so run;
heroku buildpacks:set heroku/php
Now let’s set the app key
The application’s encryption key is used by Laravel to encrypt user sessions and other information. Its value will be read from the APP_KEY environment variable on Heroku.
Run the command below to generate it.
heroku config:set APP_KEY=$(php artisan --no-ansi key:generate --show).
Step 8: Push changesto Heroku
It’s time to push our project to Heroku, add the files by
git add .
Then commit the changes
git commit -m “Deploying laravel to heroku is fun’
and push the changes to Heroku
git push heroku master
You should see something like this
Step 10: Configure MySQL on Heroku
I have used an add-on called Clear DB and it works well. ClearDB is a powerful, high-speed database-as-a-service in the cloud for your MySQL powered applications.
The ClearDB add-on can be installed and up and running in your app in no time! Run the following command to add ClearDB to your application.
heroku addons:create cleardb:ignite
N.B ignite is a free version of the platform, get full plan information here
So you should see something like this
Next, we need to retrieve your database URL by issuing the following command:
heroku config | grep CLEARDB_DATABASE_URL
Your output should look like this: CLEARDB_DATABASE_URL: mysql://b2d7528ef3cf84:email@example.com/heroku_c9de243414b056d?reconnect=true
The URL in a nutshell means: mysql://username:password@hostname/database_name?reconnect=true
so, you can copy those
So we can extract what we need from it.
According to the .env file in laravel project, we need the following to establish a connection to a database
You might have set some values for local development, you don’t need to remove them, we are going to set the above prerequisites as an environment variable on Heroku the same way we did for the APP_KEY in Step 7. Remember?
Okay, let’s move on.
First, let’s do the Extraction
From the URL gotten from CLEAR DB,
MY values are
Then We Set the Environment variables with the commands below;
heroku config:set DB_HOST=hostname DB_DATABASE=databasename
heroku config:set DB_USERNAME=username DB_PASSWORD=password
Please Replace hostname, databasename, username and password with the appropriate values from your clearDB URL.
You can confirm what you have done with
Before running migration, let’s fix a possible bug that can come our way.
Error name: Specified key was too long error , please follow this link to set some stuffs.
Done? You can now migrate your database with
heroku run php artisan migrate
We have successfully deployed Laravel on Heroku and set up a MySQL database on Heroku.
Thanks for taking this tutorial.