Configuring Memcached for session caching

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.

Getting ready

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.

How to do it…

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:

  1. First, we need to install the Memcached server and Memcached PHP client before we can connect it to Magento. Follow the next step on the shell:
    apt-get install -y libevent-dev
    apt-get install –y memcached
    
  2. Now let's test our Memcached server using the following command:
    memcached –V
    service memcached status
    netstat –anp | grep memcached
    

    As you can see, the Memcached server is running under port 11211.

  3. The next important element is installing a PHP module that can communicate with the Memcached server. We will be using the PHP Memcached extension and not the PHP Memcache extension (without the d at the end). The PHP Memcached (d) will be supporting PHP 7.

    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)
    
  4. Now let's check whether PHP has the correct Memcached extension installed. Run the following command:
    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.

  5. If everything is correct, we can restart the PHP-FPM server to activate the Memcached PHP extension. Run the following command:
    service php5-fpm restart
    
  6. To make sure that the Memcached PHP and Memcached servers are running together, we can check using the following command:
    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.

    Tip

    Use phpinfo.php wisely on a production environment. Sharing this information on a production website is not advised and could expose your security risks.

  7. Congratulations, you just finished the Memcached server and PHP Memcached setup. Now let's continue with the Magento 2 part.
  8. Open the 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'
    ),

    This is shown in the following screenshot:

    How to do it…
  9. Now save your file and remove any caches and sessions in 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.

  10. To check how many keys Memcached received, we can run the following command from the shell:
    echo "stats items" | nc localhost 11211
    
  11. Congratulations, you just finished configuring the Memcached server and PHP Memcached with Magento 2.

How it works…

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.

There's more…

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:

There's more…

Tip

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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.142.40.32