Deploying Laravel to Heroku via Git (CD)
** Create Procfile inside your main Laravel Installation Folder(root)
Inside your Procfile, put this code line.
web: vendor/bin/heroku-php-apache2 public/
Initialize Git, add files, and commit
We need to get the source code to Heroku. We’ll do this with the Git version control. First and foremost, you have to initialize a Git repository.
Type git init
.
Laravel puts the files that aren’t needed or that shouldn’t be seen by the public in it’s .gitignore
file by default. But if you’re building an application (that’s probably not Laravel), you should worry about creating a .gitignore
file and adding these secret files and configs to it so they won’t be tracked by Git.
Add the files for staging, and commit them using git add
. Then type git commit -m “message here”
.
6. Log into Heroku
To log into Heroku, type Heroku login
, and follow the prompt.
Log into Heroku via the CLI
Completing the login on the browser
I was shown this because I’ve used it recently. If this were to be my first login attempt, I’d have been asked for my email and password.
Logged in status
7. Create Heroku application
Now, let’s move to Heroku fully. To create an application on Heroku, type heroku create app (name of app)
. If you don’t put a name for the app, Heroku will create a random name and URL for it; however, if the name already exists on the Heroku platform, you’ll have to find a new unique one.
Now if we go to our Heroku dashboard, we’ll see our application has been created already.
App created on Heroku
If you type git remote -v
, you’ll see that Heroku has already supplied the correct path.
Remote path for the app
8. Push your code to Heroku, and begin the initial setup
To push the code to Heroku, run the command git push Heroku master
. This will push the code to Heroku. Set up everything, and install the dependencies needed.
You can also see it on the dashboard that we want to use an Apache server based on the information we supplied to our Procfile.
Open app
Now, let’s open the application. Click on the “Open app” button. You’ll get a server error, as shown below.
This is happening because the environment-config variables needed for the application to work are located in our .env
file (in the root directory) aren’t on Heroku yet. We can add these config variables in two ways:
- Add from your Heroku dashboard.
- Add from Heroku CLI.
I’ll show you the two methods. To add from the dashboard, go to your settings, and click on “reveal config vars.”
First, let’s add APP_DEBUG=true
. This will allow us to see the specific errors instead of the generic 500 || Server Error
.
If we refresh the page, we can now see what the actual error is. Since APP_DEBUG
has been enabled, Laravel is telling us what’s wrong: There’s no application key.
Copy the application key into your .env
file, and save it in Config Vars under settings, as we did for APP_DEBUG
.
APP_KEY=value (check your .env file for this)
Now, if we refresh again, we’ll see the application is up and running, as shown in the image below.
We need to set the rest of the application’s configuration variables. We’ll do this using the second method (via the Heroku CLI).
We’ll use the command heroku config:add ENV_CONFIG=value
. For instance, let’s add APP_NAME, APP_ENV and APP_URL
to the config variables.
heroku config:add APP_NAME=Heroku-Football-App (Use the name of your app here)
heroku config:add APP_ENV=production
heroku config:add APP_NAME= https://heroku-footballer-app.herokuapp.com/ (Use the url of your app here)
Config variables added
Let’s now take a look at our config variables on Heroku. You’ll see the variables have been added to the list of config variables already.
9. Create the database
Our Laravel application is now live on Heroku. This is, however, just half of the journey. We’ll need to connect our database to our application.
We’ll be using PostgreSQL, but you can use any database you want — MySQL, MongoDB, etc. I find PostgreSQL easy to implement on Heroku. It’s very similar to MySQL, which we used in our local environment. Plus, they have a very friendly free package. I’ve used the Hobby Dev plan, which is free, in this tutorial. You can also get the paid versions if you need more scalability and capability.
So to use the database, we’ll install it as an add-on.
PostgreSQL add-on selected
PostgreSQL add-on added
10. Get database credentials
We need to get the credentials of this database so we can integrate it with our application. This will give us the database login information, such as CONNECTION
, NAME
, PORT
, USERNAME
, PASSWORD
, and HOST
.
Luckily for us, it takes only a line of command on the Heroku CLI to get this done. This will bring out all the variables needed to set up your database. Type heroku pg:credentials:url
.
Database credentials
First, we need to add the DB_CONNECTION
as pgsql
; then we add the database config variables to our list of config vars.
Database config variables added
11. Migrate and seed your tables
Now that our database is set up, let’s run our migrations. To do this, run heroku run php artisan migrate
.
Migration done in production
On Heroku, under Resources, click the database. You’ll see the database now has all of the tables from our migration.
All of the tables have been added to the production database
Furthermore, to see all of the tables in our database and to perform SQL queries, click on the Dataclips menu.
View all tables
Click on “Create a new Dataclip.”
Creating a Dataclip
Let’s now seed our database with some data, as we did with the local version of our application.
heroku run php artisan db:seed
12. Test on Postman
You’ve made it. Our application is 100% completed. Now, let’s test it on Postman. We’ll test the same endpoints we tested in the local version.
Testing the ‘footballers’ endpoint in production
Testing the ‘footballers/{id}’ endpoint in production
13. Bonus step
In case you made a mistake or there are additions to your code, just do the following:
1. git add
2. git commit -m “message”
3. git heroku push master
4. And you’re done!
Thanks for reading.