Magento 2 CLI: Change Store Configuration with config:set (Scopes + Examples)

How to change Magento 2 configuration using Command Line Interface?

Use bin/magento config:show to read a config value and bin/magento config:set to update it. Add --scope and --scope-code when you need website or store-view specific values, then apply changes by clearing the config cache.

Magento 2 provides a Command Line Interface (CLI) that lets you change configuration values directly on the server. This is ideal for quick fixes, scripted deployments, CI/CD pipelines, and troubleshooting issues without clicking through Admin screens.

If you want help making production-safe changes (base URLs, payments, shipping, performance tuning), hire a Magento developer from Magespark.

Prerequisites

  • SSH access to your server and the Magento root directory (where bin/magento exists).
  • Correct execution user (avoid permission problems by running CLI as the same user that owns Magento files).
  • Configuration path you want to read or change (example: web/secure/base_url).
  • Scope understanding: values can be set at default, websites, or stores scope.

Read a configuration value (config:show)

To display the current value for any config path, run:

php bin/magento config:show [--scope="default|websites|stores"] [--scope-code="..."] path

Examples

# Show a value from default scope
php bin/magento config:show web/secure/base_url

# Show a value for a specific store view code (stores scope)
php bin/magento config:show --scope="stores" --scope-code="default" web/secure/base_url

Set a configuration value (config:set)

To update a configuration path with a new value, run:

php bin/magento config:set [--scope="default|websites|stores"] [--scope-code="..."] path value

Tip: If you omit --scope and --scope-code, Magento writes the value at the default scope.

Locking values (env.php and config.php)

You can lock values so they cannot be edited from the Admin panel. This is helpful for consistent deployments.

Magento 2.2.4+ (most modern setups)

php bin/magento config:set [--scope="..."] [--scope-code="..."] [-le | --lock-env] [-lc | --lock-config] path value
  • --lock-env (-le) writes to app/etc/env.php
  • --lock-config (-lc) writes to app/etc/config.php
  • Use locking when you want configuration controlled by code and deployment, not manual Admin edits.

Magento 2.2.0–2.2.3

php bin/magento config:set [--scope="..."] [--scope-code="..."] [-l | --lock] path value

If the path is incorrect, Magento returns an error such as: The "wrong/config/path" does not exist.

Set sensitive values (config:sensitive:set)

For secrets like API keys or passwords, use the sensitive config command:

php bin/magento config:sensitive:set [--scope="default|websites|stores"] [--scope-code="..."] path value
  • Avoid exposing secrets in shell history or logs.
  • Wrap values in quotes if they contain special characters or spaces.

What do scope and scope-code mean?

  • --scope (optional): where the value applies. Valid values: default, websites, stores.
  • --scope-code (optional): the website code or store view code (required when scope is websites or stores).
  • path (required): the config path, for example web/secure/base_url.
  • value (required): the new value to set.

Clear cache (apply your changes)

After updating configuration, clear the config cache:

php bin/magento cache:clean config

If you are troubleshooting and want to be extra safe, you can flush all caches:

php bin/magento cache:flush

Verify the change

Re-check the value using config:show:

php bin/magento config:show [--scope="..."] [--scope-code="..."] path

You can also verify it in Admin: Admin > Stores > Configuration.

Example: Change Magento 2 Base URL via CLI

Display the current secure base URL

php bin/magento config:show web/secure/base_url

Set the secure base URL at default scope

php bin/magento config:set web/secure/base_url "https://store.magespark.com/"

Set the secure base URL for a specific website

php bin/magento config:set --scope="websites" --scope-code="base" web/secure/base_url "https://www.magespark.com/"

Set the secure base URL for a specific store view

php bin/magento config:set --scope="stores" --scope-code="test" web/secure/base_url "https://www.magespark.com/"

Apply changes

php bin/magento cache:clean config

FAQ

What is the difference between default, websites, and stores scope?

Default applies globally. Websites applies to a website (use the website code). Stores applies to a store view (use the store view code). The most specific scope overrides broader ones.

Why doesn’t config:set apply immediately?

Magento caches configuration. After running config:set, clear the config cache using php bin/magento cache:clean config (or flush cache if needed).

Where do locked configuration values get stored?

--lock-env writes into app/etc/env.php. --lock-config writes into app/etc/config.php. Locked values typically can’t be changed from Admin.

How do I set API keys or passwords from CLI?

Use bin/magento config:sensitive:set and avoid exposing secrets in shell history or logs.

Want us to implement production-safe changes, speed optimizations, or deployment improvements? Talk to Magespark.

Talk to a Hyvä expert
Loading...