Deploy Laravel 7 to Heroku 2020

andyyou
2 min readAug 14, 2020

Again, this post is a memo and hope it can help you and myself complete the thing as soon as possible.

Prerequisites

This post assumes you have familiar with PHP, Laravel. Before you actually deploy the Laravel project to Heroku, there are few things you should complete first. For more detail you can visit the resources list as follow even some of them are outdated but still can help you to set up the environment:

1. Login your Heroku

# If you have signed the Heroku account you can check your account again
$ heroku auth:whoami
$ heroku login

2. Create Procfile

# In Laravel project root directory
$ echo "web: vendor/bin/heroku-php-apache2 public/" > Procfile

3. Create a Heroku app

$ heroku apps:create [YOUR_APP_NAME]

Note, this command will add git repository for you or you can git remote add by yourself.

4. Set buildpacks

$ heroku buildpacks:set heroku/php
$ heroku buildpacks:add heroku/nodejs

5. Generate & Set APP_KEY

$ heroku config:set APP_KEY=$(php artisan --no-ansi key:generate --show)

6. Edit package.json

"scripts": {
"postinstall": "npm run prod"
}

If you need to figure out the reason for npm errors. You can enable verbose for npm

$ heroku config:set NPM_CONFIG_LOGLEVEL=verbose

If you meet errors that can not compile successfully. Try add name and version in package.json

If you have deploy the project and need to compile js and CSS you can

$ heroku run npm run prod

7. Deploy

$ git push heroku master

8. Create database & exec migrations

# 1. Here I use postgresql as example you can add resource on Heroku web.
$ heroku addons:create heroku-postgresql:hobby-dev
# 2. Get database information to set environment variables
$ heroku config
# You should get URL like
# postgres://[USERNAME]:[PASSWORD]@[HOST]:[PORT]/[DATABASE]
# Example: postgres://vijrtzeslbxrxx:201d1a7e095cdd79a183f5cf5779c5a15fb0e28bff87d9537e3986747a890150@ec2-50-20-216-61.compute-1.amazonaws.com:5432/djff1100pm43tm
$ heroku config:set DB_CONNECTION=pgsql
$ heroku config:set DB_HOST=
$ heroku config:set DB_PORT=5432
$ heroku config:set DB_DATABASE=
$ heroku config:set DB_USERNAME=
$ heroku config:set DB_PASSWORD=
# 3. Run migration
$ heroku run php artisan migrate

Class ‘Barryvdh\Debugbar\ServiceProvider’ not found

If you set up Debugbar and meet these errors. Please remove Barryvdh\Debugbar\ServiceProvider::class, in providers of config/app.php.

Class ‘Faker\Factory’ not found

If you need to run db:seed on production mode, you should move "fzaninotto/faker": "~1.4" from "require-dev" to "require" in composer.json. Then

$ composer install
$ composer update
$ git add
$ git commit
$ git push heroku master

Memory limit errors

If you meet memory limit errors when install league/flysystem-aws-s3-v3 you can use command as follow:

$ COMPOSER_MEMORY_LIMIT=-1 composer require league/flysystem-aws-s3-v3

Resources

--

--