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 命令详解:全面指南与实用示例](https://www.361sale.com/wp-content/uploads/2024/11/20241130202348751-image.png)
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 命令详解:全面指南与实用示例](https://www.361sale.com/wp-content/uploads/2024/11/20241129145729573-image.png)
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 命令详解:全面指南与实用示例](https://www.361sale.com/wp-content/uploads/2024/11/20241129145747946-image.png)
![图片[4]-Magento 2 CLI 命令详解:全面指南与实用示例](https://www.361sale.com/wp-content/uploads/2024/11/20241129145824123-image.png)
Enter the credentials:Under Master Credentials, enter the credentials, i.e. username and password.
![图片[5]-Magento 2 CLI 命令详解:全面指南与实用示例](https://www.361sale.com/wp-content/uploads/2024/11/20241129150055833-image.png)
Access the Magento application and its public folder:
![图片[6]-Magento 2 CLI 命令详解:全面指南与实用示例](https://www.361sale.com/wp-content/uploads/2024/11/20241129151019931-image.png)
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 命令详解:全面指南与实用示例](https://www.361sale.com/wp-content/uploads/2024/11/20241129152030188-image.png)
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) cache | command | shortcuts | descriptive |
---|---|---|---|
bin/magento cache: status | bin/magento c:s | List cache types and their status | |
bin/magento cache: enabled | bin/magento c:e | Enable all caches or any specific cache type | |
bin/magento cache: disabled | php bin/magento c:d | Disable all caches or any specific cache type | |
bin/magento cache: clean | php bin/magento c:d | It will only remove cache types linked to Magento | |
bin/magento cache: refresh | bin/magento c:f | Delete 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:
indexer | command | shortcuts | descriptive |
---|---|---|---|
bin/magento indexer: info | bin/magento indexer:info | Show list of all indexers | |
bin/magento indexer: status | bin/magento i:sta | View the status of all indexers or only specific indexers | |
bin/magento indexer: setting the schema | bin/magento i:set | Reindex all indexers or only specific indexers | |
bin/magento indexer: reindexing | bin/magento i:rei | Invalidates all indexers or specific indexers | |
bin/magento indexer: display mode | bin/magento i:sho | View 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) | command | shortcuts | descriptive |
---|---|---|---|
bin/magento administrator:user:create | bin/magento a:u:c | Create Administrator | |
bin/magento admin: user: unlocked | php bin/magento a:u:u | Unlock Administrator Account | |
bin/magento administrator: adobe-ims: status | Status of the Adobe IMS Module | ||
bin/magento admin:adobe-ims:info | Adobe IMS Module Configuration Information | ||
bin/magento admin: adobe-ims: enable | Enabling the Adobe IMS Module | ||
bin/magento administrator: adobe-ims: disabled | Disabling 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) | command | shortcuts | descriptive |
---|---|---|---|
bin/magento module: status | bin/magento mo:s | Checking the Status of Magento Modules | |
bin/magento module: enable | bin/magento mo:e | Enabling Specific Magento Modules | |
bin/magento module: disable | bin/magento mo:d | Disabling Specific Magento Modules | |
bin/magento module: uninstall | bin/magento m:u | Uninstalling 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:
- magento cron: run
Example:
Cron | command | shortcuts | descriptive |
---|---|---|---|
bin/magento cron: install | bin/magento c:in | Generate and install crontab for the current user | |
bin/magento cron: delete | bin/magento c:re | Deleting tasks from crontab | |
bin/magento cron: run | bin/magento c:r | on 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:
safeguard | command | shortcuts | descriptive |
---|---|---|---|
bin/magento maintenance: allow ips | bin/magento m:a | Setting the Maintenance Mode Exemption IP | |
bin/magento maintenance: disabled | bin/magento ma:d | Disable maintenance mode | |
bin/magento maintenance: enabled | bin/magento ma:d | Enable Maintenance Mode | |
bin/magento maintenance: status | bin/magento ma:s | Display 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:
text | command | shortcuts | descriptive |
---|---|---|---|
bin/magento Info: adminuri | bin/magento i:a | Show Magento Management URIs | |
bin/magento Information: Backups: Lists | bin/magento i:b:l | Print a list of available backup files | |
bin/magento Information: Currency: Lists | bin/magento i:c:l | Display a list of available currencies | |
bin/magento Info: Dependencies: Display Framework | bin/magento i:d:show-f | Display the number of dependencies for the Magento framework | |
bin/magento info: dependencies: display module | bin/magento i:d:show-modules | Display the number of dependencies between modules | |
bin/magento info: dependencies: show module loop | bin/magento i:d:show-circular | Display the number of circular dependencies between modules | |
bin/magento Information: Language: Lists | bin/magento i:l:l | Displays a list of available locale settings | |
bin/magento Info: Time zone: Listings | bin/magento i:t:l | Display 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 up | command | shortcuts | descriptive |
---|---|---|---|
bin/magento setup: configuration: settings | bin/magento s:c:s | Creating or Modifying Deployment Configurations | |
bin/magento setup: di: compile | bin/magento s:d:c | Generate DI configuration and all missing classes that can be automatically generated | |
bin/magento setup: upgrade | bin/magento s:up | Upgrading Magento applications, database data and architecture | |
bin/magento setup: uninstall | bin/magento s:un | Uninstalling the Magento Application | |
bin/magento setup: install | bin/magento s:i | Installing the Magento Application | |
php bin/magento settings: static content: deployment | php bin/magento s:s:d | Deploying 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) shortcut | descriptive |
---|---|---|
bin/magento deploy:mode:set | bin/magento d:m:set | Setting the application mode |
bin/magento deploy:mode:show | bin/magento d:m:sho | Display 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.
Link to this article:https://www.361sale.com/en/28337The article is copyrighted and must be reproduced with attribution.
No comments