How to Install Magento 2 (LEMP Stack) on Ubuntu 16.04 LTS

This is a tutorial on how to install Magento 2 and set up the LEMP stack on Ubuntu 16.04 LTS. It is optimised for the AWS EC2, but will work on any Ubuntu 16.04 LTS.

First of all, you want to run and update & upgrade:

Then install Nginx:

If you go to the IP address of your server, given that port 80 is open, you will be able to see the default Nginx screen:

Now it’s time to download Magento 2. My preferred way is to do it the old school way. I download a tarball or a zip file from https://magento.com/tech-resources/download and I use SCP to copy the file to the server and extract.

For example:

You should extract the archive file to /home/ubuntu/magento2 for the purposes of this tutorial. The next thing to do is to install PHP and MySQL:

When MySQL finishes installing, secure it by running the following command and follow the prompts:

Now we need PHP and PHP-FPM to run. Ubuntu 16.04 LTS ships with PHP 7 by default:

Let’s create a database for Magento 2:

For the purposes of this tutorial, I’ll name the database magento2 (and also the user). We need to create a new MySQL user besides root to assign to the magento2 database:

Note that there is no space between -p and the password. In certain cases, where symbols are used in your password, sometimes it’s easier to just use -p and don’t put the password in; MySQL will prompt you for the password.

Let’s increase the memory limit of PHP-FPM to 1024MB:

(you can of course use nano instead of vim)

Change: memory_limit = 128M to memory_limit = 1024M (it was line 389 for me). Save and exit.

Reload PHP 7 FPM to enable the new setting:

Now let’s configure Nginx:

Paste in the following (change mage.dev to your domain name):

Remove the default nginx config file (which is a symlink) and create a symlink for the Magento 2 Nginx config file:

Now, Magento 2 should be nearly ready to be rendered via the web browser. Before we visit it, let’s make sure all the files are assigned to the proper user which in this case, is www-data:

Let’s check the public IP of your server:

Now copy that IP address into the browser of your choice (mine is Chrome) and you should see the following if you are successful!

Magento 2 is ready to begin the setup process

Magento 2 is ready to begin the setup process

I passed all the Magento 2 Readiness Checks thanks to my own tutorial:

Magento 2 Readiness Check

Magento 2 Readiness Check

Now fill in the database details (created previously):

The rest of it is quite straightforward – just follow the prompts.

When the setup was complete, my Magento 2 static files were giving me 404s:

The public / static files did not generate for my locale which is en_AU. If you’re using a locale / language other than the default en_US – you have to run the following steps. First, change your user to www-data:

Then run a chain of commands to generate your static files:

Replace en_AU with your locale code. That’s it!

Magento 2.1.7 is successfully set up & running

Magento 2.1.7 is successfully set up & running

Comments

  • john

    October 31, 2017 at 7:30 pm

    great tutorial, but isn’t it missing the part about setting up the cron job?

  • Francis Kim

    November 1, 2017 at 12:03 pm

    Good point – I’ll update this soon!

  • chipsofttechnology

    February 1, 2018 at 9:31 pm

    good work keep it up. thanks for this, this is helpful for me

  • Sharad

    July 3, 2018 at 6:04 pm

    Had to add default_server in configuration file to get setup page.
    listen 80 default_server;

Write a comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.