Install Magento2 (M2) with LEMP (nginx) on Ubuntu

admin/ April 12, 2016/ Linux, Magento2/ 0 comments

There are few steps to install Magento2 with nginx on Ubuntu.

Why 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.

Step 1-:
Install the require packages.

$ sudo apt-get -y install nginx php5 php5-fpm php5-intl php5-mysql php5-xsl php5-mcrypt php5-gd mysql-server git
$ sudo php5enmod mcrypt
$ sudo nano /etc/php5/php5-fpm/php.ini

 
add the below line bottom of the file or remove the comments quote from php.ini
always_populate_raw_post_data = -1

To install composer on ubuntu 14.04 and above.

 $ sudo apt-get install composer

To install composer on ubuntu 12.04.

Add the PPA:

$ apt-add-repository -y ppa:duggan/composer
$ apt-get update

Then install with:

$ sudo apt-get install php5-composer

Step 2-:
Copy the original default file on nginx .

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

add the below lines in default file.

# Default server configuration
#
server {
	listen 80 default_server;
	listen [::]:80 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;
}
	}

Save the default file.

Step 3-:
Checkout the magento2 source code from github.com.

$ sudo cd /var/www/html/
$ git clone https://github.com/magento/magento2.git 
$ sudo cd /var/www/html/magento2/
$ sudo composer install
$ sudo chown -R www-data:www-data /var/www/html/magento2/
$ sudo chmod -R 777 /var/www/html/magento2/

Step 4-:
Create Database
Now login to your mysql server with admin privileges and create a database and user for new magento 2 installation.

$ mysql -u root -p

mysql> CREATE DATABASE magento2_db;
mysql> GRANT ALL ON magento2_db.* TO magento2_usr@'localhost' IDENTIFIED BY 'password';
mysql> FLUSH PRIVILEGES;
mysql> quit

Step 5-:

$ sudo service nginx restart 
$ sudo service php5-fpm restart

Follow the Magento Installer Steps.
Your Magento2 Setup is ready.

http://<server ip/domain name >
http://< localhost >

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

Leave a Reply