Install Magento2 on Ubuntu-LTS-16.04 (Nginx Varnish php7 MySql-Server) LEMP

admin/ May 11, 2016/ Linux, Magento2/ 0 comments

Magento2 (M2) installation on Ubuntu LTS 16.04 with Nginx, Varnish, MySql Server.

Nginx

Nginx has grown in popularity since its release due to its light-weight resource utilization and its ability to scale easily on minimal hardware. Nginx excels at serving static content quickly and is designed to pass dynamic requests off to other software that is better suited for those purposes.

Nginx is often selected by administrators for its resource efficiency and responsiveness under load. Advocates welcome Nginx’s focus on core web server and proxy features.

Varnish

Varnish is an HTTP accelerator and a useful tool for speeding up a server, especially during a times when there is high traffic to a site. It works by redirecting visitors to static pages whenever possible and only drawing on the virtual private server itself if there is a need for an active process.

Lets Start Magento2 Setup.

Step 1-: Installation of required Packages.

$ sudo apt-get update
$ sudo apt-get install -y php mysql-server varnish nginx  php-curl php-dom php-xsl php-intl php-mbstring php-zip php-simplexml

Note-: The above installation commands will install Php-7.0, MySql-5.7, Varnish-4.1, Nginx-1.10 (all required repository already in Ubuntu-16.04).

Step 2-: Create Database, Configure php, Configure Nginx and Varnish Server.

a)  We need to create a database for Magento2 installation.

$ mysql -u root -p'your-root-password' -e "create database <database name>"

For Example-:

$ mysql -u root -p'magentoroot' -e "create database magento2"

b) The Nginx webserver execute the php with php-fpm module and Magento2 requires 1024 MB memory limit.

Open the php.in of php-fpm module using below commands.

$ sudo nano /etc/php/7.0/fpm/php.ini

Update the  memory_limit = 128M to  memory_limit = 1024M

c) Make Backup of the default file of nginx configuration and use the below configuration.

Make a backup of existing file.

$ sudo  mv /etc/nginx/sites-available/default /etc/nginx/sites-available/default_org

Create a new default file and paste the below content.

$ sudo nano /etc/nginx/sites-available/default
# Default server configuration
#
server {
listen 127.0.0.1:8080 default_server;
listen [::]:8080 default_server;

set $MAGE_ROOT /var/www/html/magento2/;
set $MAGE_MODE developer;

# Add index.php to the list if you are using PHP
index index.html index.htm index.nginx-debian.html;

server_name _;
root $MAGE_ROOT/pub;

index index.php;
autoindex off;
charset off;

location /setup {
root $MAGE_ROOT;
location ~ ^/setup/index.php {
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
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 unix:/run/php/php7.0-fpm.sock;
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;
}

# Remove signature of the static files that is used to overcome the browser cache
location ~ ^/static/version {
rewrite ^/static/(version\d*/)?(.*)$ /static/$2 last;
}

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 ~ (index|get|static|report|404|503)\.php$ {
try_files $uri =404;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
fastcgi_buffers 1024 4k;

fastcgi_param PHP_FLAG "session.auto_start=off \n suhosin.session.cryptua=off";
fastcgi_param PHP_VALUE "memory_limit=768M \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;
}

gzip on;
gzip_disable "msie6";

gzip_comp_level 6;
gzip_min_length 1100;
gzip_proxied any;
gzip_types
text/plain
text/css
text/js
text/xml
text/javascript
application/javascript
application/x-javascript
application/json
application/xml
application/xml+rss;

location ~ \.php$ {
deny all;
}
}

d) Configure the Varnish Cache Server For Magento2.

$ sudo cp /lib/systemd/system/varnish.service /etc/systemd/system/

Open the varnish.service file and make the below configuration.

$ sudo nano /etc/systemd/system/varnish.service

Modify the line as below

ExecStart=/usr/sbin/varnishd -j unix,user=vcache -F -a :80 -T localhost:6082 -f /etc/varnish/default.vcl -S /etc/varnish/secret -s malloc,1024m

Update  -a :6081 to -a 80 and 256m to 1024m

Open the default.vcl file and update the port as below-:

$ sudo nano /etc/varnish/default.vcl
backend default {
    .host = "127.0.0.1";
    .port = "8080";
}

Restart the Nginx, Varnish, php-fpm service using below commands.

$ sudo systemctl restart nginx
$ sudo  systemctl restart varnish
$ sudo systemctl restart php7.0-fpm

Step 3-: Magento2 (M2) download and installations.

$ cd /var/www/html/
$ git clone https://github.com/magento/magento2.git
$ cd magento2/
$ git checkout tags/2.0.4
$ git pull
$ sudo chmod -R 777 /var/www/html/magento2/
$ sudo service nginx restart
$ sudo service php5-fpm
$ composer install

If you found prompt authentication like below-:

Loading composer repositories with package information
Installing dependencies (including require-dev) from lock file
- Installing magento/magento-composer-installer (0.1.6)
Downloading: 100%

- Installing braintree/braintree_php (2.39.0)
Downloading: 100%

- Installing justinrainbow/json-schema (1.6.1)
Downloading: 100%

- Installing symfony/console (v2.6.13)
Downloading: 100%

- Installing symfony/process (v2.8.4)
Downloading: 100%

- Installing symfony/finder (v2.8.4)
Downloading: 100%

- Installing seld/jsonlint (1.4.0)
Downloading: 100%

- Installing composer/composer (1.0.0-alpha10)
Downloading: 100%

- Installing magento/composer (1.0.2)
Authentication required (repo.magento.com):
Username: 
Password:

Login here https://www.magentocommerce.com/

Click on “My Account” on right top.

Magento2

Magento Securekey

Use the Public key as UserName and Private Key as Password.

Step 4-:After install all dependencies by composer install command.
Change the required permission

$ sudo chmod -R 755 /var/www/html/magento2/
$ sudo chmod -R 777 /var/www/html/magento2/var/
$ sudo chmod -R 777 /var/www/html/magento2/pub/

Open the Web Browser ( Chrome, Firefox etc)

Follow the Magento2 installation Steps.

http://localhost/setup/
http://<ServerIP>/setup/

Schedule Magento2 Cronjobs
Finally schedule the backgound cronjobs for your magento2 installation. These cronjobs does some activities like, re-indexing, Newsletters, Update of currency rates, sending automatic emails and generating sitemaps etc. To schedule these jobs edit crontab file

$ crontab -e

and add below cronjobs at the end of file and save it.

*/1 * * * * www-data php /var/www/html/magento2/bin/magento cron:run
*/1 * * * * www-data php /var/www/html/magento2/update/cron.php
*/1 * * * * www-data php /var/www/html/magento2/bin/magento setup:cron:run

 

Gotechnies Magento 2 Blog


Rating: 4.7
Votes: 370
Reviews: 340

Leave a Reply