Deploy a Laravel Project on Heroku with SQL Database
Tags: laravel, heroku, deployment
Published: 2022-01-27 20:31:13

Heroku is a popular Platform-as-a-Service (PaaS) that enables developers to deploy and run their Web or API applications in the cloud. Heroku offers a free plan making it easy for developers to host an application online for testing or cases where you don't need to buy a hosting service just yet. Heroku supports several languages such as Ruby, Java, Node.js, Scala, Clojure, Python, PHP and Go making it a good option among developers for hosting their projects for testing.

In this article, we'll be going over a step-by-step approach to deploying a Laravel project on Heroku.

### Requirements
To follow through with this article, you need to have the following setup:
- [Git](https://git-scm.com)
- A Laravel project ready for deployment; if you don't have one yet, [you can clone this sample project from Github](https://github.com/steelze/laravel-heroku-deployment)
- A [Heroku](https://www.heroku.com/) account, and
- Heroku command-line interface (CLI) installed on your machine. You can find instructions on installing the CLI [here](https://devcenter.heroku.com/articles/heroku-cli).

To begin, open the terminal, navigate to the Laravel project directory location and type in the command below to login into your Heroku account.

```
heroku login
```

The command will open up your browser to log into your Heroku account.

![Screenshot 2022-01-18 at 09.38.17.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1642495824704/RTohi9dwK.png)

Once you are logged in successfully, you should see a page similar to the one below.

![Screenshot 2022-01-18 at 09.49.08.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1642496056011/T4A5_Trno.png)

To get our Laravel application on Heroku, we will use Git version control. Initialize `git` in the Laravel project if `git` is not initialized else, you can skip this step.

```
git init
git add .
git commit -m"Initial commit"
```

Create a file at the root of the Laravel project named Procfile. A Procfile is a simple text file without a file extension that specifies the application's commands on startup in Heroku. The Procfile must be placed in the root directory as it would not function in any other location.

Copy and paste the below in the Procfile. The command launches an Apache server with PHP installed.

```
web: vendor/bin/heroku-php-apache2 public/
```

Commit the changes

```
git add .
git commit -m"Heroku Procfile"
```

### Create Heroku Application

To create an application on Heroku, we can use the `heroku create <app-name>` command. The command will create an application on Heroku and generate a URL to access the application; however, you will have to supply a new unique name if the name already exists. Also, note that if no `<app-name>` is provided, Heroku will generate a random name.

```
heroku create laravel-heroku-deployment
```
The above command  creates an application called `laravel-heroku-deployment`, as seen on my dashboard 

![Screenshot 2022-01-18 at 11.44.54.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1642502711217/EBAy6jz8I.png)

and generates the URL [https://laravel-heroku-deployment.herokuapp.com](https://laravel-heroku-deployment.herokuapp.com) to access the application.


![Screenshot 2022-01-18 at 17.28.26.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1642523324371/8TtknsgZd.png)

Next, we use the git push command to push our code to Heroku, which will install the project dependencies.

```
git push heroku main
```

Visit the Heroku application URL generated, and we should see an error page which we will fix in the next section.


![Screenshot 2022-01-18 at 19.16.01.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1642529781707/hWaiF_CEe.png)

### Setting Environment Variables

We had the 500 error after pushing our code in the previous section because the environment variables in the `.env` file needed for our application to work are not on Heroku. Environment variables can be added in two ways:

#### Using the Heroku CLI

To set environment variables using the Heroku CLI, we will be using the `heroku config:add <ENV_NAME>=<ENV_VALUE>` command.

```
heroku config:add APP_DEBUG=true
```
To confirm the environment variable is set, click the application on your Heroku dashboard, click on the settings menu, and click on **"Reveal Config Vars"**. The variable has been added to the list of config variables.

![Screenshot 2022-01-18 at 18.43.00.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1642527896788/UWq4oMG06.png)

Visit the application URL, and we can see what the error is since `APP_DEBUG` is enabled.

![Screenshot 2022-01-18 at 19.17.20.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1642529945551/kyeHWgbK2.png)

#### From the Heroku dashboard

The second method of adding environment variables to our application is by adding variables on the **"Reveal Config Vars"** section. Let's add the `APP_KEY` variable using this section. Go to your terminal and run the command.

```
php artisan key:generate --show
```

and copy the output and paste as the value of `APP_KEY` on your dashboard.

![Screenshot 2022-01-18 at 19.26.08.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1642530375203/qx9pSo3q7.png)

Now our application should be running 😎.

![Screenshot 2022-01-18 at 19.31.58.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1642530731767/Y9Ww0CZIx.png)

### Connect Database

Now that our Laravel application is running on Heroku, we can connect a database to the application. In this article, we will be using PostgreSQL as Heroku supports PostgreSQL natively. Note that you can use any other database like MySQL etc., through Heroku addons or use third party databases such as [db4free](https://db4free.net/).

To get started, click on the **Resources** menu and in the addons section, search for **Heroku Postgres**


![Screenshot 2022-01-18 at 20.19.33.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1642536430961/_UIyUWSLk.png)

and select the **Hobby Dev plan** which is free.

![Screenshot 2022-01-18 at 20.20.44.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1642537020194/6EHb-haje.png)

Once the addon `heroku-postgresql` is attached, a new environment variable `DATABASE_URL` is automatically added to the Heroku application. You can confirm this by checking the **Config Vars** section. Also, add a new environment variable `DB_CONNECTION` and set its value to **pgsql**

![Screenshot 2022-01-18 at 21.36.48.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1642538470390/9taLpHQ48.png)

To manually view the database credentials, Click on the attached PostgreSQL addon, navigate to the settings page and click the **View Credentials**.

![Screenshot 2022-01-18 at 21.29.52.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1642537952921/EjKv0QhwE.png)

To run any of Laravel's artisan command precede all statements with `heroku run`. Paste the below in your project terminal to migrate all tables.

```
heroku run php artisan migrate:fresh
```
If you cloned the sample project on Github, you could run the `db:seed` command to seed the `posts` table and visit the `/posts` route to see the seeded posts.

```
heroku run php artisan db:seed
```

### Conclusion
Voila 😄! Now you can deploy your Laravel applications on Heroku for free without sweating over server costs. To learn more about Heroku, check the [Heroku Docs](https://devcenter.heroku.com/categories/reference).  Thanks for taking the time to read this article. I hope it helps you. Happy Coding 😎!