Magento 2 Developer Essentials: Comprehensive CLI Command Reference

Likes to develop Magento 2 developers, today I've put together a list of the correct Magento CLI commands for you to use as a reference. So far, there doesn't seem to be a centralized resource for a complete list of Magento 2 commands, and searching for the right commands on different websites can waste a lot of time.

Our team encountered this when we first started!

Magento 2's Command Line Interface (CLI) provides a convenient and efficient way to perform a variety of tasks, including installing the platform, managing modules, themes, extensions, and configuring the store.

Using CLI commands saves time and effort while enhancing the Magento 2 development experience.

In this article, we have compiled a comprehensive list of Magento 2 CLI commands that can be referred to whenever needed. Also, some tips on how to add custom CLI commands in Magento 2 will be shared.

图片[1]-Magento 2 CLI 命令详解:全面指南与实用示例

What is the Magento 2 CLI?

A command line interface (CLI) is a text-based user interface (UI). Just as driving a car involves interacting with various controls such as the steering wheel, pedals, and dashboard, the Magento 2 CLI allows you to interact with a computer or application via text commands.

For example, the CLI can be used to run programs, manage computer files, and configure settings, just as a driver controls the functions of a car to drive, accelerate, brake, and adjust car settings.

Magento 2 provides a command line interface for performing installation and configuration tasks, including cache management, installing extensions, clearing the cache, and more.

A specific example of using the Magento 2 CLI is clearing the cache. This is similar to a driver needing to remove dust or debris from the windshield of a car to ensure a clear view. This is accomplished by using the cache:clean commandYou can delete the cached data that prevents you from viewing your Magento 2 site and see the changes immediately.

How to access files via Magento CLI

To access Magento 2's command line interface (CLI), have SSH (Secure Shell Protocol) access to the server.

Attention: Once you have SSH access, you can log in to your server using a terminal emulator such as PuTTY (for Windows) or Terminal (for macOS and Linux).

图片[2]-Magento 2 CLI 命令详解:全面指南与实用示例

Follow the steps below to access and run the Magento CLI:

The general directory structure of Magento is as follows and can be used to access applications on the server depending on the operating system:

cd /bin

Example:

  • Ubuntu: cd /var/www/magento2/bin
  • CentOS: cd /var/www/html/magento2/bin

Log in to your server and access SSH: Click SSHOpen an SSH terminal, or use another terminal to access SSH.

图片[3]-Magento 2 CLI 命令详解:全面指南与实用示例
图片[4]-Magento 2 CLI 命令详解:全面指南与实用示例

Enter the credentials:Under Master Credentials, enter the credentials, i.e. username and password.

图片[5]-Magento 2 CLI 命令详解:全面指南与实用示例

Access the Magento application and its public folder:

图片[6]-Magento 2 CLI 命令详解:全面指南与实用示例

Now, located in the public directory of your Magento 2 application, you can run anybin/magentoCommand.

The Magento CLI is located in each Magento installation's/bin/directory. It can be accessed from the Magento root directory using the following command:

php bin/magento

Attention:Using "php" before bin/magento is optional, but it only works with the latest versions of Magento 2. However, this only applies to the latest version of Magento 2. Even if you omit it, the operating system will automatically find the global PHP variable to execute the command.

Run the Magento 2 CLI command

To run any command, first access the server's Command Line Interface (CLI) and navigate to the Magento 2 application's public directory. This is done as follows:

Add steps on how to navigate to the public directory of your Magento 2 application here.

To run specific Magento 2 CLI commands, run the following commands:

bin/magento [command name] [parameters]

Substitute: 

  • [command-name] is the name of the Magento 2 CLI command.
  • [Parameters] Contains any required or optional parameters for the command.

For example, to flush the Magento 2 cache, run the following command:

bin/magento cache:flush

Magento 2 Common Commands List and How to Use Them

Magento 2 CLI commands can be accessed from the server or from the command line interface of the local environment. All Magento Command Line Interface (CLI) commands depend on the Magento application and must be able to access its context, dependency injections, plugins, etc.

Magento 2 CLI command structure:

The following is the basic structure of the Magento 2 CLI commands:

bin/magento [Options] [Arguments]

In this command structure:

  • bin/magento is the path to the Magento 2 CLI executable.
  • is the name of the CLI command you want to run.
  • [] are optional arguments that can be passed to the command.

Example: php bin/magento indexer:reindex

List all commands:

To first check the list of all commands available in Magento 2, run the following command:

bin/magento list
图片[7]-Magento 2 CLI 命令详解:全面指南与实用示例

The bin/magento CLI commands can be invoked using shortcuts instead of full command names. For example, bin/magento setup:upgrade can be invoked using bin/magento s:up, bin/magento s:upg.

Here are some useful and common Magento commands for managing Magento applications:

Magento 2 Caching Commands

As the name suggests, this Magento 2 command manipulates Magento's caching options. You can use the following commands to enable, disable, clean, refresh, or check the status of the Magento cache:

  • Command:magento cache:{enable/disable/clean/flush/status}
(computing) cachecommandshortcutsdescriptive
bin/magento cache: statusbin/magento c:sList cache types and their status
bin/magento cache: enabledbin/magento c:eEnable all caches or any specific cache type
bin/magento cache: disabledphp bin/magento c:dDisable all caches or any specific cache type
bin/magento cache: cleanphp bin/magento c:dIt will only remove cache types linked to Magento
bin/magento cache: refreshbin/magento c:fDelete all caches from the same store

Magento 2 Indexer Commands

Indexer is a widely used option in Magento 2. Previously there wasn't any way to manually update the indexers, but now it is also possible to do so through Magento's admin panel. In addition to reindexing with the Magento reindex command, there is an option to set the indexing mode and check the status of the indexer via the Magento CLI.

  • Command:bin/magento indexer: {status/show-mode/set-mode/reindex/info/reset/show-dimensions-mode/set-dimensions-mode}

Example:

indexercommandshortcutsdescriptive
bin/magento indexer: infobin/magento indexer:infoShow list of all indexers
bin/magento indexer: statusbin/magento i:staView the status of all indexers or only specific indexers
bin/magento indexer: setting the schemabin/magento i:setReindex all indexers or only specific indexers
bin/magento indexer: reindexingbin/magento i:reiInvalidates all indexers or specific indexers
bin/magento indexer: display modebin/magento i:shoView the current configuration of the indexer

Magento 2 Administrative Commands

Using the Magento 2 Admin command, you can create new administrator users and edit or unlock users that have been created through the Magento CLI.

Command:

  • bin/magento administrator: user: {create/unlock}
  • bin/magento admin:user:create [-=, ...]

Example:

executive (adjective)commandshortcutsdescriptive
bin/magento administrator:user:createbin/magento a:u:cCreate Administrator
bin/magento admin: user: unlockedphp bin/magento a:u:uUnlock Administrator Account
bin/magento administrator: adobe-ims: statusStatus of the Adobe IMS Module
bin/magento admin:adobe-ims:info Adobe IMS Module Configuration Information
bin/magento admin: adobe-ims: enableEnabling the Adobe IMS Module
bin/magento administrator: adobe-ims: disabledDisabling the Adobe IMS Module

Magento 2 Module Commands

This Magento command can be used to enable, disable, uninstall or check the status of all installed modules via the Magento CLI. Disabling or uninstalling a module using this Magento 2 command will fail if the module has any dependencies.

  • Command:bin/magento module:user:{status/enable/disable/uninstall}

Example:

module (in software)commandshortcutsdescriptive
bin/magento module: statusbin/magento mo:sChecking the Status of Magento Modules
bin/magento module: enablebin/magento mo:eEnabling Specific Magento Modules
bin/magento module: disablebin/magento mo:dDisabling Specific Magento Modules
bin/magento module: uninstallbin/magento m:uUninstalling specific Magento modules

Magento 2 Cron Commands

This Magento 2 command manually executes scheduled Magento Cron jobs. This command can be placed in a scheduler to execute application tasks in a timely manner.

Command:

  1. magento cron: run

Example:

Croncommandshortcutsdescriptive
bin/magento cron: installbin/magento c:inGenerate and install crontab for the current user
bin/magento cron: deletebin/magento c:reDeleting tasks from crontab
bin/magento cron: runbin/magento c:ron schedule

Magento 2 Maintenance Commands

Magento maintenance commands can help you keep your website running smoothly. Here are some of the most useful maintenance commands.

  • command: bin/magento maintenance: {status/enable/disable/allow-ips}

Here are some useful commands:

safeguardcommandshortcutsdescriptive
bin/magento maintenance: allow ipsbin/magento m:aSetting the Maintenance Mode Exemption IP
bin/magento maintenance: disabledbin/magento ma:dDisable maintenance mode
bin/magento maintenance: enabledbin/magento ma:dEnable Maintenance Mode
bin/magento maintenance: statusbin/magento ma:sDisplay Maintenance Mode Status

Magento 2 Information Commands

By using the Magento 2 Information command, users can gain insight into information about their website and the system that runs it.

  • command: bin/magento info:{adminuri/backups/currency/dependencies/language}

Here are some useful information commands:

textcommandshortcutsdescriptive
bin/magento Info: adminuri bin/magento i:aShow Magento Management URIs
bin/magento Information: Backups: Lists bin/magento i:b:lPrint a list of available backup files
bin/magento Information: Currency: Lists   bin/magento i:c:lDisplay a list of available currencies
bin/magento Info: Dependencies: Display Frameworkbin/magento i:d:show-fDisplay the number of dependencies for the Magento framework
bin/magento info: dependencies: display module  bin/magento i:d:show-modulesDisplay the number of dependencies between modules
bin/magento info: dependencies: show module loop  bin/magento i:d:show-circularDisplay the number of circular dependencies between modules
bin/magento Information: Language: Lists bin/magento i:l:lDisplays a list of available locale settings
bin/magento Info: Time zone: Listings bin/magento i:t:lDisplay a list of available time zones

Magento 2 setup commands

There are many things you can do with the Magento setup command.

Here are some useful commands:

set upcommandshortcutsdescriptive
bin/magento setup: configuration: settings bin/magento s:c:sCreating or Modifying Deployment Configurations
bin/magento setup: di: compile bin/magento s:d:cGenerate DI configuration and all missing classes that can be automatically generated
bin/magento setup: upgrade bin/magento s:upUpgrading Magento applications, database data and architecture
bin/magento setup: uninstallbin/magento s:unUninstalling the Magento Application
bin/magento setup: install bin/magento s:iInstalling the Magento Application
php bin/magento settings: static content: deploymentphp bin/magento s:s:dDeploying static view files

Backup command

This Magento CLI command backs up your application's code, database, and media files. The backup can be found in var/backup/directory in the web root directory.

There's also a complete guide on how to backup your Magento store in just a few steps.

Command:

magento setup:backup --code --media --db

Configuration Commands

This option can be used to change the configuration in Magento. Changes relate to database parameters, administration URLs, etc. You can see the values provided in the app/etc/env.php file.

Command:

magento setup:config:set --backend-frontname="admin" --db- host="localhost" --db-name="example" --db-user="example" --db-password="example"

Other Magento 2 Setup Commands

  • To generate data for performance testing

Command:

  • magento setup:perf:generate-fixtures
  • bin/magento setup:perf:generate-fixtures {path to profile}

Example:

bin/magento setup: perf: generate fixtures /var/www/html/magento2/setup/performance-toolkit/profiles/ce/test.xml

  • To generate data, patch and declaration

Command:

  • setup:db-declaration:generate-patch
  • setup:db-declaration:generate-whitelist
  • setup:db-schema:upgrade
  • setup:db-data:upgrade

Magento 2 Deployment Commands

Magento 2 provides a series of CLI commands for easily deploying static content for the store. This content includes CSS, JavaScript, HTML files, images, fonts, and various media resources. These commands are also widely used when upgrading versions of Magento 2 or implementing security patches.

The following are examples of some common deployment commands:

Deployment commands(computer) shortcutdescriptive
bin/magento deploy:mode:setbin/magento d:m:setSetting the application mode
bin/magento deploy:mode:showbin/magento d:m:shoDisplay the current application mode

Magento 2 Theme Commands

Magento 2 has a powerful theme system that allows you to create custom themes or modify existing themes using HTML, CSS, JavaScript and XML files.

Command:

php bin/magento theme:uninstall

Shortcuts: 

php bin/magento t:u

Other useful Magento 2 commands

In addition to the previously mentioned commands, there are many other useful CLI commands in Magento 2, which we will explore below.

To generate a translation dictionary:

Command:

  • magento i18n:{collect-phrases/pack/uninstall}
  • bin/magento i18n:collect-phrases [-o|-output=""] [-m|-magento]
  • bin/magento i18n:pack [-m|-mode={merge|replace}] [-d|-allow-duplicates]

Example:

magento i18n:collect-phrases

To create CSS from LESS:

Command:

  • magento dev:source-theme:deploy
  • bin/magento dev:source-theme:deploy [-type="..."] [-locale=" ..."] [-area="..."] [-theme=" ..."] [file1] ... [fileN]

Example:

bin/magento dev:source-theme:deploy -type="less" -locale="en_US " -area="frontend" -theme="VendorName/themeName" css/styles-l

Run the specified automated tests:

  • Command:bin/magento dev:tests:run

Example:

bin/magento dev:tests:run integration

Displays a list of tests:

Example:

bin/magento dev:tests:run -help

To update the layout XML file:

  • Command:bin/magento dev:xml:convert

To install the sample data on a Magento application:

  • Command:magento sampledata:install

Used to manage the back-end configuration:

  • Command: magento config: {set/sensitive: set/show/}

To check the full list of Magento commands:

  • Command:bin/magento list

To get help with any particular command:

  • Command: bin/magento help

Example:

bin/magento help cache:enable

Fix Magento 2 commands not working

There can be various reasons why Magento 2 commands are not working. You can try some of the following troubleshooting steps:

If any commands are being run but nothing is returned, you can follow the options below to fix the issue:

Checking file permissions

Ensure that files and directories in your Magento 2 installation have the correct permissions. You can use the following command to set the correct permissions:

sudo chmod -R 777 var/ pub/

    Enable Error Reporting

    Enable error reporting in your Magento 2 installation. This can help identify any problems with the installation. To enable error reporting, edit the index.php file in your Magento 2 installation and uncomment the following line:

    #ini_set('display_errors', 1).

    Replace with the following:

    ini_set('display_errors', 1).

    Increase PHP memory limit

    If you encounter the error message "Allowed x bytes of memory size exhausted (attempt to allocate y bytes)", this may indicate a memory problem. To resolve this problem, you can increase the memory limit by including the following line in the command you run:

    -d memory_limit=XX

    Example:

    php -d memory limit=4G bin/magento setup: di: compile

    Check for updates

    Make sure the Magento 2 installation is up to date. Run the following command to check for updates:

    composer update

    Check for errors

    Run the following command to see if there are any errors in the Magento 2 installation:

    php bin/magento setup:di:compile

    If there are any errors, they will be displayed in the console. It may be necessary to fix these errors before the command will work properly.

    Check for correct command syntax

    Ensure that you are using the correct syntax for the command.

    Please note:There are several other ways to resolve this issue. Further help from the Magento community or technical support may be required.

    In short, the Magento CLI may seem like a headache, but once you master how it works, you don't have to be a developer to manage a Magento store.


    Contact Us
    Can't read the article? Contact us for free answers! Free help for personal, small business sites!
    Tel: 020-2206-9892
    QQ咨询:1025174874
    (iii) E-mail: info@361sale.com
    Working hours: Monday to Friday, 9:30-18:30, holidays off
    © Reprint statement
    This article was written by Banner1
    THE END
    If you like it, support it.
    kudos12 share (joys, benefits, privileges etc) with others
    commentaries sofa-buying

    Please log in to post a comment

      No comments