How to Install Magento 2 with Apache, Varnish and Memcache

magento 2 with memcache

In this article, we will show you how to install Magento 2 on an Ubuntu 16.04 VPS with MariaDB, Varnish as a full page cache, Apache and Memcache for session storage. This guide should work on other Linux VPS systems as well but was tested and written for an Ubuntu 16.04 VPS.

Before we begin, you may want a different setup for your Magento. You can follow this tutorial to install Magento 2 on an Ubuntu 16.04 VPS with MariaDB, PHP-FPM 7.0, Varnish as a full page cache, Nginx as SSL termination and Redis for session storage and page caching.

If you have any different requirements, just get a Linux VPS from us and we’ll set everything up for you, free of charge.

INSTRUCTIONS:

First of all login to your Ubuntu 16.04 VPS via SSH as user root

ssh root@IP_address

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

screen -U -S magento

Update the system and install necessary 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 enable automatic updates.

Install and configure MariaDB 10.1

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

apt-get install software-properties-common
apt-key adv --recv-keys --keyserver hkp://keyserver.ubuntu.com:80 0xF1656F24C74CD1D8
add-apt-repository 'deb [arch=amd64,i386,ppc64el] https://mirrors.evowise.com/mariadb/repo/10.1/ubuntu xenial main'

Once the key is imported and the repository added you can install MariaDB with:

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

Next, we need to create a database for our Magento 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)]> FLUSH PRIVILEGES;
MariaDB [(none)]> \q

Do not forget to replace ‘my_strong_password’ with a strong password.

Install Apache2 web server

[user]$ sudo apt-get install apache2

Install PHP and required PHP modules

To install the latest stable version of PHP version 7 and all necessary modules, run:

[user]$ 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

Change few default PHP settings:

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

Enable the Apache2 rewrite module if it is not already done:

[user]$ a2enmod rewrite

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

[user]$ service apache2 restart

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

[user]$ curl -sS https://getcomposer.org/installer | php
mv composer.phar /usr/local/bin/composer

Install Magento 2 from Github

Clone the Magento repository to the ~/myMagentoSite.com directory using the following command:

[user]$ git clone https://github.com/magento/magento2.git /var/www/myMagentoSite.com

Get the latest Magento 2 stable release:

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

Run composer to install all Magento dependencies:

[user]$ composer install

To continue with the installation you can either use the installation wizard or the command line, in this guide we will use the latter.

[user]$ bin/magento setup:install \
--base-url=http://myMagentoSite.com/ \
--db-host=localhost \
--db-name=magento \
--db-user=magento \
--db-password=my_strong_password \
--admin-firstname=First  \
--admin-lastname=Last \
--admin-email=user@myMagentoSite.com \
--admin-user=admin \
--admin-password=my_strong_password123 \
--language=en_US \
--currency=USD \
--timezone=America/Chicago \
--use-rewrites=1

If the installation is successful you will see something like below:

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

Run the crontab command to create a cronjob

crontab -u www-data -e

and add the following line:

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

Finally, set the correct permissions:

[user]$ chown -R www-data: /var/www/myMagentoSite.com

Configure Apache

Create a new virtual host directive in Apache. For example, create a new Apache configuration file named ‘magento.conf’ on your virtual server:

[user]$ touch /etc/apache2/sites-available/magento.conf
[user]$ ln -s /etc/apache2/sites-available/magento.conf /etc/apache2/sites-enabled/magento.conf
[user]$ nano /etc/apache2/sites-available/magento.conf

Then, add the following lines:

<VirtualHost *:80>
ServerAdmin admin@yourdomain.com
DocumentRoot /var/www/myMagentoSite.com/
ServerName myMagentoSite.com
ServerAlias www.myMagentoSite.com
<Directory /var/www/myMagentoSite.com/>
Options FollowSymLinks
AllowOverride All
Order allow,deny
allow from all
</Directory>
ErrorLog /var/log/apache2/myMagentoSite.com-error_log
CustomLog /var/log/apache2/myMagentoSite.com-access_log common
</VirtualHost>

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

[user]$ sudo service apache2 restart

You should be now able to login to your Magento back-end by going to http://myMagentoSite.com/admin_t0x0nr using the information you set when running the bin/magento setup:install.

Install and configure Varnish

Installing Varnish is as simple as running the following command:

[user]$ apt-get install varnish

From you Magento Admin dashboard click on the STORES link (left sidebar) -> Configuration -> ADVANCED -> System -> Full Page Cache

Unselected Use system value and from the Caching Application list, select Varnish Cache (Recommended), save the configuration, click on the Varnish Configuration link and click on the Export VCL for Varnish 4 button. The varnish.vcl file which we will use will be exported in the /var/www/myMagentoSite.com/var/ directory.

Flush the Magento cache with:

[user]$ php bin/magento cache:flush

Delete the /etc/varnish/default.vcl and symlink it to the exported varnish configuration.

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

To change varnish port from 6081 to 80, we need to edit the systemd service configuration.

Create a new customexec.conf file

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

paste the following:

[Service]
ExecStart=
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 reload systemd units

[user]$systemctl daemon-reload

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

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

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

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

If everything is setup correctly now you should be able to login to your Magento back-end by going to https://myMagentoSite.com/admin_t0x0nr.

[ecko_alert color=”blue”]Stuck somewhere? Get a VPS from us and we’ll do all of this for you, free of charge![/ecko_alert]

Install and configure Memcache caching

Memcache is a key-value in memory data store and we will use it to replace the default Magento 2 Zend_Cache_Backend_File backend cache. Install Memcache by running the following command:

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

Configure memcached to listen on localhost (127.0.0.1) and disable UDP:

vi /etc/memcached.conf
-l 127.0.0.1
-U 0

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

[user]$ nano /var/www/myMagentoSite.com/app/etc/env.php

change:

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

with:

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

Finally flush the cache again:

Need a fast and easy fix?
✔ Unlimited Managed Support
✔ Supports Your Software
✔ 2 CPU Cores
✔ 2 GB RAM
✔ 50 GB PCIe4 NVMe Disk
✔ 1854 GeekBench Score
✔ Unmetered Data Transfer
NVME 2 VPS

Now just $43 .99
/mo

GET YOUR VPS
[user]$ php bin/magento cache:flush

Further Optimizations

To further optimize your Magento installation from you Magento admin dashboard:

1. Go to STORES -> Configuration -> CATALOG -> Catalog -> Use Flat Catalog Category, select Yes and click Save Config.
2. Go to STORES -> Configuration -> ADVANCED -> Developer -> JavaScript Settings and set both Merge JavaScript Files and Minify JavaScript Files to Yes and click Save Config..
3. Go to STORES -> Configuration -> ADVANCED -> Developer -> CSS Settings and set both Merge CSS Files and Minify CSS Files to Yes and click Save Config.
4. Consider using a CDN – Content Delivery Network

Do not forget to flush the cache again:

[user]$ php bin/magento cache:flush

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

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


Of course, you don’t have to do any of this if you use one of our Magento VPS Hosting services, in which case you can simply ask our expert Linux admins to setup this for you. They are available 24×7 and will take care of your request immediately.

PS. If you liked this post please share it with your friends on the social networks using the buttons on the left or simply leave a reply below. Thanks.

2 thoughts on “How to Install Magento 2 with Apache, Varnish and Memcache”

Leave a Comment