This tutorial will provide a comprehensive guide on how to install Magento 2 on your web server and finalise your installation. This Magento 2 installation tutorial provides multiple installation options including:

  • How to install Magento 2 using the Magento Installer with Composer
  • How to install Magento 2 using the command line.

Magento 2 Installation Difficulty

Using the Magento 2 Installer

Installation Difficulty: Beginner

Installation Time: 20 Minutes

Using the Command Line

Installation Difficulty: Medium

Installation Time: 5 Minutes

Server Requirements

Magento 2 comes with some server perquisites that you must meet before starting the installation. Failing to meet the Magento 2 server requirements will subsequently lead to errors and loss of time moving forwards.

  • Up to 2GB of RAM
  • Apache 2.2 or Apache 2.4
  • MySQL 5.6 or 5.7
  • PHP 7.1.3+

In addition to this, the following PHP extensions are required:

  • bc-math (Magento Commerce only)
  • ctype
  • curl
  • dom
  • gd
  • intl
  • mbstring
  • hash
  • openssl
  • PDO/MySQL
  • SimpleXML
  • soap
  • spl
  • libxml
  • xsl
  • azip
  • json
  • iconv

Download the Magento 2 Files

Firstly we must download the Magento 2 files and upload them to our website.

There are two methods of downloading Magento 2 on your server, therefore, choose the method that is most suitable to you.

Method 1: How to download Magento 2 using Composer

The quickest and most convenient way to download Magento 2 is by using composer. To do this you must have SSH access to your server and be comfortable using the command line.

Firstly, navigate to your websites folder on the command line and execute the following command:

composer create-project --repository-url=https://repo.magento.com/ magento/project-community-edition .

Method 2: Download Magento 2 from the Magento website

Some users are unable to use the command line, therefore this article also explains options for not using the command line.

  • Download the latest Magento 2 source files from the Magento website and save them to your computer
  • Extract the files into a folder
  • Upload the files to your website using an FTP client such as FileZilla

Set the Magento 2 File Permissions

You have downloaded the Magento 2 files and uploaded them to your web server. Secondly, it’s time to set the correct file permissions.

Magento regularly creates and edits files on the web server due to its heavy caching system.  Therefore, incorrect file permissions may cause errors and unexpected behaviour.

Permission errors can be resolved at any time during installation and maintenance by completing the steps below.

Method 1: Using the command line

Navigate the your Magento 2 installation directory in your terminal and execute the following command:

find . -type d -exec chmod 755 {} \; && find . -type f -exec chmod 755 {} \;

Method 2: Using FTP

If for example your hosting provider does not allow you to connect using SSH and you are unable to use the command line,  you can use your FTP client to manually set the correct file permissions.

Set all files and directories to 755.

Create a MySQL database

It’s time to create a MySQL database and a user to access it, however there are some security considerations to take into account.

Most importantly, do not use a root user for Magento because this leaves your entire web server vulnerable in the event of an intruder.

In other words, create a new MySQL user and grant it privileges for your new database, making a note of the MySQL database name, username and password.

How to install Magento 2

Now that our preparation has been completed it’s time to install Magento 2 and complete our final tasks.

There are two methods that we can use to install Magento 2, so choose the solution that is most relevant to your scenario.

  1. Install Magento 2 using the Command Line
  2. Install Magento 2 using the Magento Installer

How to install Magento 2 using the Command Line

If you are comfortable using the command  line and have SSH access to your website then you can use the Magento 2 bash script to quickly run the Magento 2 installer.

To do this you must navigate to your Magento 2 project directory in your terminal and run the following commands, replacing the arguments with your details.

php bin/magento setup:install 
--base-url="http://example.co.uk/" 
--db-host="localhost" 
--db-name="databasename" 
--db-user="databaseuser" 
--db-password="databasepass" 
--admin-firstname="Yourname" 
--admin-lastname="Yourname" 
--admin-email="hello@example.co.uk" 
--admin-user="admin" 
--admin-password="setpasswordhere" 
--language="en_GB" 
--currency="GBP" 
--timezone="Europe/London" 
--use-rewrites="1" 
--backend-frontname="admin"

How to install Magento 2 using the installer

On the other hand, if you are unable to install Magento 2 using the command line then you can use the Magento web installer.

Firstly, go to your website in a browser and you will be greeted with the Magento 2 Installation Wizard. Follow the steps as pictured below to install Magento 2.

Finishing Up

You now have a Magento 2 installation on your website and you are almost good to go! It’s now time to finalise your installation by setting some file permissions and after that, creating a Cron Job.

See the section above for instructions on setting the correct file permissions for Magento 2.

We have been changing the Magento files during our installation which subsequently may have changed the file permissions.

How to setup a Cron Job for Magento 2

Cron Jobs are automatic tasks that a web server runs during a set schedule. In our case, we want to tell the web server to run the Magento built in Cron Jobs.

Magento Cron Jobs perform tasks such as reindexing the catalog amongst other things, therefore, it is important to set these up to keep your website up to date.

Setting up the Cron Job

Using the command line, find the PHP installation directory using the following command:

which php

In our case, it returns /usr/bin/php – This will be used in our Crob Job setup below. If yours is different you should replace it with your PHP directory.

Entering Cron Tab

Run the cron tab command specifying the apache web server user as the executor. For Centos servers this is apache, however on Ubuntu servers this is www-data. I will demonstrate using the apache username.

crontab -u apache -e

Enter the following code into your Cron Job editor:

* * * * * /usr/bin/php /var/www/mywebsite.com/magentofolder/bin/magento cron:run | grep -v "Ran jobs by schedule" >> /var/www/mywebsite.com/magentofolder/var/log/magento.cron.log
* * * * * /usr/bin/php /var/www/mywebsite.com/magentofolder/update/cron.php >> /var/www/mywebsite.com/magentofolder/var/log/update.cron.log
* * * * * /usr/bin/php /var/www/mywebsite.com/magentofolder/bin/magento setup:cron:run >> /var/www/mywebsite.com/magentofolder/var/log/setup.cron.log

If you have not been following the command line instructions in this tutorial and you do not have SSH access to your website, contact your web hosting provider for information on how to setup a Cron Job.

Some web hosts provide a user interface for Cron Job management which similarly tell the web server to perform automated tasks.

Conclusion

To sum up, you have downloaded, installed and finalised Magento 2. You can find further Magento Tutorials and other development guides on this blog to continue your Magento Development journey.