четверг, 24 октября 2019 г.

Linux Ububntu 18.04 Use memcached for PHP session storage

https://devdocs.magento.com/guides/v2.3/config-guide/memcache/memcache_ubuntu.html

apt update -y && apt install php-memcached memcached
/etc/php/7.2/fpm/php.ini
session.save_handler = "memcached"
session.save_path = "127.0.0.1:11211"

or /etc/php/7.2/fpm/pool.d/user.conf
php_admin_value[session.save_path] = '127.0.0.1:11211'
php_admin_value[session.save_handler] = 'memcached'

phpinfo should display memcached info on the page.

Check working with a script cache-test.php



$meminstance = new Memcached();
$meminstance->addServer("127.0.0.1", 11211);
$result = $meminstance->get("test");
if ($result) {
    echo $result; 
} else { 
    echo "No matching key found. Refresh the browser to add it!";
    $meminstance->set("test", "Successfully retrieved the data!") or die("Couldn't save anything to memcached...");
}

The first time you go to the page, the following displays:
No matching key found. Refresh the browser to add it!
Refresh the browser. The message changes to 
Successfully retrieved the data!
Finally, you can view the memcache keys using Telnet with commands:

1) List the items, to get the slab ids:
 stats items:

telnet localhost 11211
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
stats items

STAT items:1:number 1
STAT items:1:number_hot 0
STAT items:1:number_warm 0
STAT items:1:number_cold 1
STAT items:1:age_hot 0
STAT items:1:age_warm 0
STAT items:1:age 108
STAT items:1:evicted 0
STAT items:1:evicted_nonzero 0
STAT items:1:evicted_time 0
STAT items:1:outofmemory 0
STAT items:1:tailrepairs 0
STAT items:1:reclaimed 0
STAT items:1:expired_unfetched 0
STAT items:1:evicted_unfetched 0
STAT items:1:evicted_active 0
STAT items:1:crawler_reclaimed 0
STAT items:1:crawler_items_checked 0
STAT items:1:lrutail_reflocked 12
STAT items:1:moves_to_cold 5
STAT items:1:moves_to_warm 4
STAT items:1:moves_within_lru 0
STAT items:1:direct_reclaims 0
STAT items:1:hits_to_hot 0
STAT items:1:hits_to_warm 0
STAT items:1:hits_to_cold 5
STAT items:1:hits_to_temp 0
STAT items:8:number 1
STAT items:8:number_hot 0
STAT items:8:number_warm 0
STAT items:8:number_cold 1
STAT items:8:age_hot 0
STAT items:8:age_warm 0
STAT items:8:age 4733
STAT items:8:evicted 0
STAT items:8:evicted_nonzero 0
STAT items:8:evicted_time 0
STAT items:8:outofmemory 0
STAT items:8:tailrepairs 0
STAT items:8:reclaimed 0
STAT items:8:expired_unfetched 0
STAT items:8:evicted_unfetched 0
STAT items:8:evicted_active 0
STAT items:8:crawler_reclaimed 0
STAT items:8:crawler_items_checked 0
STAT items:8:lrutail_reflocked 0
STAT items:8:moves_to_cold 3
STAT items:8:moves_to_warm 2
STAT items:8:moves_within_lru 0
STAT items:8:direct_reclaims 0
STAT items:8:hits_to_hot 0
STAT items:8:hits_to_warm 0
STAT items:8:hits_to_cold 2
STAT items:8:hits_to_temp 0
END

2) The first number after ‘items’ is the slab id. Request a cache dump for each slab id, with a limit for the max number of keys to dump (e.g. 100):

stats cachedump 1 100
ITEM test [32 b; 0 s]
END

stats cachedump 8 100
ITEM memc.sess.key.nj36l4uursjmanv6vooc24g8bc [366 b; 1571998245 s]
END

3) Flush memcached storage:

flush_all
OK


4) and quit Telnet:

quit

Connection closed by foreign host.



Configure Magento for working with memcached


app/etc/env.php
    'session' => [
            'save' => 'memcached',
            'save_path' => '127.0.0.1:11211'
    ],

instead of
    'session' => [
            'save' => 'files'
    ],

For verifying, remove cache
rm -rf var/cache/* var/page_cache/* var/session/*

But storefront page throws error with such config 

InvalidArgumentException: Failed to set ini option "session.save_path" to value "127.0.0.1:11211". in /home/magento/shop1/vendor/magento/framework/Session/SessionManager.php:632 Stack trace: #0 ...
I removed 'save_path' => '127.0.0.1:11211' parameter and errors disappeared.
So correct config is

    'session' => [
            'save' => 'memcached'
    ],

Also, memcached better use with socket file instead of port.
I added to /etc/memcached.conf config such parameters and commented -l -p (ip and port) parameters.

/etc/memcached.conf
# Set unix socket which we put in the RAM /dev/shm and made memcache user the owner
-s /dev/shm/memcached.sock

# set permissions for the memcached socket so memcache user and www-data group can write
-a 0660

Memcached socket beeter place in RAM /dev/shm. 0660 permissions are only for user memcache, www-data group and main group for memcache account should be www-data

usermod -g www-data memcache

Also, I changed php.ini config by

php_admin_value[open_basedir] = /home/magento:/tmp:/dev/shm:/var/www/html
php_admin_value[session.save_path] = /dev/shm/memcached.sock
php_admin_value[session.save_handler] = memcached

where I added /dev/shm to open_basedir and socket instead of port.

So I used nc for displaying session's data

nc -U /dev/shm/memcached.sock

stats items
STAT items:9:number 1
STAT items:9:number_hot 0
STAT items:9:number_warm 0
STAT items:9:number_cold 1
STAT items:9:age_hot 0
STAT items:9:age_warm 0
STAT items:9:age 36
STAT items:9:evicted 0
STAT items:9:evicted_nonzero 0
STAT items:9:evicted_time 0
STAT items:9:outofmemory 0
STAT items:9:tailrepairs 0
STAT items:9:reclaimed 0
STAT items:9:expired_unfetched 0
STAT items:9:evicted_unfetched 0
STAT items:9:evicted_active 0
STAT items:9:crawler_reclaimed 0
STAT items:9:crawler_items_checked 0
STAT items:9:lrutail_reflocked 0
STAT items:9:moves_to_cold 1
STAT items:9:moves_to_warm 0
STAT items:9:moves_within_lru 0
STAT items:9:direct_reclaims 0
STAT items:9:hits_to_hot 0
STAT items:9:hits_to_warm 0
STAT items:9:hits_to_cold 0
STAT items:9:hits_to_temp 0
STAT items:10:number 2
STAT items:10:number_hot 0
STAT items:10:number_warm 0
STAT items:10:number_cold 2
STAT items:10:age_hot 0
STAT items:10:age_warm 0
STAT items:10:age 27
STAT items:10:evicted 0
STAT items:10:evicted_nonzero 0
STAT items:10:evicted_time 0
STAT items:10:outofmemory 0
STAT items:10:tailrepairs 0
STAT items:10:reclaimed 0
STAT items:10:expired_unfetched 0
STAT items:10:evicted_unfetched 0
STAT items:10:evicted_active 0
STAT items:10:crawler_reclaimed 0
STAT items:10:crawler_items_checked 0
STAT items:10:lrutail_reflocked 0
STAT items:10:moves_to_cold 2
STAT items:10:moves_to_warm 0
STAT items:10:moves_within_lru 0
STAT items:10:direct_reclaims 0
STAT items:10:hits_to_hot 0
STAT items:10:hits_to_warm 0
STAT items:10:hits_to_cold 0
STAT items:10:hits_to_temp 0
END

stats cachedump 9 100
ITEM memc.sess.key.t0k0h54unktgusuvh0b11ne5qg [394 b; 1572068341 s]
END

stats cachedump 10 100
ITEM memc.sess.key.h55f5vhs16d78vd2emo29ltp0n [513 b; 1572068353 s]
ITEM memc.sess.key.vlr2meg6c8otjjvbpu5rtnart3 [513 b; 1572068350 s]
END

It works now. 

Комментариев нет:

Отправить комментарий