Install Magento2 on Ubuntu (Nginx, Mysql-Server, PHP5.6, Varnish Cache)

Magento2 on EC2 instance with Nginx and Varnish Caching server

Here I am going to tell you the steps to install Magento2 on EC2 instance with Nginx and Varnish Caching server.

Few Steps are below tested on Ubuntu EC2 aws instance/VPS.

Step 1-:
Install required packages

$ sudo apt-get install software-properties-common
$ sudo add-apt-repository -y ppa:ondrej/mysql-5.6
$ sudo add-apt-repository -y ppa:ondrej/php5-5.6
$ sudo apt-add-repository -y ppa:duggan/composer
$ sudo apt-get update
$ sudo apt-get install -y php5-fpm nginx varnish php5-gd php5-intl php5-mcrypt php5-xsl mysql-server php5-curl php5-json php5-composer
$ sudo php5enmod mcrypt

Step 2-:
Create a database to install magento2 .

mysql -p -e "create database magento2"

Step 3-:
Varnish server setup.

sudo nano /etc/default/varnish

Default configuration replace from

DAEMON_OPTS="-a :6081 \
-T localhost:6082 \
-f /etc/varnish/default.vcl \
-S /etc/varnish/secret \
-s malloc,256m"

To

DAEMON_OPTS="-a :80 \
             -T localhost:6082 \
             -f /etc/varnish/default.vcl \
             -S /etc/varnish/secret \
             -s malloc,1024m"

Check default.vcl file port

nano /etc/varnish/default.vcl

Output-:

backend default {
.host = "127.0.0.1";
.port = "8080";
}

Step 4-:
Configure the nginx site-available default config.

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

Open the file

nano /etc/nginx/sites-available/default

Paste the below content into new default file.

# Default server configuration
#
server {
listen 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:/var/run/php5-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:/var/run/php5-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:/var/run/php5-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;
}
}
sudo mkdir -p /var/www/html

Step 5-:
Install Magento 2.0.4

$ 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 error comes 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: 2eacdaabf6d0ae454ca5c3d9b381b27d
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 6-:
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

 

One response to “Install Magento2 on Ubuntu (Nginx, Mysql-Server, PHP5.6, Varnish Cache)”

  1. leafcart says:

    Thanks for the post.

    Its working for me.

Leave a Reply