How to Install Magento 2 using the command line on a shared server

Category: Magento
How to Install Magento 2 using the command line on a shared server

In this article we will go through Magento 2 installation via command-line. We’ll take a look on different parameters used in to magento setup:install command, we’ll set correct file and folder permissions so you can start with Magento 2 development.

These steps are also covered in the Magento 2 documentation but didn’t quite work for me straight out of the box. So I’m going to share my experience with you!

Foreword

Magento 2 release has been announced a long time ago and in 2015 Magento 2 was finally released. Magento has always been powerful, flexible and full of features. It also has great community and documentation. And having a new version of such a great e-commerce platform is very exciting. With the release of Magento 2 1.0.0 Beta, I rushed and installed it. Now it’s time to install Magento 2.1.0 - final release version.

The learning curve of Magento has been very steep. XML based configurations are meant to make things simpler but for a beginner it’s very hard to get used to. But still, before diving into development you should first install it.

When I tried installing Magento 2 on localhost I found out that I had to install mcrypt and intl php extensions so I wrote an article Install Mcrypt and Intl Php Extensions on Mac OS X and XAMPP. Then I wanted to install Magento 2 on a shared hosting and I kept failing because installation process was getting stuck at 66% with this message “[ERROR] exception 'Exception' with message 'Warning: Error while sending QUERY packet”. I tried playing around with all kinds of PHP and MySQL settings but nothing seemed to work for me.

While reading bunch of articles I came across command-line installation of Magento 2 that lets you install Magento 2 without any issues that might arise while installing via web installer. While reading more about command-line installation I discovered that using bash you can manipulate, tweak and manage Magento better then via web interface. So as you start with Magento 2 it will be better to get more familiar with commands and for sure it will be handier when you dive more into Magento 2 development.

Before we proceed I’d like to mention very obvious characteristics of Magento. It’s huge, it’s massive and it takes a lot of effort to get used to it to handle it the way you would like to. So you should always keep in mind to take it easy, relax and stay calm while working with Magento 2. Treat it as a wild cat that you are trying to tame and when you think you almost made it, it will bite you again. I might be exaggerating, so don’t be afraid. All I’m trying to say is that you need patience as you start with Magento and be ready to do things couple of times.

Install Magento 2

First you need to connect to your server via SSH. If you haven’t dealt with SSH connection before you can take a look at my article Installing Magento Patches SUPEE-5344, SUPEE-1533 – Using SSH Connection and Running SH Script where I cover every step in detail. First you want to download Magento 2 from the download page and upload it to your server using FTP client (ex. Filezilla) or cPanel’s file manager. I prefer doing this instead of cloning GitHub and installing dependencies via composer. I think downloading archive and uploading it directly to server is much easier and faster, at least for me. I uploaded my tar.gz archive to public_html/magento directory via File Manager from CPanel of my hosting. Next I connect to the server via SSH, navigate to the directory where magento archive is uploaded and extract it:

cd public_html/magento
tar -zxvf Magento-CE-2.1.0-2016-06-23-02-28-19.tar.gz

Just in case if you downloaded .zip file you should use unzip name_of_file.zip command to extract it, instead of tar –zxvf.

Hopefully the extraction process went well so now we can install Magento 2 via command-line.

Before I tried installing Magento 2 via CLI (command-line interface) I navigated to the directory via web browser: http://mydomain.com/magento. I was greeted by this error message:

Fatal error: Uncaught exception 'Zend\Stdlib\Exception\RuntimeException' with message 'No wrapper found supporting "UTF-8"' in … on line 135

It turned out that the server was missing mb_string and iconv php extensions. As you might guess I didn’t check the system requirements by Magento 2. Actually I thought that everything would be present on the server.

So just in case you haven’t checked system requirements check Magento 2 Documentation to see the list of requirements and in your terminal issue command php –m that will give you the full list of php modules installed. Now you can make sure your Apache and PHP setup conform all requirements.

It’s my personal preference to make the readiness check of Magetno 2 before proceeding. To do so I visit my magento URL: http://mydomain.com/magento. There’s a Start Readiness Check button on the first step of the web installer and if all of the requirements are met you can start installation of the system.

Before installing Magento 2, developer documentation recommends to set file permissions on the following directories:

  • vendor (Composer or compressed archive installation)
  • app/etc
  • pub/static
  • var

NOTE: I haven’t set any permissions on these folder but still didn’t have any issues installing Magento 2

Using the command-line access, enter the following commands in the order shown:

cd <your Magento install dir>
find var vendor pub/static pub/media app/etc var -type f -exec chmod u+w {} \;
find var vendor pub/static pub/media app/etc var -type d -exec chmod u+w {} \;
chmod u+x bin/magento

NOTE: If you are not familiar what u+w mean you can watch Chmod Tutorial - How To Use chmod or How To Use chmod and chown.

It’s needless to say that you have to create MySQL database and user.

It’s time to fire up our engines and install the system. Magento 2 installation command looks like this:

php bin/magento setup:install --base-url=http://mydomain.com/magento/ --db-host=localhost --db-name=magento_storedb --db-user=magento_user --db-password=magento123 --admin-firstname=Michael --admin-lastname=Gotiashvili --admin-email=This email address is being protected from spambots. You need JavaScript enabled to view it. --admin-user=admin --admin-password=admin123 --backend-frontname=admin_magento --language=en_US --currency=USD --timezone=Asia/Tbilisi --cleanup-database --sales-order-increment-prefix="ORD$" --use-rewrites=1 --session-save=file

If you take a closer look at the command you will notice that we are going to use Magento’s built-in command-line interface that is able to perform both installation and configuration tasks and it’s located in /bin/magento. The new interface performs multiple tasks, including:

  • Installing Magento (and related tasks such as creating or updating the database schema, creating the deployment configuration, and so on)
  • Clearing the cache
  • Managing indexes, including reindexing
  • Creating translation dictionaries and translation packages
  • Generating non-existent classes such as factories and interceptors for plug-ins, generating the dependency injection configuration for the object manager
  • Deploying static view files
  • Creating CSS from LESS

To use Magento’s CLI you simply need to type the following in terminal (assuming you are in the root directory, where magento files are located) php bin/magneto _and_some_command_. To list all available installation and configuration commands use php /bin/magento list

To install Magento 2 we use setup:install command that is followed by parameters and values. They are really straightforward. I’d like to highlight only --backend-frontname paramenter. This is where you tell magento installer how to name/set URI to your backend. Magento recommends not using “admin”, “cpanel” and anything that is easy to guess. Here’s the guidline. I will leave the name admin_magento intentionally in order to show you how to change/update magento configuration from command line.

I would recommend you adding --session-save parameter into the installation query (already included in the query). If you don’t specify this option Magento by default will use database to store session data. Over time it will result into huge database. Magento recommends to use database in case if you have a clustered database. I think using file system for session storage will work better for me and in the query I added --session-save=files.

To find out more on magento installation parameters see Magento 2 Documentation.

Hopefully everything will run without any issues. Just in case you get a message saying “Too Many Arguments” make sure to check that your query is correct.

To make sure that you have successfully installed Magento 2 system, you can navigate to your installation URL: http://mydomain.com/magento and check out Magento’s frontend. Know that when when you run magento for the first time it will generate static files like javascript, css, images and create cached version of pages and assets. If you have products then it will create product images resized too. Hence the first time load will take a lot of time. To deploy static view files use this command:

php bin/magento setup:static-content:deploy

Change the Admin URL

Now let’s get back to installation command. If you recall I intentionally left backend name to simple one in order to show you how you can update/change Magento 2 configuration via command-line.

To find out what is the current address to our backend and to display the Admin Uniform Resource Identifier (URI) issue the following command: magento info:adminuri

You can also view the Admin URI in /app/etc/env.php. A snippet follows:

'backend' =>
  array (
    'frontName' => 'magento_admin',
  ),

To change the Admin URI, use the magento setup:config:set command. Let’s change backend URI by issuing php bin/magento setup:config:set --backend-frontname=SOME_NEW_NAME. Then you will have to agree by typing y and hitting the return key.

Now let’s access the backend.

As soon as you login in into admin you will be notified that some indexes are out of date and they need to be re-indexed. Also Magento need to have CRON jobs enabled to perform some tasks on schedule. I will cover CRON jobs in another article.

Re-Indexing via Command-line

To get information on indexer status you simply issue command php bin/ magento indexer:status. The result might look like this:

Category Products:             Reindex required
Product Categories:            Reindex required
Product Price:                 Reindex required
Product EAV:                   Reindex required
Stock:                         Reindex required
Catalog Rule Product:          Reindex required
Catalog Product Rule:          Reindex required
Catalog Search:                Reindex required

In order to reindex use php bin/magento indexer:reindex. Now your Magento 2 is almost ready to go online.

File Permissions

Finally let’s set correct file permissions for you Magento 2 installation. You can simply copy/paste each line at a time:

find . -type d -exec chmod 755 {} \;
find . -type f -exec chmod 644 {} \;
find ./var -type d -exec chmod 777 {} \; 
find ./pub/media -type d -exec chmod 777 {} \;
find ./pub/static -type d -exec chmod 777 {} \;
chmod 777 ./app/etc
chmod 644 ./app/etc/*.xml

NOTE: Personally I do use only the first two commands for setting permissions for directories and files.

Hooray! We have installed Magento 2.

Conclusion

I’m very excited that I finally managed to publish this article. I’m planning to focus on Magento 2 development and write more articles and Magento 2 fronted development in depth. Just in case you have any questions regarding the installation process feel free to contact me. You can follow me on Twitter to stay updated on new articles on Magento 2 development.