How to install Magento 2 with Varnish, Apache and Pound as SSL Termination

In this article, we will show you how to install Magento 2 on Ubuntu 16.04 VPS with MariaDB, Varnish as full page cache, Apache and Pound as SSL Termination. This guide should work just as well on other Linux VPS systems, but has been tested and written for an Ubuntu 16.04 VPS.

Before we start, you can choose a different installation for your Magento. You can read the tutorial how to install Magento 2 on Ubuntu 16.04 VPS with MariaDB, PHP-FPM 7.0, Varnish as full page cache, Nginx, SSL Termination and Redis for session storage and page caching.

Or you can check out this tutorial to install Magento 2 on Ubuntu 16.04 VPS with MariaDB, Varnish as full page cache, Apache and Memcache for session storage.

1. Login and server update

First of all, you need to log into your Ubuntu 16.04 VPS via SSH with superuser rights:

ssh [email protected]_IP_address

It is also advised to start a screen session by running the following command

screen -U -S magento

System update and install required packages:

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

It is very important that you always keep your server up to date. You can even turn on automatic updates in order to achieve this.

2. Installing and configuring MariaDB

Install the latest 10.0 MariaDB server from the official Ubuntu repositories:

apt-get install -y mariadb-server

Next, we need to create a database for our Magento installation.

mysql -u root -p
MariaDB [(none)]> CREATE DATABASE magento;
MariaDB [(none)]> GRANT ALL PRIVILEGES ON magento.* TO 'magento'@'localhost' IDENTIFIED BY 'strong_password';
MariaDB [(none)]> q

Remember to replace “strong_password” with your actual strong password. You can even create a strong password from the command line.

3. Install Apache2 web server

apt-get install apache2

4. Install PHP and required PHP modules

To install the latest stable PHP version 7 and all required modules, run the command:

apt-get install php7.0 libapache2-mod-php7.0 php7.0-mbstring php7.0-curl php7.0-zip php7.0-gd php7.0-mysql php7.0-mcrypt php7.0-xsl php-imagick php7.0-gd php7.0-cli php-pear php7.0-intl

Changing a few PHP defaults:

sed -i "s/memory_limit = .*/memory_limit = 256M/" /etc/php/7.0/cli/php.ini
sed -i "s/upload_max_filesize = .*/upload_max_filesize = 128M/" /etc/php/7.0/cli/php.ini
sed -i "s/zlib.output_compression = .*/zlib.output_compression = on/" /etc/php/7.0/cli/php.ini
sed -i "s/max_execution_time = .*/max_execution_time = 18000/" /etc/php/7.0/cli/php.ini

Enable Apache2 rewrite module if not already done:

a2enmod rewrite

In order to activate the new configuration, restart the Apache web server using the following command:

service apache2 restart

5. Installing Composer

Composer is a dependency manager for PHP with which you can install packages. Composer will pull in all the required libraries and dependencies you need for your project.

curl -sS | php
mv composer.phar /usr/local/bin/composer

6. Installing Magento 2 from Github

Clone Magento repository to directory ~/AndreyExMagentoSite.ruusing the following command:

git clone /var/www/

Get the latest stable release of Magento 2:

cd /var/www/
git checkout $(git describe --tags $(git rev-list --tags --max-count=1))

Launch Composer and install all Magento dependencies:

composer install

To proceed with the installation, you can use the installation wizard or the command line, in this tutorial we will use the latter.

bin/magento setup:install 
[email protected] 

If the installation was successful, you will see something like this:

[SUCCESS]: Magento installation complete.
[SUCCESS]: Magento Admin URI: /admin_andreyex

Run the command crontabto create a cronjob

crontab -u www-data -e

and add the following line:

* * * * * /usr/bin/php /var/www/ cron:run | grep -v "Ran jobs by schedule" >> /var/www/

Finally, set the correct permissions:

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

7. Configuring Apache

Create a new virtual host directive in Apache. For example, you can create a new Apache configuration file called “magento.conf” on the server:

touch /etc/apache2/sites-available/magento.conf
ln -s /etc/apache2/sites-available/magento.conf /etc/apache2/sites-enabled/magento.conf
nano /etc/apache2/sites-available/magento.conf

Then add the following lines:

<VirtualHost *:80>
ServerAdmin [email protected]
DocumentRoot /var/www/
<Directory /var/www/>
Options FollowSymLinks
AllowOverride All
Order allow,deny
allow from all
ErrorLog /var/log/apache2/
CustomLog /var/log/apache2/ common

Restart the Apache web server for the changes to take effect:

service apache2 restart

You should now be able to log into your Magento admin area by going to the information you set at startup bin/magento setup:install

8. Installing and configuring Varnish

To install Varnish, run the following command:

apt-get install varnish

From your Magento admin panel click on STORES link (left sidebar) -> Configuration -> Advanced -> System -> Full page cache

Uncheck “Use system value” and from the list of Caching applications, select Varnish Cache (recommended), save the configuration, click on the Configure Varnish link and click on Export VCL for the Varnish 4 button. varnish.vclwhich we will be using will be exported to the directory /var/www/

Clear the Magento cache:

php bin/magento cache:flush

Delete /etc/varnish/default.vcl and create a symlink to your varnish configuration.

rm -f /etc/varnish/default.vcl
ln -sf /var/www/ /etc/varnish/default.vcl

In order to change the port in varnish from 6081 to 80, we need to change the configuration of the Systemd service.

Create a new file customexec.conf

mkdir -p /etc/systemd/system/varnish.service.d
nano /etc/systemd/system/varnish.service.d/customexec.conf

paste the following:

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

and restart the Systemd service

systemctl daemon-reload

Now we need to change the Apache listening port from 80 to 8080. To do this, open the Apache ports configuration file and change it as follows:

nano /etc/apache2/ports.conf

Listen 80 -> Listen 8080
nano /etc/apache2/sites-available/magento.conf

<VirtualHost *:80> -> <VirtualHost *:8080>

If everything is configured correctly, then you should be able to log into your Magento by following the link

9. Installing and Configuring Pound as SSL Termination

Varnish doesn’t support SSL traffic, so you’ll need to set up a proxy or load balancer like Pound to handle SSL traffic. To do this, we will configure Pound to only listen on port 443 and forward traffic to port 80 of Varnish.

First, let’s install Pound:

apt-get install pound

Open the Pound config file:

nano /etc/pound/pound.cfg

and set the following values:

    Address ## это должен быть ваш общедоступный IP-адрес сервера
    Port 443
    Cert "/etc/apache2/ssl/" ## путь к вашему SSL сертификату
    HeadRemove "X-Forwarded-Proto"
    AddHeader "X-Forwarded-Proto: https"
            Port 80

Once you’re done, restart Apache and Varnish and Pound:

service apache2 restart
service varnish restart
service pound restart

10. Further optimization

To further optimize your Magento installation, visit your Magento dashboard admin:

  1. Go to STORE -> Configuration -> Catalog -> Catalog -> Use flat catalog, select Yes and click the Save configuration button.
  2. Go to STORE -> Configuration -> Advanced -> Developer -> JavaScript Settings and set JavaScript merging and JavaScript minification to Yes and click the Save Configuration button.
  3. Go to STORE -> Configuration -> Advanced -> Developer -> CSS Options and set CSS Combine and Minify CSS Files to Yes and click the Save Configuration button.
  4. Consider using a CDN – Content Delivery Network

Don’t forget to clear your cache again:

php bin/magento cache:flush

You can also follow our guide on how to speed up Magento.

That’s all. You have successfully installed Magento 2 with Memcache as session storage and page caching, Varnish as full page caching and Apache on your Ubuntu 16.04 VPS, and you have configured Pound as an SSL Termination. For more information on how to manage your Magento installation, please refer to the official Magento documentation.