Installing nginx is as simple as

1
2
sudo apt-get update
sudo apt-get install nginx

Now if you go to http://localhost you can see a welcome message from nginx. You can stop, start or restart it by using sudo /etc/init.d/nginx stop.

Next we have to install php7 (package maintained by ondrej. It will be a long time until ubuntu will release an oficial package, if ever.):

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

If you have php5 and you want to remove it:

1
sudo apt-get update && apt-get purge php5-fpm && apt-get --purge autoremove

Then install php7 and most common packages:

1
sudo apt-get install php7.0-fpm php7.0-mysql php7.0-curl php7.0-gd php7.0-json php7.0-mcrypt php7.0-opcache php7.0-xml php7.0-xsl php7.0-mbstring

and restart nginx (sudo /etc/init.d/nginx restart.).

Next step is to install mariadb:

1
sudo apt-get install mariadb-server

You will be asked for the password. Then stop the service to configure it:

1
2
sudo service mysql stop
sudo mysql_install_db

Start MariaDB:

1
sudo service mysql start

And now let’s secure MariaDB by removing the test databases and anonymous user created by default:

1
sudo mysql_secure_installation

And configure your root details. To check your installation run mysql -p.If you get an access denied run sudo mysql -p. If it works to login to mysql only as root then you have to create a normal user and give him access to the db:

1
2
3
CREATE USER 'computer_user'@'localhost' IDENTIFIED BY 'some_pass';
GRANT ALL PRIVILEGES ON *.* TO 'computer_user'@'localhost'
    ->     WITH GRANT OPTION;

Then you create your magento2 database: create database magento2; .

Next step is to configure the nginx configuration for our magento2 website. In /etc/nginx/sites-available/ create a new file called magento2.com, or whatever your site name is. In there copy these settings:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# Magento Vars
# set $MAGE_ROOT /path/to/magento/root;
# set $MAGE_MODE default; # or production or developer

 upstream fastcgi_backend {
    server   unix:/var/run/php/php7.0-fpm.sock;
 }
 server {
    listen 80;
    server_name magento2.com;
    set $MAGE_ROOT /home/user/sites/magento2;
    set $MAGE_MODE developer;
    root $MAGE_ROOT/pub;

     index index.php;
     autoindex off;
     charset off;

     add_header 'X-Content-Type-Options' 'nosniff';
     add_header 'X-XSS-Protection' '1; mode=block';

     location /setup {
         root $MAGE_ROOT;
         location ~ ^/setup/index.php {
             fastcgi_pass   fastcgi_backend;
             fastcgi_index  index.php;
             fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
             include        fastcgi_params;
         }

         location ~ ^/setup/(?!pub/). {
             deny all;
         }

         location ~ ^/setup/pub/ {
             add_header X-Frame-Options "SAMEORIGIN";
         }
     }

     location /update {
         root $MAGE_ROOT;

         location ~ ^/update/index.php {
             fastcgi_split_path_info ^(/update/index.php)(/.+)$;
             fastcgi_pass   fastcgi_backend;
             fastcgi_index  index.php;
             fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
             fastcgi_param  PATH_INFO        $fastcgi_path_info;
             include        fastcgi_params;
         }

         # deny everything but index.php
         location ~ ^/update/(?!pub/). {
             deny all;
         }

         location ~ ^/update/pub/ {
             add_header X-Frame-Options "SAMEORIGIN";
         }
     }

     location / {
         try_files $uri $uri/ /index.php?$args;
     }

     location /pub {
         location ~ ^/pub/media/(downloadable|customer|import|theme_customization/.*\.xml) {
             deny all;
         }
         alias $MAGE_ROOT/pub;
         add_header X-Frame-Options "SAMEORIGIN";
     }

     location /static/ {
         if ($MAGE_MODE = "production") {
             expires max;
         }
         location ~* \.(ico|jpg|jpeg|png|gif|svg|js|css|swf|eot|ttf|otf|woff|woff2)$ {
             add_header Cache-Control "public";
             add_header X-Frame-Options "SAMEORIGIN";
             expires +1y;

             if (!-f $request_filename) {
                 rewrite ^/static/(version\d*/)?(.*)$ /static.php?resource=$2 last;
             }
         }
         location ~* \.(zip|gz|gzip|bz2|csv|xml)$ {
             add_header Cache-Control "no-store";
             add_header X-Frame-Options "SAMEORIGIN";
             expires    off;

             if (!-f $request_filename) {
                rewrite ^/static/(version\d*/)?(.*)$ /static.php?resource=$2 last;
             }
         }
         if (!-f $request_filename) {
             rewrite ^/static/(version\d*/)?(.*)$ /static.php?resource=$2 last;
         }
         add_header X-Frame-Options "SAMEORIGIN";
     }

     location /media/ {
         try_files $uri $uri/ /get.php?$args;

         location ~ ^/media/theme_customization/.*\.xml {
             deny all;
         }

         location ~* \.(ico|jpg|jpeg|png|gif|svg|js|css|swf|eot|ttf|otf|woff|woff2)$ {
             add_header Cache-Control "public";
             add_header X-Frame-Options "SAMEORIGIN";
             expires +1y;
             try_files $uri $uri/ /get.php?$args;
         }
         location ~* \.(zip|gz|gzip|bz2|csv|xml)$ {
             add_header Cache-Control "no-store";
             add_header X-Frame-Options "SAMEORIGIN";
             expires    off;
             try_files $uri $uri/ /get.php?$args;
         }
         add_header X-Frame-Options "SAMEORIGIN";
     }

     location /media/customer/ {
         deny all;
     }

     location /media/downloadable/ {
         deny all;
     }

     location /media/import/ {
         deny all;
     }

     location ~ cron\.php {
         deny all;
     }

     location ~ (index|get|static|report|404|503)\.php$ {
         try_files $uri =404;
         fastcgi_pass   fastcgi_backend;

         fastcgi_param  PHP_FLAG  "session.auto_start=off \n suhosin.session.cryptua=off";
         fastcgi_param  PHP_VALUE "memory_limit=256M \n max_execution_time=600";
         fastcgi_read_timeout 600s;
         fastcgi_connect_timeout 600s;
         fastcgi_param  MAGE_MODE $MAGE_MODE;

         fastcgi_index  index.php;
         fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
         include        fastcgi_params;
     }
 }

Don’t forget to replace these 2 variables:

1
2
server_name magento2.com;
set $MAGE_ROOT /home/user/sites/magento2;

Now you must link the available sites to the enabled ones:

1
sudo ln -s /etc/nginx/sites-available/magento2.com /etc/nginx/sites-available/magento2.com

Then restart nginx: sudo /etc/init.d/nginx restart..

Now it is time to configure our magento2 files. If you’re starting from an already existing project just git clone your project, otherwise run a composer create:

1
composer create-project --repository-url=https://repo.magento.com/ magento/project-community-edition <installation directory name>

If you don’t have composer installed, you can install it easily:

1
curl -sS https://getcomposer.org/installer | sudo php -- --install-dir=/usr/local/bin --filename=composer

Go into your project folder and run a composer install. Read carefully the outputs and if a package is missing just install it.

The last step is to go to your /etc/hosts file and add (run sudo nano /etc/hosts):

1
127.0.0.1 magento2.com

This will point that website to localhost which is served by nginx.

The last step is to go to: http://magento2.com/setup and follow the installation instructions.

If you receive permission errors, then you must give privileges to your server for those directories, for example /var/generation or pub/static:

1
find /var/www/magento2/var/generation -type d -exec chmod g+s {} \;

That should be it.

Possible issues could be your fastcgi_split_path_info setting in php.ini which can be 1 if you’re not using it in production. If you get errors you should check /var/log/nginx/error.log to see exactly what went wrong, or check the magento error reports in the var/log folder.