How to use Magento 2 command line tools

Time to Read: 10 minutes Difficulty Level: Intermediate
Tools Needed: Plesk control panel, SSH client, Magento 2 installation Last Updated:

Using the Magento 2 command line tools

One of the key advantages of Magento 2 over its predecessor is the powerful command line functionality that’s right at your fingertips. This guide will assume that you’re already comfortable using a SSH client (such as PuTTY) to connect to your webspace and input the commands as specified.

We’ll begin by enabling SSH access for our domain.

Enabling shell access to your domain
Using Magento 2 command line tools

Enabling shell access to your domain

1) Log in to your Plesk Onyx hosting control panel

2) Click on Domains in the left-hand menu to bring up your list of domains.

3) Click the domain you have Magento 2 installed on to bring up its Plesk Dashboard, and click on FTP Access to view your FTP user(s).

4) Now click on your FTP user, so that we can enable SSH access to your domain for that user.

5) Now set Access to the server over SSH to /bin/bash. Lastly, if you’ve forgotten your FTP username and password for your domain (which is the same as your SSH username and password) then reset it in here now, and make a note of it as you’ll need it next. Please make sure your password is a strong password before moving on to the next step in the guide.

6) Click OK and SSH access is now enabled for your domain.

7) Lastly you need to allow SSH access through your Plesk Firewall, which is disabled by default. (There’s a more thorough guide to managing your server’s firewall here for reference). Click Tools & Settings in the left-hand menu of your Plesk Panel.

8) Click Modify Plesk Firewall Rules at the top of the page, then scroll down and click on SSH (secure shell) server.

9) You will now be presented with the action options ‘Allow’, ‘Deny’ and ‘Allow from selected sources, deny from others’. We always recommend Allow from selected sources, deny from others and then adding just your IP address. You can find the IP you need to add by clicking here. Please note that if you don’t have business broadband, there is a likelihood that your broadband IP will change if your router is disconnected. In that event, you may need to follow this process again.

Once you’ve found and added your IP as above, you should now be able to connect to your domain over SSH with no problems.

Using Magento 2 command line tools

1) Open up PuTTY or your SSH client of choice. If you are using a Mac, then open your Terminal app

2) Now do the following:

Windows users (PuTTY or other SSH client)

Input the ‘Host Name’ as yourdomain.com (where yourdomain.com is, well, the name of your domain!). Then leave everything else and click Open. If everything above is correct you’ll be asked for a username and then password. Use your FTP username and password from above to login.

OS x/Linux users (Terminal)

Type:

If everything in the setup stage was correct you’ll be asked for your password. Input your FTP password from above.

NB If your domain isn’t currently pointing at your server yet you’ll need to login to your Plesk control panel and go to your domain dashboard and use the IP address mentioned there as your hostname.

3) You should now be logged into the shell so we now need to make sure that Magento 2 is configured to use the correct version of PHP on the command line.

Run the following command to go to the user’s home directory:

cd ~

4) Now you’re definitely in the user’s home directory, check the PHP version being used by the shell is the correct version by typing:

php -v

6) If the version is correct it the output should contain a reference to the same version of PHP as you have set for your domain via Plesk’s Hosting Settings:

PHP 7.0.32 (cli) (built: Sep 14 2018 13:16:36) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2017 Zend Technologies
    with the ionCube PHP Loader (enabled) + Intrusion Protection from ioncube24.com (unconfigured) v10.2.1, Copyright (c) 2002-2018, by ionCube Ltd.
    with Zend OPcache v7.0.32, Copyright (c) 1999-2017, by Zend Technologies

In the above example, we’re using PHP 7.0. The version of PHP you use on the command line should automatically be set to the correct version for your site, but contact us for advice if the version does not match.

7) Now that is all set all you need to is go to the /bin directory in your Magento 2 install as follows:

cd ./httpdocs/bin

8) You are now ready to run Magento’s command line tools. For example, to reindex your products you would run the following:

php magento indexer:reindex

More Magento command line tools can be found in the Magento 2 documentation here: http://devdocs.magento.com/.

We hope this helps and please let us know your experiences with the tools below