A Very Quick and Easy Way to Install Magento 2 with Nginx on Ubuntu

Magento 2 is probably the best opensource eCommerce platform available for folks wanting to sell thier products online. It allows anyone to run a their stores online for free using the commnuity edition.

This brief tutorial is going to show students and new users how to quickly and easily install Magento 2 on Ubuntu 16.04 /17.10 and 18.04 with Nginx support. This post is probably out easiest tutorial by far among others we’ve written on installing Mageoto on Ubuntu server.

When you’re ready, continue with the steps below:

Step 1: Install Nginx

Since we’re using Nginx as our web server, run the commands below to install it.

sudo apt update
sudo apt install nginx

Step 2: Install MariaDB

Now that Nginx is installed, run the commands below to install MariaDB database server.

sudo apt install mariadb-server mariadb-client

After installing the database server above, run the commands below to create a root password and secure it.

sudo mysql_secure_installation

When prompted, answer the questions below by following the guide.

  • Enter current password for root (enter for none): Just press the Enter
  • Set root password? [Y/n]: Y
  • New password: Enter password
  • Re-enter new password: Repeat password
  • Remove anonymous users? [Y/n]: Y
  • Disallow root login remotely? [Y/n]: Y
  • Remove test database and access to it? [Y/n]:  Y
  • Reload privilege tables now? [Y/n]:  Y

Step 3: Create Magento Database

After installing the server above, the steps below to create a blank datbase for Magento.

Logon to the database server by running the commands below. when prompted for the root password, type the one you created above.

sudo mysql -u root -p

Then create a database called magento


Create a database user called magentouser with new password

CREATE USER 'magentouser'@'localhost' IDENTIFIED BY 'new_password_here';

Then grant the user full access to the database.

GRANT ALL ON magento.* TO 'magentouser'@'localhost' IDENTIFIED BY 'user_password_here' WITH GRANT OPTION;

Finally, save your changes and exit.


Step 4: Install PHP7.1-FPM and Related Modules

Now that Nginx is installed, run the commands below to install PHP-FPM and related modules. If Ubuntu 7.1 isn’t available on your system, you should install the PPA below.

sudo apt-get install software-properties-common
sudo add-apt-repository ppa:ondrej/php

Then update and upgrade to PHP 7.1-FPM

sudo apt update

sudo apt install php7.1-fpm php7.1-cli php7.1-gd php7.1-mysql php7.1-xml php7.1-curl php7.1-mbstring php7.1-mcrypt php7.1-intl php7.1-soap php7.1-zip

Step 5: Install and Configure Magento

Now that all the servers are installed, run the commands below to change into temp directory and download Magento

cd /tmp && wget https://github.com/magento/magento2/archive/2.2.3.tar.gz
tar -xzvf 2.2.3.tar.gz
sudo mv magento2-2.2.3/ /var/www/html/magento2/

Next, install Composer by running the commands below

sudo curl -sS https://getcomposer.org/installer | sudo php -- --install-dir=/usr/bin --filename=composer
cd /var/www/html/magento2
composer install -v

After that run the commands below to change the folder permissions on the directory

sudo chown -R www-data:www-data /var/www/html/magento2
sudo chmod -R 755 /var/www/html/magento2

Step 6: Configure Nginx

At this point, all that is left is to configure Nginx site. run the commands below to create a new Magento2 site.

sudo nano /etc/nginx/sites-available/magento2

Then copy and paste the content below into the file and save.

upstream fastcgi_backend {
     server  unix:/run/php/php7.1-fpm.sock;

 server {

     listen 80;
     server_name example.com;
     set $MAGE_ROOT /var/www/html/magento2;
     include /var/www/html/magento2/nginx.conf.sample;

Save the file and continue

Next, run the commands below to enable the site above.

sudo ln -s /etc/nginx/sites-available/magento2 /etc/nginx/sites-enabled

Restart Nginx and test the site by opening your browser and going to the URL as defined above to start Magento setup wizard

sudo systemctl restart nginx.service

ubuntu new install

Continue with the wizard until you’re done. you’ll be asked to enter the database connection info that you created above, and create an administrator acccount to manage the backend.

Magento Ubuntu setup

Continue with the database connection info

magento setup new



  1. Hi Amazing Guide

  2. But I got a problem after a simple bin/magento setup:upgrade, I get the error HTTP ERROR 500 can you please help me ?
    is it something about file permissions ?

  3. Donald Lourenço Silveira

    It’s possible to run Magento 2 in a subdirectory, like:

    https://mystore.com/store – Storefront
    https://mystore.com/store/admin – Admin

    I want to use https://mystore.com to serve my own website

  4. Hi ,
    Thank you very much for the simple guide but i have issue when getting to setup page.
    I get a blank page on setup screen.
    How to resolve this ?

    Thank you

  5. Cant even get past step 2 without undocumented errors. Just like any other M2 install guide on the internet. Is there anyone that is actually using M2? M2 appears to be such a colossal screw up compared to M1 its insane!

    My server uses secure keys to log in so no password, Vesta CP created a password for Admin and both pressing for no password and entering in the Admin password still returns “ERROR 2002 (HY000): Can’t connect to local MySQL server through socket ‘/var/run/mysqld/mysqld.sock’ (2 “No such file or directory”)”

Leave a Reply

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.