You have to maintain an active subscription in order to be able to use the repository!
This is a stub article at present and it will be expanded with new commands in future.
Whether you are a seasoned PHP programmer, or just a newbie who is trying to get their hands on Magento 2 – you must learn how to use command line with Magento. Some tasks are simply most efficient to be run on the command line, and some tasks can only be run via command line.
There are primarily two command line utilities for Magento 2. Those are
magento (located in
bin/ directory of your Magento website) and
magento command line program allows you to accomplish most of the day-to-day tasks, whereas the
n98-magerun2 has some extra useful commands available. But let’s start with the basics.
Connect to your Magento website via SSH
In the proper setup, you’d be using a special SSH user for your Magento website. Suppose that you have the de-facto standard stack for Magento 2, which is: Varnish, NGINX and PHP-FPM. In this kind of setup:
- Varnish worker process runs under
- NGINX worker process(es) run under
- PHP-FPM site pool runs under
magento(or other arbitrary name) user. This is the user you should SSH to!
Connecting via SSH using the same user as what PHP-FPM ensures there are no problems with
chmod whatsoever. The PHP-FPM runs under that user, and you upload / change / adjust files using that same user. Thus, there is never a conflict between who has the rights to which directory or a file.
So an obvious rule comes from it: never run any Magento related commands using the
root user or anything other than
magento user! Otherwise, they will likely cause some files to change ownership to
root and thus break PHP-FPM from being able to access those files.
bin/magento CLI utility
The standard command-line utility shipped with Magento 2 is the
To use it, simply SSH to your server under the
magento user and run it inside your Magento website directory:
This should give you a long list of available commands. In case you receive the error
bash: bin/magento: Permission denied, you need to make it executable first:
chmod +x bin/magento
An easy shortcut
Now, you can alter your user’s
PATH in order to be able to just type
~/.bash_profile, and add the following at the bottom. Ensure to adjust the path to your Magento’s
PATH=$PATH:/path/to/your/site/bin export PATH
After you log in to your server again, you can just type magento. You still have to
cd to Magento directory though.
Regenerate secure admin URI
As a security measure, you may want to regularly change admin URI:
magento setup:config:set --backend-frontname=backend_$(pwgen -1 --secure)
Get the new/current admin URI
Create Magento administrator user
If you are locked out of administration area, or just want to automated Magento setup process, you can create administrator user from the command line:
magento admin:user:create --admin-user="Username" \ --admin-password="secret" \ --admin-email="firstname.lastname@example.org" \ --admin-firstname="John" \ --admin-lastname="Smith"
Flush entire contents of cache storage backends
magento setup:config:set --http-cache-hosts=127.0.0.1
Setup IP addresses of Varnish instances that serve your Magento 2 website. When your catalog changes, Magento 2 will send
PURGE requests to those Varnish instances. You can specify multiple IP addresses delimited by comma, like so:
magento setup:config:set --http-cache-hosts=127.0.0.1,188.8.131.52,184.108.40.206
Plugin specific commands
It is important to know, that third-party Magento 2 plugins may add additional commands to
magento utility. So, for example, if you use the excellent Mirasvit cache warmer plugin, you can interactively invoke the warmer with:
n98-magerun2 CLI utility
n98-magerun2 command-line utility adds a whole lot of commands you can run in your terminal, to manage your Magento instance.
n98-magerun2 extends the
magento utility described above.
n98-magerun2 admin:user:create ... will work equally to
magento admin:user:create ....
You can stick to using only
n98-magerun2 because it will expose all the commands that
magento has. Just more!
But let’s see how to install it first.
Install n98-magerun2 in CentOS/RHEL 6, 7 or 8
We have packaged
n98-magerun2 for CentOS/RHEL, so it’s easy to install and update via
yum install https://extras.getpagespeed.com/release-latest.rpm yum install n98-magerun2
You can optionally install bash completions package that allows to use TAB for autocompletion of
yum install n98-magerun2-completion-bash
Install n98-magerun2 in other OS-es (generic)
curl -O https://files.magerun.net/n98-magerun2.phar chmod +x ./n98-magerun2.phar mv ./n98-magerun2.phar /usr/local/bin/n98-magerun2
This is quite good to run once right after you install your Magento 2 instance. It will do some sanity checks of your environment and show you whether you’re missing on some PHP extensions or have filesystem ownership problems.
In doubt whether a particular cron task was run? This command shows the list of recently ran Magento cron tasks.
n98-magerun2 sys:cron:run <task name>
Fire a cron job task now, e.g.
n98-magerun2 sys:cron:run newsletter_send_all. Especially useful when you want to run something now, as opposed to waiting until the cron service runs it.
Extra modules for
You can easily extend
n98-magerun2 with an even larger number of interesting commands. The additional commands can be installed as
GetPageSpeed module for
If you have used CentOS/RHEL package for
n98-magerun2, you can install our module for it using:
yum install n98-magerun2-module-getpagespeed
This command allows getting optimal Varnish parameters, based on your Magento database. The output tells you the best values for efficient and failsafe Varnish setup. Sample output:
Largest product category has this number of products: 63 +-------------------+----------------+-------------------+ | http_resp_hdr_len | http_resp_size | workspace_backend | +-------------------+----------------+-------------------+ | 8192 | 32768 | 65536 | +-------------------+----------------+-------------------+
Other commands are useful for quicker generation of static assets and code compilation, to reduce downtime during deployment.
There’s always more. Check out clearmage2 for the CLI utility for Magento 2 designed specifically for deploying code changes with virtually no downtime.