Sunday, 9 July, 2017 UTC


Summary

This is a tutorial on how to install Magento 2 and set up the LEMP stack on Ubuntu 16.04 LTS. It is optimised for the AWS EC2, but will work on any Ubuntu 16.04 LTS.
First of all, you want to run and update & upgrade:
sudo apt update && sudo apt upgrade -y
Then install Nginx:
sudo apt install nginx -y
If you go to the IP address of your server, given that port 80 is open, you will be able to see the default Nginx screen:
Now it’s time to download Magento 2. My preferred way is to do it the old school way. I download a tarball or a zip file from https://magento.com/tech-resources/download and I use SCP to copy the file to the server and extract.
scp -i [privatekeyname].pem [file to upload] ubuntu@[IP or hostname of your server]:~/
For example:
scp -i foobar.pem Magento-CE-2.1.7-2017-05-30-01-36-53.zip [email protected]:~/
You should extract the archive file to /home/ubuntu/magento2 for the purposes of this tutorial. The next thing to do is to install PHP and MySQL:
sudo apt install mysql-server -y
When MySQL finishes installing, secure it by running the following command and follow the prompts:
sudo mysql_secure_installation
Now we need PHP and PHP-FPM to run. Ubuntu 16.04 LTS ships with PHP 7 by default:
sudo apt install php php-fpm php-mcrypt php-gd php-mysql php-curl php-xml php-soap php-mbstring php-zip -y
Let’s create a database for Magento 2:
mysql -u root -p[your password] -e "create database [your database name]"
For the purposes of this tutorial, I’ll name the database magento2 (and also the user). We need to create a new MySQL user besides root to assign to the magento2 database:
CREATE USER 'magento2'@'localhost' IDENTIFIED BY '[your password]';
GRANT ALL PRIVILEGES ON magento2 . * TO 'magento2'@'localhost';
FLUSH PRIVILEGES;
Note that there is no space between -p and the password. In certain cases, where symbols are used in your password, sometimes it’s easier to just use -p and don’t put the password in; MySQL will prompt you for the password.
Let’s increase the memory limit of PHP-FPM to 1024MB:
sudo vim /etc/php/7.0/fpm/php.ini
(you can of course use nano instead of vim)
Change: memory_limit = 128M to memory_limit = 1024M (it was line 389 for me). Save and exit.
Reload PHP 7 FPM to enable the new setting:
sudo service php7.0-fpm reload
Now let’s configure Nginx:
sudo cp nginx.conf.sample /etc/nginx/sites-available/magento
Edit the file to make slight changes:
# Example configuration:

server {

	listen 80;
	server_name mage.dev;
	set $MAGE_ROOT /home/ubuntu/magento2;

	root $MAGE_ROOT;

	index index.php;
	autoindex off;
	charset UTF-8;
	error_page 404 403 = /errors/404.php;
	#add_header "X-UA-Compatible" "IE=Edge";

	# PHP entry point for setup application
	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";
		}
	}

	# PHP entry point for update application
	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 ~ ^/pub/static/version {

                        rewrite ^/pub/static/(version\d*/)?(.*)$ /pub/static/$2 last;
                }
	}

	location /static/ {

		# Uncomment the following line in production mode
		# 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;
	}

	# PHP entry point for main application
	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_read_timeout 600s;
		fastcgi_connect_timeout 600s;

		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_buffers 16 8k;
	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
	image/svg+xml;
	gzip_vary on;

	# Banned locations (only reached if the earlier PHP entry point regexes don't match)
	location ~* (\.php$|\.htaccess$|\.git) {

		deny all;
	}
}
Remove the default nginx config file (which is a symlink) and create a symlink for the Magento 2 Nginx config file:
sudo rm /etc/nginx/sites-enabled/default
sudo ln -s /etc/nginx/sites-available/magento /etc/nginx/sites-enabled/magento
Now, Magento 2 should be nearly ready to be rendered via the web browser. Before we visit it, let’s make sure all the files are assigned to the proper user which in this case, is www-data:
cd /home/ubuntu/magento2
sudo chown www-data:www-data -R *
Let’s check the public IP of your server:
curl icanhazip.com
Now copy that IP address into the browser of your choice (mine is Chrome) and you should see the following if you are successful!
Magento 2 is ready to begin the setup process
I passed all the Magento 2 Readiness Checks thanks to my own tutorial:
Magento 2 Readiness Check
Now fill in the database details (created previously):
The rest of it is quite straightforward – just follow the prompts.
When the setup was complete, my Magento 2 static files were giving me 404s:
The public / static files did not generate for my locale which is en_AU. If you’re using a locale / language other than the default en_US – you have to run the following steps. First, change your user to www-data:
sudo su -s /bin/bash www-data
Then run a chain of commands to generate your static files:
php bin/magento setup:upgrade && php bin/magento setup:static-content:deploy --language en_AU && php bin/magento indexer:reindex && php bin/magento cache:clean && php bin/magento cache:flush
Replace en_AU with your locale code. That’s it!
Magento 2.1.7 is successfully set up & running
The post How to Install Magento 2 (LEMP Stack) on Ubuntu 16.04 LTS appeared first on Francis Kim.