How to specify env variables in config.json

Currently inside the config.production.json we have the following:

  "database": {
    "client": "mysql",
    "connection": {
      "host": "localhost",
      "user": "some-user-name",
      "password": "some-user-password",
      "database": "ghost_production"
    }
  },

It’s not OK to hardcode user and password values into config, so I want to use the environment variables to pass the values. What’s the correct syntax in the configuration to do that? Something like:

  "database": {
    "client": "mysql",
    "connection": {
      "host": "localhost",
      "user": "USER_ENV_VARIABLE",
      "password": "PASSWORD_ENV_VARIABLE",
      "database": "ghost_production"
    }
  },

I’ve found this blog post but it doesn’t show how this could be done. Thanks!

1 Like

If you want to pass data via env vars, you need to do so when spawning the process, not in the configuration file.

database__connection__password=5up3rs3cr3t node index.js

thanks! the env variable will override the value in the config, right?

1 Like

Would be good to know how to do this properly. A lot of incomplete threads on it.

1 Like

My previous post contains a link to the instructions to do it properly:

env variables will override the config variables that are supported in config.env.json

But are the config.development.json and/or config.production.json files required by ghost when using the env variables from docker? If so, then what is the minimum config.env.json that is required? I plan on supplying everything via docker.
Thanks.

At the bare minimum, you’ll probably want url, server, database, and paths

Must the config.env.json file still exist when using env vars? That’s my question :slight_smile:

I’m not sure if it has to exist, I’d suggest you try without a config file and see if it crashes :wink:

To clarify my comment, I think those are the minimum values you have to provide, either via env vars or the config file

No, you do not need even the config file as long as you can provide the minimum required parameters (database) using env vars. NCONF is smart enough to take the default values from Ghost, override it with config.env.json and env variables
I just tested this without a config.production.json file (not Docker, but local install). I just provided database connection values (tested with sqlite3 but should work the same with MySQL)
I tested both:

  1. Providing values on the command line :
    NODE_ENV=production database__connection__filename=“SQLITE_DB_FILE” database__client=“sqlite3”
  2. Setting env variables explicitly :
    export NODE_ENV=production
    export database__connection__filename=“SQLITE_DB_FILE”
    export database__client=“sqlite3”

You can provide env vars anyway and it should work. Once you provide the values, the rest will be set to default(url,paths,etc.). If you want to override the others, like mail__transport (default Direct),etc. feel free to do so.

1 Like

But does anyone know how to specify and call environmental variables if using ghost pro? Ideally, we should be able to set env vars within the ghost admin and then reference them in the json config file. I’m not sure why we would ever want to expose our secret passwords in a public json file. I’m using a git workflow with a custom GitHub integration that re-builds the site whenever I do a commit and push to my GitHub repo. But I shouldn’t be committing files to my repo that have secret passwords. Those should be set manually inside the ghost admin, but it seems as though Ghost is telling us that the way to do this is to expose our env vars in either the config json or the package json. Anybody know what I’m talking about and can provide assistance please? Thanks!

P.s.

I suppose having a way to upload our configuration file manually within the ghost admin would work too.

Ghost Pro is a managed hosting environment, meaning you won’t have fine-grained control of how Ghost is managed.

You might have more luck by creating a new thread that explains what you’re trying to do and why :slightly_smiling_face: