How to Setup Magento 2 With Varnish and Apache on Ubuntu 16.04

Magento is a free and open source Content Management System built in PHP, Zend framework and MySQL database. It is very popular eCommerce web application that runs on top of many open source applications such as Apache, MySQL and many more. Magento comes with lots of built-in modules that is used to add more functionality.

Varnish Cache is a powerful open source web application accelerator also known as a caching HTTP reverse proxy. It is used to speed up your website by caching a copy of a webpage the first time a user visits. Magento and Varnish is a known combination to increase site performance significantly. Varnish is integrated by default in Magento 2. You will only need to make some configuration changes to make it work.

In this tutorial, we will learn how to install Magento 2 with Varnish as a full page cache on Ubuntu 16.04 server.


  • A server running Ubuntu 16.04.
  • Apache, PHP, and MariaDB.
  • A non-root user with sudo privileges setup on your server.

1 Getting Started

Before starting, it is recommended to update your system with the latest package by running the following command:

sudo apt-get update -y
sudo apt-get upgrade -y

Once your system is up-to-date, restart your system and log in with sudo user.

2 Install LAMP Server

Magento won't function without installing LAMP stack (Apache, PHP, MariaDB). First, install Apache web server and varnish by running the following command:

sudo apt-get install apache2 varnish -y

After installing Apache, start Apache service and enable it to start on boot time by running the following command:

sudo systemctl start apache2
sudo systemctl enable apache2

Magento is written in PHP language, so you also need to install PHP with all the libraries to your system. You can install all of them by running the following command:

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

After installing PHP along with all required libraries, you will need to change few default PHP settings. You can do this by editing php.ini file:

sudo nano /etc/php/7.0/cli/php.ini

Change the following lines:

memory_limit = 512
upload_max_filesize = 128M
zlib.output_compression = on
max_execution_time = 15000

Save and close the file when you are finished.

3 Install and Configure MariaDB

Magento uses MariaDB as a database. So you will need to install it to your server. You can install it by just running the following command:

sudo apt-get install mariadb-server -y

After installing MariaDB, start MariaDB service and enable it to start at boot time by running the following command:

sudo systemctl start mysql
sudo systemctl enable mysql

By default, MariaDB is not secured, so you will need to secure it. You can secure it by running the following command:

sudo mysql_secure_installation

Answer all the questions as shown below:


In order to log into MariaDB to secure it, we'll need the current
password for the root user.  If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none): 
OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.

You already have a root password set, so you can safely answer 'n'.

Change the root password? [Y/n] n

 ... skipping.

By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n] Y
 ... Success!

Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n] Y
 ... Success!

By default, MariaDB comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n] Y
 - Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n] Y
 ... Success!

Cleaning up...

All done!  If you've completed all of the above steps, your MariaDB
installation should now be secure.

Thanks for using MariaDB!

Next, login to MariaDB shell with the following command:

mysql -u root -p

Enter your root password when prompt, then create a database for Magento with the following command:

MariaDB [(none)]>CREATE DATABASE magento_db;

After creating database for Magento, create a username, and password with the following command:

MariaDB [(none)]>CREATE USER 'magento'@'localhost' IDENTIFIED BY 'password';

Next, grant privileges to the Magento database with the following command:

MariaDB [(none)]>GRANT ALL PRIVILEGES ON magento_db.* TO 'magento'@'localhost';

Next, run the FLUSH PRIVILEGES command reloade the privileges:


Finally, exit from the MariaDB console with the following command:

MariaDB [(none)]>\q

4 Download Magento

First, you will need to download the latest version of the Magento from their official website.

After downloading Magento, extract the downloaded zip file to the apache web root directory with the following command:

sudo mkdir /var/www/html/magento/
sudo unzip -d /var/www/html/magento

Next, change the ownership of the magento directory and give proper permission to it:

sudo chown -R www-data:www-data /var/www/html/magento
sudo chmod -R 777 /var/www/html/magento

Next, you will need to install Composer to your system. Composer is a dependency manager that is used to install all required PHP dependencies. You can download and install Composer binary with the following command:

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

Next, run composer command to install all the dependencies required by Magento:

cd /var/www/html/magento
sudo composer install

Once all the dependencies are installed, you can proceed to configure Apache for Magento.

5 Configure Apache for Magento

Next, you will need to create an Apache virtual host file for Magento. Create a new Apache configuration file named magento.conf with the following command:

sudo nano /etc/apache2/sites-available/magento.conf

Add the following lines:

<VirtualHost *:8080>
ServerAdmin [email protected]
DocumentRoot /var/www/html/magento
<Directory "/var/www/html/magento/">
Options FollowSymLinks
AllowOverride All
Order allow,deny
allow from all
ErrorLog /var/log/apache2/magento-error_log
CustomLog /var/log/apache2/magento-access_log common

Save the file, then enable your virtual host with Apache rewrite module the following command:

sudo a2ensite magento
sudo a2enmod rewrite

Next, you will also need to change Apache listening port from 80 to 8080. Because we will reserve port 80 for varnish. To do so, open ports.conf file:

sudo nano /etc/apache2/ports.conf

Change the file as shown below:

Listen 8080

Finally, restart Apache service to apply this changes with the following command:

sudo systemctl restart apache2

6 Access Magento Web Installation Wizard

Magento is now installed, now it's time to install Magento through web browser.

To do so, open your web browser and type the URL, you should see the following page:

Magento Web Installation Wizard

Now, agree to Terms and Conditions then click on the "Agree and Setup Magento" button, you should see the following page:

Agree TOS

Check all the Readiness and click on the "Next" button, you should see the following page:

Database details

Provide your database details, like Database name, Database username and Password, then click on the "Next" button, you should see the following page:

website details

Provide your website details and click on the "Next" button, you should see the following page:

magento settings

Customize Magento as per your need and click on the "Next" button, you should see the following page:

admin user details

Provide your admin user details and click on the "Next" button, you should see the following page:

Install now

Now, click on the "Install Now" button to start Magento installation. Once the installation is completed you should see the following page:

Installaton successful

Note: Remember your Magento Admin URL: This will use to access Magento admin.

Now, click on "Launch Magento Admin" button, you should see the Magento login page as below:

Magento login

Provide your Magento admin credentials and click on the "Sign In" button, you should see the Magent-o Dashboard as below:

Magento Dashboard

From you Magento Admin dashboard click on the STORES button (leftside) > Configuration > ADVANCED > System > Full Page Cache, then Unselect use system value and select Varnish Cache from the Caching Application list, save the configuration, click on the Varnish Configuration link and click on the Export VCL for Varnish 4 button (this will export the varnish.vcl file in /var/www/html/magento/var directoy) as shown below:

Magento store configuration

Finally, flush the Magento cache with the following command:

cd /var/www/html/magento
sudo php bin/magento cache:flush

Once you are finished, you can proceed to configure varnish.

6 Configure Varnish

Magento is now installed and configured. It's time to configure varnish.

First, delete the /etc/varnish/default.vcl file and create a symlink from the exported varnish configuration:

sudo rm -rf /etc/varnish/default.vcl
sudo ln -s /var/www/html/magento/var/varnish.vcl /etc/varnish/default.vcl

Next, you will need to create a systemd service file for varnish. You can do this by copying varnish.service from /lib/systemd/system/ to /etc/systemd/system/ directory:

sudo cp /lib/systemd/system/varnish.service /etc/systemd/system/

After copying this file, you will need to make some changes in varnish.service file:

sudo nano /etc/systemd/system/varnish.service

Make the following changes:

Description=Varnish HTTP accelerator
Documentation= man:varnishd

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


Save the file when you are finished, then restart varnish service with the following command:

sudo systemctl daemon-reload
sudo systemctl reload varnish.service

If everything is working fine, then you should be able to login to your Magento back-end by typing the following URL on your web browser:

You can also check whether varnish is enabled or not by running the following command:

curl -I

You should see that varnish is enabled:

Date: Fri, 07 Jul 2017 17:10:01 GMT
Server: Apache/2.4.18 (Ubuntu)
Set-Cookie: store=default; expires=Sat, 07-Jul-2018 17:10:03 GMT; Max-Age=31536000; path=/; HttpOnly
Set-Cookie: PHPSESSID=irp2k8cmrhct0dfh18qk7ap0i4; expires=Fri, 07-Jul-2017 18:10:04 GMT; Max-Age=3600; path=/; domain=; HttpOnly
Expires: Thu, 07 Jul 2016 17:10:04 GMT
Cache-Control: max-age=0, must-revalidate, no-cache, no-store
Pragma: no-cache
X-Content-Type-Options: nosniff
X-XSS-Protection: 1; mode=block
X-Frame-Options: SAMEORIGIN
Content-Length: 0
Content-Type: text/html; charset=UTF-8
X-Varnish: 2
Age: 0
Via: 1.1 varnish-v4
Connection: keep-alive

Congratulations! you have successfully configured Magento with Varnish on Ubuntu 16.04 server.

Share this page:

Suggested articles

5 Comment(s)

Add comment


By: Fayyaz Khattak

Great and to the point tutorial. I was looking for the same thing and thanks to you that I have found it. Thank you for sharing such an informative blog post.

By: João Costa

If magento virtualhost is set to port 8080 and i configure on magento setup store url with ip instead of, it won't let me access



<<Directory "/var/www/html/magento/">SHOULD BE<Directory "/var/www/html/magento/">

By: sen

while running the "sudo systemctl reload varnish.service" this command in terminal I am getting the below error, I am using trying to enable the varnish cache in localhost

Job for varnish.service failed because the control process exited with error code

By: luis

Hey, I have a quick question is it possible to follow these steps for the Debian OS. I have seen a guide: https:// blog/ magento-2-memcached/ with Debian and web servers like Apache and NGINX. So will it be helpful?