Install Magento2 on AWS EC2 with Redis, varnish and Nginx as SSL Terminator

Through this article I will show you how you can install magento2 on AWS EC2 with redis cache, varnish cache and nginx for ssl termination. I am assuming you already know how to set up a vm on AWS EC2 and you are already logged in through ssh. If you do not know how to login through ssh to your ec2 instance then please refer – how to connect to aws EC2 through ssh.

STEP 1: Update and Install Software Packages

Run below commands to update and install software packages.


sudo apt-get update && sudo apt-get -y upgrade
sudo apt-get -y install curl nano git

 

STEP 2: Install PHP and other dependencies

Run below commands to install PHP and PHP extensions


sudo apt-get -y install php-fpm php-cli php-gd php-imagick php-mysql php-mcrypt php-pear php-curl php-intl php-xsl php-zip php-mbstring

STEP 3: Change PHP.ini Settings

Run below commands to change few settings in php.ini.


sudo sed -i "s/memory_limit = .*/memory_limit = 256M/" /etc/php/7.0/fpm/php.ini
sudo sed -i "s/upload_max_filesize = .*/upload_max_filesize = 20M/" /etc/php/7.0/fpm/php.ini
sudo sed -i "s/zlib.output_compression = .*/zlib.output_compression = on/" /etc/php/7.0/fpm/php.ini
sudo sed -i "s/max_execution_time = .*/max_execution_time = 1800/" /etc/php/7.0/fpm/php.ini

STEP 4: Install Composer

Run below commands to intsall composer


curl -sS https://getcomposer.org/installer | php
sudo mv composer.phar /usr/local/bin/composer

STEP 5: Install Mariadb database

Run below commands to install mariadb.


sudo apt-get install -y mariadb-server
mysql_secure_installation

Connect to database


mysql -uroot -p

Create user, database and allow grant permission to user for database


MariaDB [(none)]> CREATE DATABASE magentosite;
MariaDB [(none)]> GRANT ALL PRIVILEGES ON magentosite.* TO 'magentouser'@'localhost' IDENTIFIED BY 'password';
MariaDB [(none)]> FLUSH PRIVILEGES;
MariaDB [(none)]> \q

STEP 6: Clone and Install Magento2

Run below commands to clone magento from github


sudo git clone https://github.com/magento/magento2.git /var/www/magentosite.com

Install Magento


cd /var/www/magentosite.com
sudo git checkout $(git describe --tags $(git rev-list --tags --max-count=1))
sudo composer install
sudo bin/magento setup:install \
--base-url=http://magentosite.com/ \
--db-host=localhost \
--db-name=magentosite \
--db-user=magentouser \
--db-password=password \
--admin-firstname=First \
--admin-lastname=Last \
--admin-email=user@magentosite.com \
--admin-user=admin \
--admin-password=adminpassword \
--language=en_US \
--currency=USD \
--timezone=America/Chicago \
--use-rewrites=1

On successful installation you will get success message as below


[SUCCESS]: Magento installation complete.
[SUCCESS]: Admin Panel URI: /admin_pux59q

Setup Cron job and File ownership


crontab -u www-data -e
* * * * * /usr/bin/php /var/www/magentosite.com/bin/magento cron:run | grep -v "Run magento jobs" >> /var/www/magentosite.com/var/log/magento.cron.log

Change file and folder ownership

sudo chown -R www-data: /var/www/magentosite.com

STEP 7: Install Nginx Webserver

Run below command to install nginx


sudo apt-get -y install nginx

Create and open config file for site

 sudo nano /etc/nginx/sites-available/magentosite.com

Paste below contents


upstream fastcgi_backend {
server unix:/run/php/php7.0-fpm.sock;
}

server {
server_name magentosite.com www.magentosite.com;
listen 80;
set $MAGE_ROOT /var/www/magentosite.com;
set $MAGE_MODE developer; # or production

access_log /var/log/nginx/magentosite.com-access.log;
error_log /var/log/nginx/magentosite.com-error.log;

include /var/www/magentosite.com/nginx.conf.sample;
}

Create symlink


sudo ln -s /etc/nginx/sites-available/magentosite.com /etc/nginx/sites-enabled/magentosite.com

Remove default config


sudo rm -f /etc/nginx/sites-enabled/default

Do nginx config test to verify the rules you have written in config file are valid. Please correct the rules if it results in error.


sudo nginx -t

Restart Nginx webserver


sudo service nginx restart

STEP 8: Install and Setup Varnish

Run below commands to install and setup varnish


sudo apt-get install varnish
sudo php bin/magento cache:flush
sudo rm -f /etc/varnish/default.vcl
sudo ln -sf /var/www/myMagentoSite.com/var/varnish.vcl /etc/varnish/default.vcl
sudo mkdir -p /etc/systemd/system/varnish.service.d

Open customexec

sudo nano /etc/systemd/system/varnish.service.d/customexec.conf

Write below contents


[Service]
ExecStart=
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,256m

Reload the daemon


sudo systemctl daemon-reload

STEP 9: Update the config file for ports, ssl and other changes after varnish installation

Open config file


sudo nano /etc/nginx/sites-available/magentosite.com

Make the following changes in config file


upstream fastcgi_backend {
# Socket path
server unix:/run/php/php7.0-fpm.sock;
}

server {
server_name magentosite.com www.magentosite.com;
listen 8080;
set $MAGE_ROOT /var/www/magentosite.com;
set $MAGE_MODE production; # or developer

access_log /var/log/nginx/magentosite.com-access.log;
error_log /var/log/nginx/magentosite.com-error.log;

include /var/www/magentosite.com/nginx.conf.sample;
}

server {

listen 443 ssl http2;
server_name magentosite.com www.magentosite.com;

ssl_certificate /etc/ssl/certs/ssl-cert-snakeoil.pem; # change with your SSL cert
ssl_certificate_key /etc/ssl/private/ssl-cert-snakeoil.key; # change with your SSL key
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers 'AES128+EECDH:AES128+EDH:!aNULL';
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 24h;
keepalive_timeout 300s;

location / {
proxy_pass http://127.0.0.1;
proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Ssl-Offloaded "1";
proxy_set_header X-Forwarded-Proto https;
proxy_set_header X-Forwarded-Port 443;
#proxy_hide_header X-Varnish;
#proxy_hide_header Via;
proxy_set_header X-Forwarded-Proto $scheme;

}

}

Restart nginx webserver and varnish cache


sudo systemctl restart nginx
sudo systemctl restart varnish

Set magento base url and flush cache


sudo bin/magento setup:store-config:set --base-url="https://magentosite.com"
sudo php bin/magento cache:flush

STEP 10: Install and Setup Redis


apt-get install php-redis redis-server

Open env file to make changes for redis


sudo nano /var/www/myMagentoSite.com/app/etc/env.php

Make following changes


'session' =>
array (
'save' => 'files',
),

'session' =>
array (
'save' => 'redis',
'redis' =>
array (
'host' => '127.0.0.1',
'port' => '6379',
'password' => '',
'timeout' => '2.5',
'persistent_identifier' => '',
'database' => '0',
'compression_threshold' => '2048',
'compression_library' => 'gzip',
'log_level' => '1',
'max_concurrency' => '6',
'break_after_frontend' => '5',
'break_after_adminhtml' => '30',
'first_lifetime' => '600',
'bot_first_lifetime' => '60',
'bot_lifetime' => '7200',
'disable_locking' => '0',
'min_lifetime' => '60',
'max_lifetime' => '2592000'
)
),

'cache' =>
array(
'frontend' =>
array(
'default' =>
array(
'backend' => 'Cm_Cache_Backend_Redis',
'backend_options' =>
array(
'server' => '127.0.0.1',
'port' => '6379'
),
),
'page_cache' =>
array(
'backend' => 'Cm_Cache_Backend_Redis',
'backend_options' =>
array(
'server' => '127.0.0.1',
'port' => '6379',
'database' => '1',
'compress_data' => '0'
)
)
)
),

Flush Cache


sudo php bin/magento cache:flush
Please follow and like us:
20

Add a Comment

Your email address will not be published. Required fields are marked *