How to Install and Configure Magento 2 on Ubuntu 18.04

Magento 2 is the successor of the powerful open source eCommerce platform Magento available on your Linux distributions. This tutorial will give the steps to install Magento 2 with Nginx on Ubuntu 18.04. This software is written in PHP and use Mysql or MariaDB for the database which means that the configuration can be done with LEMP.

Magento 2 is the latest upgrade of Magento which offers some improvements such as

  • both frontend and backend, one for administration and one for customers of store
  • flexibility and the full support
  • the site speed and the integrated plugins...

The database diagram of Magento 2 is different from Magento 1. Some tables have been renamed or changed from Magento 1 to Magento 2 

  • core_website - store_website (structure is changed)
  • core_store - store (structure is not changed)
  • core_translate - translation
  • core_url_rewrite - url_rewrite
  • core_store_group - store_group (structure is not changed)
  • core_email_template - email_template
  • admin_role - authorization_role (structure is changed)
  • admin_rule - authorization_rule (structure is changed)
  • sales_flat_order - sales_order
  • sales_flat_order_address - sales_order_address
  • sales_flat_order_grid - sales_order_grid
  • sales_flat_order_item - sales_order_item
  • sales_flat_order_payment - sales_order_payment
  • sales_flat_order_status_history - sales_order_status_history
  • coupon_aggregated_order - salesrule_coupon_aggregated_order

Magento seems not to be actually fully compatible with php 7.2 which is the default installed on Ubuntu 18.04. There is a magento community on GitHub available where they try to implement the support of php-7.2 in Magento.

You can below the different php versions actually compatibles with magento 2. You can see the other requirements on the official site of magento

1) Install the pre-requisites

In our tutorial, we will install Magento 2 with Nginx on Ubuntu 18.04 and we will use the MySQL database. This means that you will need to have a running LEMP server on your server. Magento requires some PHP extensions that you will need to install

  • bc-math
  • ctype
  • curl
  • dom
  • gd, ImageMagick 6.3.7 (or later) or both
  • intl
  • mbstring
  • mcrypt
  • hash
  • openssl
  • PDO/MySQL
  • SimpleXML
  • soap
  • spl
  • libxml
  • xsl
  • zip
  • json
  • iconv

You can follow our tutorial how to install LEMP on Ubuntu 18.04 but you will need to make some changes for Magento 2 by installing php 7.1 instead of php 7.2. You need to have the software-properties-common package which is already present in Ubuntu 18.04 and the ondrej/php repository that we will add

# add-apt-repository ppa:ondrej/php -y

Now install php 7.1 with the dependencies

# apt install php7.1-fpm php7.1-mcrypt php7.1-curl php7.1-cli php7.1-mysql php7.1-gd php7.1-xsl php7.1-json php7.1-intl php-pear php7.1-dev php7.1-common php7.1-mbstring php7.1-zip php7.1-soap php7.1-bcmath
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following additional packages will be installed:
  autoconf automake autopoint autotools-dev binutils binutils-common binutils-x86-64-linux-gnu build-essential cpp cpp-7 debhelper dh-autoreconf
  dh-php dh-strip-nondeterminism dpkg-dev fakeroot g++ g++-7 gcc gcc-7
...
...

Please note that if you don't install all these php-dependencies, you will be blocked on a future step of our tutorial.

Then edit the lines of the file /etc/php/7.1/fpm/php.ini to have something like below and make sure to adapt the values to yours

# vim /etc/php/7.1/fpm/php.ini
file_uploads = On
upload_max_filesize = 150M
memory_limit = 256M
cgi.fix_pathinfo=0
date.timezone = Cameroon/Douala

Restart php 7.1-fpm

# systemctl restart php7.1-fpm

2) Create the Magento 2 database

Now we should connect to MySQL database in order to create the database for Magento

# mysql -u root -p
Enter password:

Then create the magento database, user and grant the privileges

mysql> CREATE DATABASE magentodb;
Query OK, 1 row affected (0.00 sec)

mysql> CREATE USER 'magento'@'localhost' IDENTIFIED BY 'magentodb-password';
Query OK, 0 rows affected (0.00 sec)

mysql> GRANT ALL PRIVILEGES ON magentodb.* TO 'magento'@'localhost' IDENTIFIED BY 'magentodb-password';
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)

mysql> exit
Bye

3) Download and configure Magento 2

This tutorial will show the installation of Magento 2.2.5 which is actually the latest release version available. We will download it directly from the official GitHub repository of Magento

# wget https://github.com/magento/magento2/archive/2.2.5.tar.gz
--2018-07-13 08:24:37-- https://github.com/magento/magento2/archive/2.2.5.tar.gz
Resolving github.com (github.com)... 192.30.253.112, 192.30.253.113
Connecting to github.com (github.com)|192.30.253.112|:443... connected.
HTTP request sent, awaiting response... 302 Found
...
...

Now uncompress it

# tar xvf 2.2.5.tar.gz

Then move it to the root folder of your nginx server

# mv magento2-2.2.5/ /var/www/html/magento

Make sure to update the permission

# chown -R www-data:www-data /var/www/html/ && chmod -R 755 /var/www/html/

You can also need composer if you plan to develop Magento extensions. You will also need it if you want to contribute to the Magento 2 codebase.

# apt install composer -y
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
jsonlint php-cli-prompt php-composer-ca-bundle 
....
....

Then check

# composer -V
Do not run Composer as root/super user! See https://getcomposer.org/root for details
Composer 1.6.3 2018-01-31 16:28:17

Now we can install the magento components with composer. This can be done only if you have installed all the php requirements

# cd /var/www/html/magento/ && composer install -v
Do not run Composer as root/super user! See https://getcomposer.org/root for details
Loading composer repositories with package information
Installing dependencies (including require-dev) from lock file
Dependency resolution completed in 0.001 seconds
Analyzed 177 packages to resolve dependencies
Analyzed 482 rules to resolve dependencies
Package operations: 117 installs, 0 updates, 0 removals
Installs: magento/magento-composer-installer:0.1.13, braintree/braintree_php:3.28.0, colinmollenhour/cache-backend-file:1.4, colinmollenhour/cache-bac
kend-redis:1.10.4, colinmollenhour/credis:1.8.2
...
...

4) Configure nginx for magento

Now we will configure Nginx for Magento. Check the location of the PHP socket that Nginx will connect to. So List the contents for the directory /var/run/php/

# ls /var/run/php/
php7.1-fpm.pid php7.1-fpm.sock

Now edit the configuration file

# vim /etc/nginx/sites-available/magento

upstream fastcgi_backend {
        server unix:/var/run/php/php7.1-fpm.sock;
}       
        
server {
        listen 80 default_server;
        listen [::]:80 default_server;

        server_name www.mywebstore.com;

        set $MAGE_ROOT /var/www/html/magento;
        set $MAGE_MODE developer;
        include /var/www/html/magento/nginx.conf.sample; 
}

You can save the configuration, exit and activate the virtual host by creating the symbolic link as below

# ln -s /etc/nginx/sites-available/magento /etc/nginx/sites-enabled/

To avoid some errors about the default virtual host to use, make sure to edit the server block of the nginx default configuration /etc/nginx/sites-available/default in order to remove the default_server mention as below

 #vim /etc/nginx/sites-available/default

server {
        listen 80;
        listen [::]:80;
        # your others line below
}

If you don't do that, you will have an error similar to below

[emerg] 2369#2369: a duplicate default server for 0.0.0.0:80 in /etc/nginx/sites-enabled/magento:22

showing that the two virtual hosts have as considered as the default virtual host to use.  Check the configuration

# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

Now restart nginx server

# systemctl restart nginx

4) Complete magento wizard web installation

Now you can launch your web browser to continue the installation of Magento http://www.yourwebstore.com then agree

Check the php environment

If your environment is correct, you will have all in green

Now configure the database already created with the information used previously with mysql command line

Configure the web access. Make sure to keep the magento admin address. You can take a look at the advanced options

Now choose the currency, the language and the timezone which will be used by your store

You can see the advanced option showing the modules which will be installed

Configure the admin account

Now the installation can start

Now you can see a summarize of the configuration. Don't forget about the important mentions on this page for the security reasons

Now you will see the login page. Enter your admin parameters created before

You can see the dashboard home page of your magento web store. Feel free to browse each feature provided by magento2

You can open the home page of your webstore. It is the page for the client

You can face some errors during the process, we will cover the possible errors which can occur.

5) Fix some possibles errors

During the process, you can face some issues which can stop the installation of your Magento 2. So we have listed some errors with the fixes that could be applied

php FastCGI denied

You can have a permission denied due to the default nginx configuration file of Magento 2 that can not work. By checking your nginx log, you can see the error below

To resolve it, edit the /var/www/html/magento/nginx.conf.sample /setup location block by adding a specific line as below

 # vim /var/www/html/magento/nginx.conf.sample

location ~* ^/setup($|/) {
    root $MAGE_ROOT;
    location ~ ^/setup/index.php {

        ### The only line to add is the one below:
        fastcgi_split_path_info ^(.+?\.php)(/.*)$;
        ###################
        ## Don't change anything else in the file

Then restart nginx. You can follow a thread on Magento Github about that problem if you are unable to run Web Setup on nginx

One or more indexers are invalid. Make sure your Magento cron job is running

While accessing to the admin panel for the first time, you can have a message concerning the invalid indexes and the magento cron job which are not working properly as below

Here you need to reindex all available indexing in Magento 2. For that, go to the project root directory

# cd /var/www/html/magento

and execute the magento CLI

# php bin/magento indexer:reindex
Design Config Grid index has been rebuilt successfully in 00:00:00
Customer Grid index has been rebuilt successfully in 00:00:00
Category Products index has been rebuilt successfully in 00:00:00
Product Categories index has been rebuilt successfully in 00:00:00
Catalog Rule Product index has been rebuilt successfully in 00:00:00
Product EAV index has been rebuilt successfully in 00:00:00
Stock index has been rebuilt successfully in 00:00:00
Product Price index has been rebuilt successfully in 00:00:00
Catalog Product Rule index has been rebuilt successfully in 00:00:00
Catalog Search index has been rebuilt successfully in 00:00:00

Now to setup and run cron for Magento 2, use the command

# php bin/magento cron:run
Ran jobs by schedule.

Cache Types are invalidated

You can face the cache types which are invalidated. The message will appear on the top

One or more of the Cache Types are invalidated: Page Cache. Please go to Cache Management and refresh cache types.
The reasons for block cache invalidation can be from updates of products, catalog price rule changes, and also 3rd party extensions. Some running cronjobs can also cause block caches to become invalidated. The resolution is indicated in the message, go to system > cache management 

You will see all the cache types. Now click on Flush Magento Cache

Read Also:

You have the different steps to have a functional Magento 2 on Ubuntu 18.04. The support of the php-7.2 seems not to be ready but a magento community is working on this purpose. You can force trying to adapt Magento 2 with php-7.2 but, when the official support will be ready, your patch can be no longer working.

Alain Francois 12:05 am

About Alain Francois

IT Linux administrator passionate of free and open source software, I work on Linux Systems since some years on installations and deployments of certain solutions. I like to share my experiences with a wider audience by training and conferences.

Author Archive Page

Have anything to say?

Your email address will not be published. Required fields are marked *

All comments are subject to moderation.