#### How To Install Magento2 on Ubuntu 18.04 and Debian Jessie in 10 Minutes

##### This guide shows you how to get a Mangento ecommerce shop up and running in 10 minutes or less.

The Magento developers provide great documentation covering installing Magento, however, they have created rather too much. They list several options at each step which are spread across many pages of dense information. I have gone through this information and extracted a simple walk through that will get you from a fresh Ubuntu 16.04 or Debian Jessie server to a working Magento installation in under 10 minutes.

All that you will need to follow this guide are: 1. An Ubuntu 18.04, Debian Jessie server with at least 1GB of RAM. 2. A basic knowledge of using the Linux command line.

Please note, Whenever I mention Magento in the following guide I am referring to Magento 2.

In order to begin these steps, you must SSH into your server as the root user.

## Step 1 – Install all necessary packages.

Magento requires a web server, a database server, PHP and some PHP extensions.

We will use Apache2 and MariaDB as our web and database servers along with the default PHP in Ubuntu 16.04 and Debian Jessie, PHP 7. The following commands will install everything required:

apt-get update
apt-get install apache2 mariadb-server php-curl php-gd php-intl php-mbstring php-mcrypt php-mysql php-xml php-soap php7.0-xml php-zip php-json libapache2-mod-php php-fpm


## Step 2 – Create a system user

The Magento developers recommend, for security reasons, that a non-root system user is created and given ownership of the Magento installation files.

We will create a user called magento and add them to the web server’s group with the following commands:

adduser magento
usermod -a -G www-data magento


Next, we need to create and configure the database that Magento will use.

## Step 3 – Creating a database

In order to create a database, we need to first enter the MariaDB shell. This is simply done by entering the command mysql on the command line. When you do that you will be immediately taken to the MariaDB shell that will supply a new prompt that looks like this:

MariaDB [(none)]>


The following command will create a new database called magentodb:

CREATE DATABASE magentodb;


Next, we will create a new user called magento and assigned them full permissions to the new database with the following command:

GRANT ALL ON magentodb.* TO magento@localhost IDENTIFIED BY 'a_strong_password';


You must change a_strong_password to a secure password when you enter this command. Make a note of the database name, username and password as they will be required later. After you have done this log out of the MariaDB shell by entering exit;

It is a good idea to validate the database we just created along with the username and password. This is done by logging into the MariaDB shell as the new user with the following command:

mysql -u magento -p magentodb


If you are able to log in using the password you set in the “GRANT ALL” command then the new database and user are working correctly.

You should select version 2.1.x without sample data. We will use the .tar.gz version as this is the most widely supported compression format. The required version is shown highlighted here:

After you have uploaded the archive to your server you need to log back in as root and create the directory that will contain the Magento instance.

In this guide, we will install the Magento instance to /var/www/magneto.

The following command creates this directory:

mkdir /var/www/magento


Next, copy the Magento archive into /var/www/magento:

cp /path/to/Magento-archive.tar.gz /var/www/magento


Then, move into /var/www/magento and unpack the archive:

cd /var/www/magento
tar -zxf Magento-archive.tar.gz


The archive is no longer needed in the installation directory and should be deleted:

rm Magento-archive.tar.gz


Next, we will set recommended ownerships and permissions. The following commands must be run from the installation directory, i.e. /var/www/magento:

cd /var/www/magento
find var vendor pub/static pub/media app/etc -type f -exec chmod g+w {} \;
find var vendor pub/static pub/media app/etc -type d -exec chmod g+ws {} \;
chmod u+x bin/magento


Finally, we have to set the correct ownerships:

chown -R magento:www-data /var/www/magento


We will finish installing Magento from an installation page accessed by a browser. Before we can do this we must configure Apache to serve the site.

### Step – 5 Configuring Apache

Apache, PHP and all the necessary extensions were installed at the beginning of this guide. All that is left is to create the VirtualHost file for the site that you will use to access Magento.

First, change into the sites-available directory under Apache’s configuration directory:

cd /etc/apache2/sites-available/


Then use your preferred text editor to create and open a new VirtualHost file. Here we will use nano and the file will be called example.com.conf:

nano example.com.conf


We will use the following VirtualHost template:

<VirtualHost *:80>
ServerName www.example.com
DocumentRoot /var/www/magento
ErrorLog ${APACHE_LOG_DIR}/magento_error.log CustomLog${APACHE_LOG_DIR}/magento_access.log combined

<Directory /var/www/magento >
AllowOverride All
</Directory>

</VirtualHost>


Copy and paste this into the VirtualHost file. The only change that you will need to make is to change www.example.com to the name of your web site.

Then, enable the new site with the following command:

a2ensite example.com


You must change this example to match the name of the VirtualHost file you created. Simply use the file name without the “.conf” to enable it e.g. my-domain.com.conf becomes:

a2ensite my-domain.com


Next, run the following to enable mod_rewrite Apache module which is required by Magento:

a2enmod rewrite


It is always a good idea to check any new Apache configuration with its built-in tool to ensure that there are no errors. This is because if you have made a mistake in the configuration Apache will not restart taking any other sites on your server offline. The following command checks Apache’s configuration files for syntax errors:

apachectl configtest


If there are no problems Syntax OK will be printed to the screen. If there is an issue the output will tell you the file and line that has the error. When you can pass the configuration test run the following command to reload Apache:

systemctl reload apache2


We can now browse to the installation page and start the installation.

## Step – 6 Installing Magento

We will install Magento by accessing the installation page in a web browser. You need to visit the web site address that you set in the VirtualHost file and you will be automatically redirected to the installation page.

Start the installation by clicking on:

On the next page click:

This will initiate a test that checks that all the required PHP extensions are on the server and that the correct ownership and permissions were set. Everything should pass as we have installed and configured everything as required by Magento.

Click:

to proceed to the next page when the tests have completed.

The next page is where we configure Magento to use the database we created. This is a screenshot of the fields filled in with the details we used when we created the database above:

You will need to enter the password you set when we created the database. Click Next when you have filled in the fields.

The next page is where you can change the store name and administration access URL if you need to. If you are not sure about these leave them as you can change them after Magento is installed.

On the next page, you can set the time zone, currency and language of your store.

The next page is where you must create a user to access the Magento administration panel. Do not use admin as the username as this is always the first username that hackers will try to guess.

Click Next when you have filled in those fields and taken a note of the information you set.

On the last page click:

This will initiate the installation process. It will take a couple of minutes to complete.

When the installation has finished you will see a Success page listing all the details of your Magento installation. You should copy and paste all this information into a file for future reference.

The final task for this installation is to create the Magento cron jobs.

## Step 7 – Creating the Magento cron jobs

Magento requires several periodic, backend tasks to keep the installation working correctly. Linux has a specific program called cron which is designed to run tasks or jobs at intervals. We will configure cron to run Magento’s backend tasks.

On the command line run the following command to open the crontab (the crontab is the file containing the jobs for cron to run) in editor mode as the user magento.

crontab -u magento -e


When the editor is open copy and paste the following three lines at the bottom of the file:

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



When you have pasted these three lines make sure to leave a final, blank line (as shown above) at the bottom of the file as this is required by cron.

Magento is now installed and working.

## Conclusion

We have now completed a Magento 2 installation following the recommendations of the Magento developers. A good idea at this point is to take a backup of your server or VM before you customize your Magento installation. This will give you an excellent starting point to return to if you mess up your installation or if you simply want to experiment with Magento and want a handy starting point before settling on a production version.