As Magento 2 does not support Redis session caching from the beginning, we need to use Memcached instead. Memcached has been around for a long time and was used in Magento 1, since the beginning, as backend and session caching.
Memcached is a distributed memory caching system. It is a flexible in-memory storage container to cache data. As the session handler in the PHP Redis does not support session locking, we use Memcached instead. Keep in mind that Memcached is not persisted, so after restarting the server or daemon, all the data is gone. This could have an impact on a production environment—lost sessions or baskets.
For this recipe, we will use a Droplet created in Chapter 2, Magento 2 System Tools, at DigitalOcean, https://www.digitalocean.com/. We will be using NGINX, PHP-FPM, and a Composer-based setup including sample data connected to a Memcached server. No other prerequisites are required.
For the purpose of this recipe, let's assume that we need to create a Magento 2 Memcached setup. The following steps will guide you through this:
apt-get install -y libevent-dev apt-get install –y memcached
memcached –V service memcached status netstat –anp | grep memcached
As you can see, the Memcached server is running under port 11211.
Use the following command to install PHP Memcached:
apt-get install php5-memcached
We can also use the following command to install it:
apt-get install php-memcached (php7)
cat /etc/php5/mods-available/memcached.ini
Now you should see the Memcached extension called extension=memcached.so
.
Depending on whether you are using PHP 5 or PHP 7, you may want to change the PHP path.
service php5-fpm restart
php -r "if (new Memcached() == true){ echo "
OK
"; }"
It can also be checked using the following command:
echo "stats settings" | nc localhost 11211
By default, creating a phpinfo.php
page in the root directory in Magento 2 will not work. First, you need to create the phpinfo.php
file in the /pub
directory. Then, you need to change the NGINX configuration from location ~ (index|get|static|report|404|503).php$ {
to location ~ (index|get|static|report|404|503|phpinfo).php$ {
, which is located at the bottom of the file. In Apache, we don't have an issue like this; it works by default.
env.php
file in Magento 2 located at /app/etc
and change the following code in the session
section:'session' => array ( 'save' => 'files', ),
Change the preceding code to the following:
'session' => array ( 'save' => 'memcached', 'save_path' => 'localhost:11211' ),
var/page_cache
, var/cache
and var/session
. Restart your website using the following command:service nginx restart && service php-fpm restart
Let's open up your browser and refresh your website.
If everything is configured correctly in the env.php
file, you should not get any errors and the /var/session
directory should be empty.
echo "stats items" | nc localhost 11211
Let's recap and find out what we did throughout this recipe. In steps 1 through 11, we installed a Memcached server and configured Magento 2 to store the sessions.
In steps 1 through 3, we installed the default Ubuntu Memcached server and PHP Memcached client modules. Depending on whether we are using PHP 5 or 7, we pick a different one. Before this, we make sure to restart the PHP-FPM server and test if everything is working correctly.
In step 8, we added an additional piece of code to the env.php
file, which will tell Magento 2 to store all of the sessions in Memcached as of now.
If you are interested in monitoring your Memcached server, the next step is interesting. Clone the
memcached.php
file from the GitHub Gist (https://gist.github.com/raybogman/b8b7b4d21bf34ed9dd76) in your Magento 2 root directory, /var/www/html/pub
. Make sure to change the ownership to www-data
for the owner and group.
By default, creating a memcached.php
page in the root directory in Magento 2 will not work. First, you need to store the memcached.php
file in the /pub
directory. Then, you need to change the NGINX configuration from location ~ (index|get|static|report|404|503).php$ {
to location ~ (index|get|static|report|404|503|memcached).php$ {,
which is located at the bottom of the file. In Apache, we don't have an issue like this; it works by default.
Once installed correctly, the page will look as follows:
Note that this Memcached viewer is an outdated version created in 2008 by Harun Yayli and is not maintained anymore. Use it wisely.
As an alternative, you can also use https://github.com/clickalicious/phpMemAdmin.
3.142.40.32