How to install Magento 2 with Apache, Varnish and Memcache

In this article, we will show you how to install Magento 2 on an Ubuntu 16.04 VPS with MariaDB, Varnish as full page cache, Apache and Memcache for session storage. 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 watch this tutorial to install Magento 2 on Ubuntu 16.04 VPS with MariaDB, PHP-FPM 7.0, Varnish as full page cache, Nginx, SSL terminator and Redis for session storage and page caching.


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

                      ssh [email protected]_address

At the very beginning, it is best to start a screen session by running the following command:

                      screen -U -S magento

Updating the system and installing the required packages:

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

Make sure to always keep your server up to date. You can even turn on automatic updates.

Installing and configuring MariaDB 10.1

To add the MariaDB repository to the source list and install the latest MariaDB server, run the following commands:

                      apt-get install software-properties-common
apt-key adv --recv-keys --keyserver hkp:// 0xF1656F24C74CD1D8
add-apt-repository 'deb [arch=amd64,i386,ppc64el] xenial main'

After that, as the key is imported and the repository is added, you can install MariaDB:

                      apt-get update && apt-get -y upgrade
apt-get install mariadb-server

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

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

Remember to replace “my_strong_password” with a strong password.

Installing Apache2 web server

                      [destroyer]$ sudo apt-get install apache2

Install PHP and required PHP modules

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

                      [destroyer]$ 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:

                      [destroyer]$ sed -i "s/memory_limit = .*/memory_limit = 256M/" /etc/php/7.0/cli/php.ini
[destroyer]$ sed -i "s/upload_max_filesize = .*/upload_max_filesize = 128M/" /etc/php/7.0/cli/php.ini
[destroyer]$ sed -i "s/zlib.output_compression = .*/zlib.output_compression = on/" /etc/php/7.0/cli/php.ini
[destroyer]$ 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:

                      [destroyer]$ a2enmod rewrite

To activate the new configuration, restart the Apache web server using the following command:

                      [destroyer]$ service apache2 restart

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.

                      [destroyer]$ curl -sS | php
mv composer.phar /usr/local/bin/composer

Installing Magento 2 from Github

Clone Magento repository into directory ~/ using the following command:

                      [destroyer]$ git clone /var/www/

Get the latest stable release of Magento 2:

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

Run composer and install all Magento dependencies:

                      [destroyer]$ 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.

                      [destroyer]$ bin/magento setup:install 
[email protected] 

If the installation was successful, you will see something like the one shown below:

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

Run the command crontab to 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:

                      [destroyer]$ chown -R www-data: /var/www/

Apache configuration

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

                      [destroyer]$ touch /etc/apache2/sites-available/magento.conf
[destroyer]$ ln -s /etc/apache2/sites-available/magento.conf /etc/apache2/sites-enabled/magento.conf
[destroyer]$ 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:

                      [destroyer]$ sudo service apache2 restart

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

Installing and Configuring Varnish

Install Varnish by running the following command:

                      [destroyer]$ apt-get install varnish

Go to your Magento admin area, click on the STORES link (left sidebar) -> Configuration -> Advanced -> System -> Full Cache page

Uncheck System Usage and from the Caching Applications list, select Varnish Cache (Recommended), save your configuration, click on the Varnish Setup link and click on Export VCL for the Varnish 4 button. varnish.vcl which we will be using will be exported to the directory /var/www/

Clear Magento cache:

                      [destroyer]$ php bin/magento cache:flush

Delete /etc/varnish/default.vcl  and symlinks of the exported varnish configuration.

                      [destroyer]$ rm -f /etc/varnish/default.vcl
[destroyer]$ ln -sf /var/www/ /etc/varnish/default.vcl

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

Create a new file customexec.conf

                      [destroyer]$ mkdir -p /etc/systemd/system/varnish.service.d
[destroyer]$ nano /etc/systemd/system/varnish.service.d/customexec.conf

paste the following:

ExecStart=/usr/sbin/varnishd -j unix,destroyer=vcache -F -a :80 -T localhost:6082 -f /etc/varnish/default.vcl -S /etc/varnish/secret -s malloc,256m

and restart the Systemd service

                      [destroyer]$systemctl daemon-reload

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

                      [destroyer]$ nano /etc/apache2/ports.conf

Listen 80 -> Listen 8080
                      [destroyer]$ nano /etc/apache/sites-available/magento.conf

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

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

Installing and configuring Memcache caching

Memcache is a key value for storing data in memory and we will use it to replace Magento 2’s Zend_Cache_Backend_File in the default admin area. Install Memcache by running the following command:

                      [destroyer]$ apt-get install php-memcache memcached

To configure your Magento installation and use Memcache to store the session open the file app/etc/env.php and change / add the following:

                      [destroyer]$ nano /var/www/

the change:

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


                      'session' =>
   array (
      'save' => 'memcache',
      'save_path' => 'tcp://'

Finally, clear the cache again:

                      [destroyer]$ php bin/magento cache:flush


To further optimize your Magento installation, go to your Magento admin dashboard:

1. Go to STORES -> Configuration -> Catalog -> Catalog -> Use Flat Catalog tab, select Yes and click the Save Configuration button. 2. Go to the STORES tab -> Configuration -> Advanced -> Developer -> JavaScript Settings and set: merge JavaScript files and minify JavaScript files – to Yes and click the Save configuration button. 3. Go to STORES -> Configuration -> Advanced -> Developer -> CSS Options and set: Combine CSS files 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:

                      [destroyer]$ php bin/magento cache:flush

You can also see the guide on how to speed up Magento.

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

Related Posts