Deploy the Laravel App to Heroku with Database connection

Tram Ho

In today’s post, I will show you how to deploy a Laravel app to Heroku using git and Heroku CLI. Heroku is a cloud platform that allows programmers to build, deploy, manage, and scale applications, it is very flexible and easy to use, providing a simplest path to reach products. user.

However, Heroku is only suitable for simple apps, the database is not too complicated, few users and for you to use for testing. If you want to build large apps and users will have a good experience, you should deploy to better Hosting.

Let’s get started!

1. Install Heroku CLI

The Heroku CLI (Command Line Interface) is a simple way for them to create and manage your Heroku apps through the terminal. Below I will install Heroku CLI for Ubuntu 16+, if you use another operating system, you can refer here.

After installation check to see if Heroku has been successfully installed

2. Install the Laravel app

Still a familiar command that all Laravel learners know (but not sure to remember)

Here I installed version 5.8 because I found it quite stable, there are few trivial ways about version

3. Create Procfile

In the laravel app’s root folder, create a Procfile file. In the Procfile file we will specify the command to be executed by the app on launch.

4. Install git and commit to the app

5. Login Heroku through Heroku CLI

To perform this step, you must have a Heroku account. If you don’t have one, please sign up https://signup.heroku.com/ . Once you have an account, execute the command

After entering, a question will appear: heroku: Press any key to open up the browser to login or q to exit: press any word and then enter to continue. When the browser is opened, press Log in.

Here are the results after logging into Heroku with CLI

6. Create app on Heroku

After successfully logged in, we proceed to create Heroku app by CLI

A repository has been successfully created

When I check git remote -v , the remote heroku is created in the app

7. Deploy code to the newly created repo

Now just push the code to the repo just created with the command

App has been released successfully

8. Configure env for the project on Heroku

This part you can go to the settings of the app you just created on Dashboard Heroku . Create the Config Vars section you add keys to the Laravel app

On the Terminal, execute the heroku open command to open the app, and below is the result after we deploy

9. Configure Https for the app

After scrolling through the pages of the app, I see that only the root page can be https rest are http , so we will reconfigure so that all of them switch to https

In the AppServiceProvider.php file we will add the register() function as shown below

After changing the code, you also have to commit and push the code back up as before!

And config for the app in APP_ENV settings by Production . Reopen again to test the app!

Make an SQL connection to the app

Definitely a dynamic app must have Database already, below are the steps to connect Database on Heroku

Install Heroku Postgres and reconfigure for the App

In the resources section, we find Heroku Postgres and install it into the app

Once installed, click on the package. Settings to Settings and View Database Credentials for Database information and then configure the env app.

Note, in DB_CONNECTION will be pgsql , not mysql

Perform the migration

Command heroku apps to check which apps are on your Heroku

Command heroku run bash --app name-your-app to access the app through the CLI

Now you can execute Laravel commands right on Heroku CLI

And this is the result

summary

Above are the steps to deploy a Laravel app to my Heroku, the article is still sketchy, so I hope everyone can give me suggestions for better development. Thank you everyone for watching the article, wish everyone success

Here is the website link that I have deployed to Heroku, you can refer to: https://shielded-stream-69901.herokuapp.com/

Share the news now

Source : Viblo