There is a new version of this tutorial available for Ubuntu 22.04 (Jammy Jellyfish).

How to Install Magento with Nginx on Ubuntu 16.04

Magento is an open source e-commerce software and content management system for e-commerce websites based on the PHP Zend Framework. It uses MySQL or MariaDB as database backend. The Magento development has been started in 2008 by Varien.

In this tutorial, I will show you how to install Magento 2 with Nginx, PHP 7.0 in FPM mode, and MariaDB as the database. I will use ubuntu 16.04 (Xenial Xerus) as server operating system. If you don't have a minimal server setup yet, please see this tutorial to get a proper base setup.


  • Ubuntu 16.04 - 64 bit.
  • Root Privileges.

Step 1 - Install Nginx

Login to your Ubuntu server with your root account (e.g. by SSH) and update the repository.

sudo su
apt-get update

Then install Nginx:

apt-get install nginx -y

Verify that nginx has been installed properly by checking the port:

netstat -plntu | grep 80

Step 2 - Install and Configure PHP-FPM

In this step we will to install PHP 7 in PHP-FPM mode. Additionally we will install the following PHP extensions that are required by magento.

  • php-gd
  • php-mhash
  • php-mcrypt
  • php-xsl
  • php-pear
  • php-soap

Install the packages with the apt command below:

apt-get install php7.0-fpm php7.0-mcrypt php7.0-curl php7.0-cli php7.0-mysql php7.0-gd php7.0-xsl php7.0-json php7.0-intl php-pear php7.0-dev php7.0-common php7.0-mbstring php7.0-zip php-soap libcurl3 curl -y

Now edit the php.ini files for fpm and cli.

vim /etc/php/7.0/fpm/php.ini
vim /etc/php/7.0/cli/php.ini

and increase the memory limit and php execution time and turn on zlib compression by adding the following lines at the end of the files:

memory_limit = 512M
max_execution_time = 1800
zlib.output_compression = On

Save the file and exit the editor.

Restart the PHP-FPM service to apply the configuration changes:

systemctl restart php7.0-fpm

Step 3 - Install and Configure MariaDB

I will use MariaDB instead of MySQL here. Install MariaDB with the apt command:

apt-get install mariadb-server mariadb-client -y

Set the MariaDB root user password with this command:

mysqladmin -u root password mypassword

Set root password? [Y/n] Y
New password:
Re-enter new password: <-- Enter the new password

Remove anonymous users? [Y/n] Y
 ... Success!

Disallow root login remotely? [Y/n] Y
 ... Success!

Remove test database and access to it? [Y/n] Y

Reload privilege tables now? [Y/n] Y
 ... Success!

Then connect to the MySQL shell (the MariaDB shell gets started with the command mysql) with your root password, create a database with the name 'magentodb' and a user 'magentouser' with the password '[email protected]'. Please choose a secure password for the 'magentouser' on your server and not the one '[email protected]' that I used in this example!

Login to the MySQL shell:

mysql -u root -p

In the MySQL shell, run these commands:

create database magentodb;
create user [email protected] identified by '[email protected]';
grant all privileges on magentodb.* to [email protected] identified by '[email protected]';
flush privileges;

 Create the magento database

Database created and configured.

Step 4 - Install and Configure Magento 2

We will install Magento in the directory '/var/www/magento2'. For the Magento installation, we need the PHP composer.

- Install php composer

Go to the root directory, download the composer installer file with curl and run it to install composer.

cd ~/
curl -sS | php

Move the file 'composer.phar' file to the bin directory of your server and rename it to composer so it can be executed easily:

mv composer.phar /usr/bin/composer

Now verify that composer command is working:

composer -v

- Download and Extract Magento 2

Go to the web directory '/var/www/' and download Magento from it's Github repository, then unpack the downloaded tar.gz file:

cd /var/www/
tar -xzvf 2.0.7.tar.gz
mv magento2-2.0.7/ magento2/


- Configure the Magento Key

Register an account on the Magento website This account is required to use Magento and the Magento composer store. When you have registered, go to the Tab 'My Account > Developer > Secure Keys', next generate your keys.

Configure the magento key

- Install Third-party Components for Magento

Go to the Magento 2 installation directory '/var/www/magento2' and run the composer command:

cd /var/www/magento2/
composer install -v

You will be asked for the Magento authentication, use the public key as username  and the private key for the password.

Install magento with composer

- Configure the Nginx Virtualhost

Magento offers a ready-made Nginx virtual host configuration, so we just have to include it in our configuration.

Go to the Nginx virtual host directory and create new file called magento:

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

Paste configuration below:

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

server {

        listen 80;
        set $MAGE_ROOT /var/www/magento2;
        set $MAGE_MODE developer;
        include /var/www/magento2/nginx.conf.sample;

Replace with the domain name of the website that your shop shall use.

Save and exit.

Now activate the virtual host and restart Nginx:

ln -s /etc/nginx/sites-available/magento /etc/nginx/sites-enabled/
systemctl restart nginx

- Install Magento

We will install magento on the command line. In the Magento directory '/var/www/magento2/' there is binary file with the name 'magento' that is used to install and manage magento. Run the command:

bin/magento setup:install --backend-frontname="adminlogin" \
--key="biY8vdWx4w8KV5Q59380Fejy36l6ssUb" \
--db-host="localhost" \
--db-name="magentodb" \
--db-user="magentouser" \
--db-password="[email protected]" \
--language="en_US" \
--currency="USD" \
--timezone="America/New_York" \
--use-rewrites=1 \
--use-secure=0 \
--base-url="" \
--base-url-secure="" \
--admin-user=adminuser \
[email protected] \
[email protected] \
--admin-firstname=admin \
--admin-lastname=user \

backend-frontname = the admin page for our magento site, we use 'adminlogin'.
Key = our magento keys, we can generate it, or find it random on
Base-url = make sure it is same with virtual host configuration.

Magento installation finished

At the end of the installation procedure you should see these lines:

[SUCCESS]: Magento installation complete.
[SUCCESS]: Magento Admin URI: /adminlogin

Before we will test the Magento installation, ensure the web directory owner is 'www-data', then restart nginx.

cd /var/www/magento2/
chmod 700 /var/www/magento2/app/etc
chown -R www-data:www-data .
systemctl restart nginx

Now open the Magento domain in your browser:

In my case, the domain name is:

Magento home page

Try to log in to the Magento admin dashboard:

Magento admin login

Magento admin Dashboard

Note :

If you get an error about a missing Magento indexer cronjob, then you can solve it by adding the following cronjob to your server:

crontab -u www-data -e

Add the following lines:

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

Save and exit.

Magento 2 with Nginx and PHP-FPM 7 on Ubuntu 16.04 is installed now.


Magento is an open source e-commerce platform based on the PHP Zend Framework. It is a complex e-commerce software to help you with your online business. Magento uses an MVC (Model-View-Controller) architecture and MySQL or MariaDB as database. Magento is easy to install, we can install it with Nginx or Apache web server. Magento has become one of the most popular e-commerce software on the internet and is uses by many successful shop websites worldwide.

Share this page:

11 Comment(s)

Add comment

Please register in our forum first to comment.


By: Order confirmation email is not sent(magento2.1)


Website successfull installed on VPS( Nginx+PHP ver7.0.8+MariaDB+Ubuntu 16 +MAGENTO 2.1)

I want to send the email just when new order from customer.


But Email  the order is not sent by email any customer.

The "Order" and "Order Comments" setting in the System->Configuration->Sale Emails are Enabled.


What can I do? Please help me. Thank very much

By: BM

Have u setup postfix?

By: patrick

How to Magento root directory support PHP ? 

If I add a PHP file to root directory , How to modify nginx.confi 

By: Francis Rodrigues

Thank so much for the tutorial. I'm so thankfull but this nginx conf file is crash others configurations for same domain and others domains in my server. What's happening?

By: Stojan

Hello there,

Thanks for the great and on-spot tutorial.

One thing is, you need to put this into a code section:


This needs to be executed.

By: jbbankshot

This is nicely laid out! Thank you!

I did not have any issues executing all of this, but when I hit my browser to go to magento, I get sent to the internet vs bring up magento. Any suggestions on what I might have missed? I did use the 

By: Mike

Hi there,

Thank you very much for this excellent article. Yours is the best I have seen so far.

Just wondering if you could clarify the domain issue (also brought up by "jbbankshot") that I got stuck with.

I added to /etc/hosts:

( is local ngix server ip).

Local visit (on the Magento 2 server itself) seems runs fine(no gui installed, use curl):


I got correct html content in console.

Then when I open from Chrome on a Windows 10 machine (on the SAME lan):

It tried to visit internet instead of the Magento 2 home page on my local server.

I also tried this from Chrome on Windows 10:

Got: "404 Not Found"

Could you shed some light on how to tackle this?

Thanks in advance,


By: vishal sapkal

Hi,I did setup Magento2 with AWS RDS Behind load balncer, http working well. https not working getting error Blocked loading mixed active content. Connection also showing not secure. Regards,Vishal 

By: vishal sapkal

Can you answer anybody? 

By: M Faisal

Hi there,

I installed Magento2 using this guide. After the server went into production after months of development I started getting strange issues, like problems in installing modules, system upgrade etc. Then I came to know that one should never install a production server using composer. It is for testing/development purposes only.

I then got stuck as it was produciton site with lots of products/blog posts etc. Well I had to create a new server and then copy everything one by one... hard luck.

Secondly I've seen Magento2 is tightly integtrated with Apache. It gives strange problems with NginX when in production. It does run but it limps. My 2nd advice will be to run it on Apache if you want to save yourself headaches down the line. Though you would need more resources but it's better than not being even able to access Web Setup Wizard without a hack.

By: till

Using Composer is fine on production systems as well, it does not matter for a PHP file if you copy it manually or if it got copied by a composer script. Magento works fine on Nginx, many large Magento shops use it and it's also documented by the Magento developers how to run their software n Nginx.