How to deploy a Drupal 8 project to Heroku (Part 2)

After following the first part of this series (How to create a Drupal 8 project for Heroku (Part 1)) you should have a composer managed Drupal project, which uses Amazon S3 as the file storage, and takes basic configuration settings from environmental variables. Now we are going to deploy Drupal to Heroku.

In this guide we will:

  • # Setup local Git repository
  • # Link Drupal project to Heroku App
  • # Add additional Heroku dependencies
  • # Set environmental variables in Heroku
  • # Create the Procfile (commands file)
  • # Push and Install

Things you need before we start:

  • A Drupal project compatible with Heroku (see part 1)
  • Git installed locally
  • A Heroku account with an empty Heroku App
  • The Heroku CLI installed locally
  • An Amazon S3 bucket
  • An Amazon S3 user with read/write permissions for that bucket

All the commands executed from now on are done so in the project's directory, so open up a new terminal window and go there before continuing:

cd /my/projects/folder

Setup local Git repository 

In order for us to be able to deploy our project to Heroku, we need to have it committed to a git repo.

If you already have a git repo setup and have committed your changes, then skip to the next step, if not, execute the following commands to initialize your repo and commit your project:

git init
# Initialized empty Git repository in /my/projects/folder
git add .
git commit -m "Initial commit"
# Some git info about your commit.

With our project committed, we can now continue.

Link Drupal project to Heroku App 

We need to login to the Heroku account that will be used to deploy the project and link the current Drupal project to the Heroku App where it will be deployed.

heroku login
# ...
# Logged in as user@domain.com

heroku git:remote --app YOUR-HEROKU-APP
# set git remote heroku to https://git.heroku.com/your-heroku-app.git

What we just did is tell Heroku that whatever heroku commands we execute from within this folder and its children are related to the linked app.

Add additional Heroku dependencies 

At the time of writing this, Heroku didn't provide GD as a standard PHP library, and without it, we can't install Drupal. We specify it as a dependency using composer.

composer require "ext-gd:*"
# ./composer.json has been updated
# Loading composer repositories with package information
# Updating dependencies (including require-dev)
# Nothing to install or update
# Writing lock file
# Generating autoload files

Your composer.json file should then have the following lines:

    "require": {
        "ext-gd": "*",
        "..."
    },

And your composer.lock should have these lines:

    "platform": {
        "ext-gd": "*"
    },

These changes need to be committed to our repo:

git add composer.json composer.lock
git commit -m "add GD as a dependency"

# [master ########] add GD as a dependency
#  2 files changed, 5 insertions(+), 2 deletions(-)

Set environmental variables in Heroku 

Since we use environmental variables for some of our settings, we have to set their values in Heroku. That is done via the Heroku CLI "heroku config:set" command.

We need to pass each VAR=value set as an argument. Use the following command, replacing with your values as appropriate:

heroku config:set \
  HASH_SALT=RANDOM-STRING_WITHNU_MBERS-01234567890 \
  FLYSYSTEM_S3_KEY=YOUR_S3_KEY \
  FLYSYSTEM_S3_SECRET=YOUR_S3_SECRET \
  FLYSYSTEM_S3_REGION=YOUR_S3_REGION \
  FLYSYSTEM_S3_BUCKET=YOUR_S3_BUCKET \
  FLYSYSTEM_S3_PREFIX=YOUR_S3_SUB_FOLDER

# Setting HASH_SALT, ..., FLYSYSTEM_S3_PREFIX and restarting ⬢ your-heroku-app... done

Create the Procfile (commands file) 

Heroku uses a Procfile to declare what commands are to be executed by the application's dynos. We want to run Drupal under Apache in order to take advantage of Drupal's .htaccess files settings.

touch Procfile
echo "web: vendor/bin/heroku-php-apache2 web/" >! Procfile
git add Procfile
git commit -m "create Procfile"

# [master ########] create Procfile
#  1 file changed, 1 insertion(+)

Push and Install 

Now with everything ready, it is time to push our code to Heroku and install Drupal.

git push heroku master

# ...
# To https://git.heroku.com/your-heroku-app.git
#  * [new branch]        master -> master

And, done! You can now go to http://your-heroku-app.herokuapp.com and install Drupal.

March 30, 2017

Comments