There is a new version of this tutorial available for Ubuntu 24.04 (Noble Numbat).

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.

Prerequisites:

  • 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
mysql_secure_installation

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 'magentouser@'. Please choose a secure password for the 'magentouser' on your server and not the one 'magentouser@' 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 magentouser@localhost identified by 'magentouser@';
grant all privileges on magentodb.* to magentouser@localhost identified by 'magentouser@';
flush privileges;
\q

 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 https://getcomposer.org/installer | 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/
wget https://github.com/magento/magento2/archive/2.0.7.tar.gz
tar -xzvf 2.0.7.tar.gz
mv magento2-2.0.7/ magento2/

Done.

- Configure the Magento Key

Register an account on the Magento website repo.magento.com. 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;
        server_name www.newmagento.com;
        set $MAGE_ROOT /var/www/magento2;
        set $MAGE_MODE developer;
        include /var/www/magento2/nginx.conf.sample;
}

Replace www.newmagento.com 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="magentouser@" \
--language="en_US" \
--currency="USD" \
--timezone="America/New_York" \
--use-rewrites=1 \
--use-secure=0 \
--base-url="http://www.newmagento.com" \
--base-url-secure="https://www.newmagento.com" \
--admin-user=adminuser \
--admin-password=admin123@ \
[email protected] \
--admin-firstname=admin \
--admin-lastname=user \
--cleanup-database

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 http://randomkeygen.com/.
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: www.newmagento.com.

Magento home page

Try to log in to the Magento admin dashboard:

www.newmagento.com/adminlogin

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.

Conclusion

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)