Site icon GetPageSpeed

Magento 2 Command-Line Interface

This is a stub article at present and it will be expanded with new commands in the future.

Whether you are a seasoned PHP programmer or just a newbie who is trying to get your hands on Magento 2 – you must learn how to use the 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 the command line.

There are primarily two command-line utilities for Magento 2. Those are magento (located in bin/ directory of your Magento website) and n98-magerun2.

The 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:

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.

The bin/magento CLI utility

The standard command-line utility shipped with Magento 2 is the bin/magento program.

To use it, simply SSH to your server under the magento user and run it inside your Magento website directory:

bin/magento

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 magento. Edit ~/.bash_profile, and add the following at the bottom. Ensure to adjust the path to your Magento’s bin directory:

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.

Useful magento commands

Regenerate secure admin URI

As a security measure, you may want to regularly change the admin URI:

magento setup:config:set --backend-frontname=backend_$(pwgen -1 --secure)

Get the new/current admin URI

magento info:adminuri

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="user@example.com" \
  --admin-firstname="John" \
  --admin-lastname="Smith"

Flush entire contents of cache storage backends

magento cache:flush

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,1.1.1.1,2.2.2.2

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:

magento mirasvit:cache-warmer:crawl

The n98-magerun2 CLI utility

The 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.

So 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.

yum -y install https://extras.getpagespeed.com/release-latest.rpm
yum -y install n98-magerun2

You can optionally install bash completions package that allows to use TAB for autocompletion of n98-magerun2 commands:

yum -y 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

Useful n98-magerun2 commands

n98-magerun2 sys:check

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.

n98-magerun2 sys:cron:history

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 n98-magerun2

You can easily extend n98-magerun2 with an even larger number of interesting commands. The additional commands can be installed as n98-magerun2 modules.

GetPageSpeed module for n98-magerun2

If you have used CentOS/RHEL package for n98-magerun2, you can install our module for it using:

yum install n98-magerun2-module-getpagespeed
n98-magerun2 varnish:tuned

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.

Exit mobile version