Deploying a Laravel application to Heroku with a MySQL database
This tutorial will enable you to deploy your Laravel 8 application with a MySQL database to GitHub and Heroku, saving you the time to deal with subsequent errors you might encounter if you were to follow their usual tutorials. as they skip over some little however important detail. that decides the success of your deployment.
I will assume you already have the Laravel installer and composer installed on your machine as well as a web server like Xampp, then you should also have a GitHub account and git installed, it comes with the bash CLI which I recommend you use for this tutorial.
Finally, have a Heroku account then install the Heroku CLI. they are the prerequisites for this given tutorial.
The Laravel section
1 in your command line, run "laravel new apoku --jet" and follow the command prompt.
$ laravel new apoku --jet
a choose your desired templating engine e.g livewire or inertia
b choose if you will be using a team or no team
2 if you chose inertia then npm install && npm run dev will be done automatically for you so you can skip it else you run the command below one after the other has been completed.
$ npm install
$ npm run dev
3 next create a database with a name in the phpmyadmin panel that corresponds to your app name in my case it will be "apoku" then run the command below, this will migrate all your base database information. without a database laravel applications won't work which is one of the main reasons laravel applications are difficult to deploy and set up on Heroku
$ php artisan migrate
by now you should see your laravel application active on your local development environment.
now it is time to deploy your project on GitHub then subsequently on Heroku.
The GitHub section
4 the first step is to initialize your project environment with the command below
$ git init
5 next add all your files using the following command "git add ." or "git add file.ext" if an individual file {read best practices on how to use git commands}
$ git add .
6 you need to commit your changes next with the command below
$ git commit -m "first commit"
7 add a branch to your local project
$ git branch -M main
8 if you haven't already created a repository on GitHub then now would be the best time [actually the last time] to do so to move forward, when you're done you copy the repo link then run the command like below [note: I use ssh so your link might be different. my apologies, I won't be covering the differences from here]
$ git remote add origin git@github.com:okmarq/apoku.git
9 then upload your code to GitHub with the command below
$ git push -u origin main
at this point, we are ready to initialize our Heroku environment for deployment.
The Heroku Section
10 the first step here is to sign in to Heroku via your CLI with the command below, follow the given instructions in the command prompt
$ heroku login
11 next you create a Heroku app with your project name like below or leave it blank for Heroku to give you a randomly generated project name [your project name should be unique or it will be rejected]
$ heroku create apoku
12 your next step is to push your code to Heroku where the build pack will be automatically detected and installed for you. in our case php
$ git push heroku main
opening the app on Heroku at this stage will give us a forbidden output as we don't have a Procfile.
13 in this case you create add a Procfile with no extensions then add the following line for an apache server "web: vendor/bin/heroku-php-apache2 public/" to the file as the content then stage, commit and push the changes to Heroku.
now you should see an "HTTP 500 Internal Server Error".
you should take note of the "public" in the Procfile, Heroku might give you "web" which is just a generic indication, this will lead you to see an application error which from here on out, any other error will now begin to look painful to fix which is the very reason for this tutorial itself so follow it closely.
14 add a database from your Heroku dashboard. specifically, use a JawsDB MySQL addon. the reason for this is so you can have easy access to all the separate parameters we will be needing to connect to the database, the Host, Username, Password, Port, and Database.
15 your next step is to use your CLI to add the following parameters to your Heroku config vars. run the following commands below one after the other has completed
$ heroku config:set APP_NAME=apoku
$ heroku config:set APP_KEY=base64:138V/prcrPWnpMdyl9kPmRPug4s133odCrWuFl/KOas=
$ heroku config:set APP_DEBUG=true
$ heroku config:set APP_URL=http://apoku.test
$ heroku config:set DB_CONNECTION=mysql
$ heroku config:set DB_HOST=j21q532mu148i8ms.cbetxkdyhwsb.us-east-1.rds.amazonaws.com
$ heroku config:set DB_PORT=3306
$ heroku config:set DB_DATABASE=kvqb74eik8x9hi68
$ heroku config:set DB_USERNAME=hksxxqiwg639dry3
$ heroku config:set DB_PASSWORD=oczq10yi11woi5e9
you're almost there.
16 your next step is to migrate your database. while I'll be using the CLI to do this step, you can do this directly from your heroku dashboard.
$ heroku run bash
upon running the above command, a bash interface will be activated after which you will run the following command in it.
$php artisan migrate
very nice, running a bash interface from a bash cli. cool huh?
well, congratulations on making it this far, you should pat yourself on the back, your application should be up and running right now, assuming you followed the instructions.
If you still have an error and you are using a laravel application, then read through again, to ensure you used "public" and not "web" in your Procfile.
Top comments (3)
I have a project in laravel with a MySQL database, but when saving the images it doesn't work, it gives me an error. Any ideas?
Hello Reyes, sorry for the late reply.
I will need more context on what you hope to achieve to be able to provide a better answer.
when saving images to database, you need to have the images uploaded to your server while you store the name in a database.
Heroku doesnt allow you to save images or upload files to their server