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

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)]> \q

STEP 6: Clone and Install Magento2

Run below commands to clone magento from github

sudo git clone /var/www/

Install Magento

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

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/ cron:run | grep -v "Run magento jobs" >> /var/www/

Change file and folder ownership

sudo chown -R www-data: /var/www/

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/

Paste below contents

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

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

access_log /var/log/nginx/;
error_log /var/log/nginx/;

include /var/www/;

Create symlink

sudo ln -s /etc/nginx/sites-available/ /etc/nginx/sites-enabled/

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/ /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

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/

Make the following changes in config file

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

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

access_log /var/log/nginx/;
error_log /var/log/nginx/;

include /var/www/;

server {

listen 443 ssl http2;

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_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=""
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/

Make following changes

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

'session' =>
array (
'save' => 'redis',
'redis' =>
array (
'host' => '',
'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' =>
'frontend' =>
'default' =>
'backend' => 'Cm_Cache_Backend_Redis',
'backend_options' =>
'server' => '',
'port' => '6379'
'page_cache' =>
'backend' => 'Cm_Cache_Backend_Redis',
'backend_options' =>
'server' => '',
'port' => '6379',
'database' => '1',
'compress_data' => '0'

Flush Cache

sudo php bin/magento cache:flush

Add a Comment

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