Introduction

The Command Line Interface (CLI) allows developers to use default commands provided by Magento modules. In this blog post, we will guide you on how to change Magento 2 configuration from the CLI command.

The prerequisites

  • The configuration path
  • The scope code, to set a configuration value for a particular scope. 
  • Log in to your account using SSH and change to the Magento root directory.

Set configuration values

  • To display the value of configuration settings
php bin/magento config:show [--scope[="..."]] [--scope-code[="..."]] path
  • To set system-specific configuration values on Magento 2.2.0 - 2.2.3, use: 
php bin/magento config:set [--scope="..."] [--scope-code="..."] [-l | --lock] path value
  • To set system-specific configuration values on Magento 2.2.4 and higher, use: 
php bin/magento config:set [--scope="..."] [--scope-code="..."] [-le | --lock-env] [-lc | --lock-config] path value
  • To set sensitive configuration values, use: 
php bin/magento config:sensitive:set [--scope="..."] [--scope-code="..."] path value
  • The config path default is <section_id>/<group_id>/<field_id> or the custom path that you config on <config_path> tag.
  • If the path is incorrect, this command returns an error. The “wrong/config/path” does not exist

Where

--scope - Optional - The scope of the configuration. The possible values are default, website, or store. The default is default.

--scope-code - Optional - The scope code of configuration (website code or store view code).

-le or --lock-env - Optional - Either locks the value so it cannot be edited in the Magento Admin or changes a setting that is already locked in the Magento Admin. The command writes the value to the <Magento base dir>/app/etc/env.php file.

-lc or --lock-config - Optional - Either locks the value so it cannot be edited in the Magento Admin or changes a setting that is already locked in the Magento Admin. The command writes the value to the <Magento base dir>/app/etc/config.php file. The --lock-config option overwrites --lock-env if you specify both options.

path - Required - The configuration path.

value - Required - The value of the configuration.

Clear cache

  • After running your commands, clear cache config to apply your changes
php bin/magento cache:clean config
  • To check your command is working or not, run this command 
php bin/magento config:show [--scope[="..."]] [--scope-code[="..."]] path

or check it on Site Admin >> Stores >> Configuration

Example

  • Display the configuration of the default Web URL
php bin/magento config:show --scope="stores" --scope-code="default" web/default/front
  • Set the default Web URL for the default scope
php bin/magento config:set web/default/front https://store.magenest.com/
  • Set the default Web URL for the base website
php bin/magento config:set --scope=websites --scope-code=base web/default/front https://store.magenest.com/
  • Set the default Web URL for the test store view
php bin/magento config:set --scope=stores --scope-code=test web/default/front https://store.magenest.com/
  • View the change

Conclusion

We at Magenest hope that this article contains the answer you have been looking for. If it doesn't, or you still have more technical problems, please don't hesitate to drop us some emails at support@magenest.com