The Magento 2 CLI (Command Line Interface) is a powerful tool that allows you to perform various actions on your store from the command line. In this article, we will review the 7 most important CLI commands for your store. We will be using the Magento 2.1.3 CLI version for this example. Please note that the Magento team is constantly updating the CLI, so this information may change in future releases.
How To Find Out Magento 2 CLI version?
The first thing we need to do is make sure that your Magento 2 version is at least 2.1.3, otherwise the commands below will not work. You will need to connect to your Magento 2 root folder over SSH as the Magento file owner. All commands can be launched as bin/magento {command} or {php_path} bin/magento. In most cases, you will be able to just type php instead of {php_path}.
Once you are connected, try running the command php bin/magento. If you see a list of Magento 2 CLI commands, then you will need to use php before bin/magento for each command. If you see an error, then there are a few possible issues:
bin/magento
does not have executable rights. (Usechmod u+x bin/magento
to fix this.)- You need to use the path to the PHP executable file instead of just
php
. (You can find the path to the PHP executable file by running the commandphp -
.) - There is another kind of error. In this case, you can ask your hosting provider how to correctly run the
bin/magento
command.
Once you have access to the list of commands, we need to check your Magento 2 CLI version. To do this, run the command:
php bin/magento --version
This will output your Magento 2 CLI version. If you are not sure which version of Magento 2 you are running, you can also check the composer.json file in the root of your Magento 2 installation. The version of Magento 2 is listed under the version key.
Once you have confirmed your Magento 2 version, you can start using the CLI commands. For more information on the available commands, please refer to the Magento 2 CLI documentation.
1. Enable Maintenance Mode
The maintenance:enable command enables maintenance mode on your store. This is useful when you are making changes to your store that you don't want customers to see. To enable maintenance mode, run the following command:
php bin/magento maintenance:enable
2. Upgrade Magento
The setup:upgrade command upgrades Magento to the latest version. This command should be run after installing a new Magento extension or after upgrading Magento. To upgrade Magento, run the following command:
php bin/magento setup:upgrade
3. Compile DI Container
After php bin/magento setup:upgrade you will need to re-compile Magento 2 dependency injection. When you use Magento in developer mode you dont need to do it, because in this mode Magento 2 makes symlinks for source files.
php bin/magento setup:di:compile
4. Reindex Data
The indexer:reindex command reindexes all of the data on your store. This command can be used to improve the performance of your store. To reindex the data, run the following command:
php bin/magento indexer:reindex
5. Clear Cache
This will flush the Magento 2 cache. It can be useful when you are unable to flush the cache in the Admin. Magento 2 has a lot of cache types when you are using production mode. If you want to remove all cache, as well as compiled code and dependency injection, you can use the following command:
php bin/magento cache:flush
6. Generate Static Content
This command will deploy static content for your store, such as LESS file compilation, JavaScript minification, and concatenation. It needs to be run after LESS/CSS styles are updated, after a Magento 2 extension is installed, and after Magento 2 is upgraded.
Here is a more detailed explanation of what the command does:
- LESS file compilation: This compiles LESS files into CSS files. This is necessary because LESS files are not directly compatible with browsers.
- JavaScript minification: This removes unnecessary characters from JavaScript files, making them smaller and faster to load.
- JavaScript concatenation: This combines multiple JavaScript files into a single file. This can improve performance by reducing the number of HTTP requests that need to be made.
It is important to run this command after making changes to your store's LESS/CSS styles or after installing a new Magento 2 extension. This will ensure that your store's static content is up-to-date and that your store is running as efficiently as possible.
php bin/magento setup:static-content:deploy
7. DIsable Maintenance Mode
This command will disable maintenance mode of Magento 2 store and it will be accessible online again.
php bin/magento maintenance:disable
The top 7 Magento 2 CLI Commands You Should Know: Video
These are just a few of the many CLI commands that are available for Magento 2. For more information, please refer to the Magento CLI documentation.