1

I'm relatively new to Craft 3, and am able to run Craft locally with a Postgres db and Valet as my server. When I try to deploy to Heroku, I run into issues with (I think) Postgres.

Background

I tried setting up Craft to work with Heroku by following this guide (see the "Publishing" section near the bottom of the guide). I found that if I just run a heroku create and then push my local branch to the remote server, I get an "Internal Server Error" when loading the remote site on Heroku.

Instead, I created an app.json file as per this guide by @edenspiekermann. Then I run https://heroku.com/deploy with arguments to create the Heroku app, and this allows me to access the Craft admin page at /admin/install on the remote server.

Following the first guide, I also push my local Postgres database to Heroku with heroku pg:push DB_NAME DATABASE_URL --app HEROKU_APP_NAME.

Questions

-How does Heroku know the right DATABASE_URL to use in /config/db.php? Locally, that variables comes from .env which is not pushed to my repo or Heroku.

-Why does Heroku show an internal server error if I just push my local branch to the remote server (whereas creating a Heroku app with https://heroku.com/deploy works)?

My db.php file:

preg_match('|postgres://([a-z0-9]*):([a-z0-9]*)@([^:]*):([0-9]*)/(.*)|i', getenv('DATABASE_URL'), $matches);

$user = $matches[1];
$password = $matches[2];
$server = $matches[3];
$port = $matches[4];
$database = $matches[5];

return [
  'driver' => "pgsql",
  'server' => $server,
  'user' => $user,
  'password' => $password,
  'database' => $database,
  'schema' => getenv('DB_SCHEMA'),
  'tablePrefix' => getenv('DB_TABLE_PREFIX'),
  'port' => $port
];

My Procfile for Heroku:

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

My .env file:

# The environment Craft is currently running in ('dev', 'staging', 'production', etc.)
ENVIRONMENT="dev"

# The secure key Craft will use for hashing and encrypting data
SECURITY_KEY=""

# The database driver that will be used ('mysql' or 'pgsql')
DB_DRIVER="pgsql"

# The database server name or IP address (usually this is 'localhost' or '127.0.0.1')
DB_SERVER="localhost"

# The database username to connect with
DB_USER="atc"

# The database password to connect with
DB_PASSWORD=""

# The name of the database to select
DB_DATABASE="blogdb"

# The database schema that will be used (PostgreSQL only)
DB_SCHEMA="public"

# The prefix that should be added to generated table names (only necessary if multiple things are sharing the same database)
DB_TABLE_PREFIX=""

# The port to connect to the database with. Will default to 5432 for PostgreSQL and 3306 for MySQL.
DB_PORT="5432"

DATABASE_URL="postgres://atc:@localhost:5432/blogdb"

DEFAULT_SITE_URL="https://landing-page.dev"

Error I receive when navigating to homepage on Heroku after I've pushed my local Postgres db to Heroku remote:

HTTP 503 – Service Unavailable – craft\web\ServiceUnavailableHttpException
1
  • Was able to figure out the answer to my two questions (by setting the ENVIRONMENT and SECURITY_KEY in Config Vars of the Settings dashboard in Heroku). How getting a HTTP 503 "Service Unavailable" error when navigating to the remote server :(
    – atc
    Sep 24 '19 at 0:51
1

Found out that there are a variety of Config vars in Heroku you need to set that match your local .env file. In particular:

  • ENVIRONMENT
  • SECURITY_KEY
  • DB_SCHEMA

Edit 10.1.19 Wrote this tutorial on how to get Craft 3 up and running on Heroku.

1

Craft/Yii on Heroku has been made much simpler with the addition of the url config option:

<?php return [
    // ...
    'production' => [
        'url' => getenv('DATABASE_URL')
    ]
    // ...
];

I think there are too many factors for us to say why the one-click deploy works, but pushing an existing application doesn't—it could be that one of the custom environment variables isn't populated (as it would be with Heroku's deploy/app wizard).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.