Set Up Magento 2 with Redis, Varnish, and Nginx SSL Termination
This guide covers installing and configuring Magento 2 on Ubuntu with MariaDB, PHP-FPM, Redis for caching, Varnish as a full-page cache, and Nginx handling SSL termination as a reverse proxy in front of Varnish.
Prerequisites
- Ubuntu server with sudo access
- Domain name pointed at your server IP
- Magento Marketplace account with access keys
Steps
1. Update System and Install Utilities
sudo apt-get update && sudo apt-get -y upgrade
sudo apt-get -y install curl nano git
2. Install MariaDB and Create Database
sudo apt-get install -y mariadb-server
mysql_secure_installation
mysql -uroot -p
CREATE DATABASE magento;
GRANT ALL PRIVILEGES ON magento.* TO 'magento'@'localhost' IDENTIFIED BY 'StrongPass123';
FLUSH PRIVILEGES;
\q
3. Install PHP 7.x and Required 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
Tune PHP settings:
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 = 128M/" /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 = 18000/" /etc/php/7.0/fpm/php.ini
4. Install Composer
curl -sS https://getcomposer.org/installer | php
sudo mv composer.phar /usr/local/bin/composer
5. Install Magento 2 from GitHub
sudo git clone https://github.com/magento/magento2.git /var/www/myMagentoSite.com
cd /var/www/myMagentoSite.com
sudo git checkout $(git describe --tags $(git rev-list --tags --max-count=1))
sudo composer install
6. Run Magento Installer
sudo bin/magento setup:install --base-url=http://myMagentoSite.com/ --db-host=localhost --db-name=magento --db-user=magento --db-password=StrongPass123 --admin-firstname=First --admin-lastname=Last --admin-email=user@myMagentoSite.com --admin-user=admin --admin-password=AdminPass123 --language=en_US --currency=USD --timezone=America/Chicago --use-rewrites=1
7. Set Up Cron
crontab -u www-data -e
# Add:
* * * * * /usr/bin/php /var/www/myMagentoSite.com/bin/magento cron:run 2>&1
8. Configure Redis
sudo apt-get install -y redis-server
# Configure Magento to use Redis for sessions and cache via bin/magento commands or env.php
9. Install and Configure Varnish
Generate the Magento VCL and apply it to Varnish, then configure Nginx to listen on 443 and proxy to Varnish on port 80 (or 6081).
bin/magento varnish:vcl:generate --export-version=6 > /etc/varnish/default.vcl
systemctl restart varnish
10. Configure Nginx as SSL Terminator
server {
listen 443 ssl http2;
server_name myMagentoSite.com;
ssl_certificate /etc/ssl/myMagentoSite.com.crt;
ssl_certificate_key /etc/ssl/myMagentoSite.com.key;
location / {
proxy_pass http://127.0.0.1:6081;
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 X-Forwarded-Proto https;
proxy_set_header X-Forwarded-Port 443;
proxy_set_header Ssl-Offloaded "1";
}
}
Verify
- Browse to your store URL over HTTPS
- Check Varnish is caching:
varnishstatorvarnishlog - Confirm Redis sessions:
redis-cli keys '*'
Notes
- In Magento Admin, go to Stores > Configuration > Advanced > System > Full Page Cache and set caching application to Varnish.
- Run
bin/magento cache:flushafter configuration changes.