Tag: memcached

  • Install Memcached on cPanel Server

    Login to WHM as user root, go to Terminal or login to SSH as user root. Then run the command

    yum install memcached
    

    Enable memcached to start on boot.

    systemctl enable memcached
    

    Configure memcached

    Default configuration on AlmaLinux 9 look like the following

    PORT="11211"
    USER="memcached"
    MAXCONN="1024"
    CACHESIZE="64"
    OPTIONS="-l 127.0.0.1,::1"
    

    edit configuration file

    vi /etc/sysconfig/memcached
    

    Lets change cache size to 128 mb

    Find

    CACHESIZE="64"
    

    Replace 64 with 128

    In AlmaLinux 9, memcached only listen on loop back interface IP address (127.0.0.1,::1). If your configuation don’t restrict memcache to listen to 127.0.0.1, change your options line as follows.

    OPTIONS="-l 127.0.0.1 -U 0"
    

    This will restrict memcached to listen on IP address 127.0.0.1 and -U 0 disable UDP. This is done to protect memcachd installation from attacks.

    Restart memcached with

    systemctl restart memcached
    

    To verify memcached is running, run

    [root@server1 ~]# netstat  -lntp | grep memc
    tcp        0      0 127.0.0.1:11211         0.0.0.0:*               LISTEN      2224258/memcached   
    [root@server1 ~]# 
    

    We have memcached listening on 127.0.0.1:11211

    Now we have memcached deamon running. We need PHP module installed.

    Installing memcached PHP module

    To list all available memcached PHP module, run

    yum search php-memcached
    

    list memcache php modules

    To install memcache module for PHP 8.2, run

    yum install -y ea-php82-php-memcached
    

    For PHP 8.0, install package ea-php80-php-memcached, similarly, for PHP 8.1 install ea-php81-php-memcached.

    If you use CloudLinux PHP selector, memcached is already installed on the system. You can enable the module using PHP Selector.

    Back to memcached

  • How to run multiple memcached instances

    How to run multiple memcached instances

    I had copied a PrestaShop site to another domain. Both domains are on same server, using same Memcached server for caching. When i visit one of the site, resources like css/js files are loading from other website. To fix this, i configured 2 Memcached servers running on 2 different ports, each site use its own Memcached instance.

    On Ubuntu server, i installed memcached and supervisor with

    apt install -y memcached memcached
    

    Create file

    vi /etc/supervisor/conf.d/memcached.conf 
    

    Inside add

    [program:memcached2]
    priority=200
    command=/usr/bin/memcached -m 64 -p 11212 -u memcache -l 127.0.0.1 -P /var/run/memcached/memcached2.pid
    user=memcache
    autorestart=true
    autostart=true
    redirect_stderr=true
    

    By default Memcached runs on port 11211, above configuration will make an instance of Memcached that runs on port 11212.

    If you want to create another instance of memcached, duplicate above lines, in the lines, change

    [program:memcached2]
    

    with

    [program:memcached3]
    

    Find

    command=/usr/bin/memcached -m 64 -p 11212 -u memcache -l 127.0.0.1 -P /var/run/memcached/memcached2.pid
    

    Replace port with different port, say 11213 and /var/run/memcached/memcached2.pid with /var/run/memcached/memcached2.pid

    Here is the complete configuration with 2 Memcached instances running under supervisiord

    [program:memcached2]
    priority=200
    command=/usr/bin/memcached -m 64 -p 11212 -u memcache -l 127.0.0.1 -P /var/run/memcached/memcached2.pid
    user=memcache
    autorestart=true
    autostart=true
    redirect_stderr=true
    
    [program:memcached3]
    priority=200
    command=/usr/bin/memcached -m 64 -p 11213 -u memcache -l 127.0.0.1 -P /var/run/memcached/memcached3.pid
    user=memcache
    autorestart=true
    autostart=true
    redirect_stderr=true
    

    Reload supervisor

    supervisorctl reload
    

    Check status

    supervisorctl status
    

    Here is an example with 4 memcached instances running

    root@zen-keldysh:/etc/supervisor/conf.d# supervisorctl reload
    Restarted supervisord
    root@zen-keldysh:/etc/supervisor/conf.d# supervisorctl status
    memcached_erikoisrahti           RUNNING   pid 337864, uptime 0:00:02
    memcached_longdrink24            RUNNING   pid 337865, uptime 0:00:02
    memcached_tulivesi               RUNNING   pid 337866, uptime 0:00:02
    memcached_viskit                 RUNNING   pid 337867, uptime 0:00:02
    root@zen-keldysh:/etc/supervisor/conf.d# 
    

    Here are the configuration file for above 4 Memcached instance setup

    root@server1:~# cat /etc/supervisor/conf.d/memcached.conf 
    [program:memcached_longdrink24]
    priority=200
    command=/usr/bin/memcached -m 64 -p 11212 -u memcache -l 127.0.0.1 -P /var/run/memcached/memcached2.pid
    user=memcache
    autorestart=true
    autostart=true
    redirect_stderr=true
    
    [program:memcached_viskit]
    priority=200
    command=/usr/bin/memcached -m 64 -p 11213 -u memcache -l 127.0.0.1 -P /var/run/memcached/memcached-viskit.pid
    user=memcache
    autorestart=true
    autostart=true
    redirect_stderr=true
    
    [program:memcached_erikoisrahti]
    priority=200
    command=/usr/bin/memcached -m 64 -p 11214 -u memcache -l 127.0.0.1 -P /var/run/memcached/memcached-erikoisrahti.pid
    user=memcache
    autorestart=true
    autostart=true
    redirect_stderr=true
    
    [program:memcached_tulivesi]
    priority=200
    command=/usr/bin/memcached -m 64 -p 11215 -u memcache -l 127.0.0.1 -P /var/run/memcached/memcached-tulivesi.pid
    user=memcache
    autorestart=true
    autostart=true
    redirect_stderr=true
    
    root@server1:~# 
    
  • Enable  memcached on Magento 2 in Plesk Server

    Enable memcached on Magento 2 in Plesk Server

    To install memcached, run

    apt install php-memcached
    apt install build-essential
    apt install autoconf automake gcc libmemcached-dev libhashkit-dev pkg-config plesk-php*-dev zlib1g-dev
    

    Enable memcached php module

    /opt/plesk/php/7.3/bin/pecl install memcached
    

    This is for PHP 7.3, change the path to pecl for your version of PHP.

    Activate memcached

    echo "extension=memcached.so" > /opt/plesk/php/7.3/etc/php.d/memcached.ini
    

    Update php handler

    plesk bin php_handler --reread
    

    Restart php-fpm

    systemctl stop plesk-php73-fpm.service
    systemctl start  plesk-php73-fpm.service
    

    Install memcached deamon

    apt install memcached
    

    Restart memcached

    systemctl enable memcached
    systemctl restart memcached
    

    Verify it is working with netstat

    malta2032:~# netstat -lntp | grep mem
    tcp        0      0 127.0.0.1:11211         0.0.0.0:*               LISTEN      30204/memcached     
    malta2032:~# 
    

    Activate memcached in Magento 2

    Edit file

    vi app/etc/env.php
    

    Find

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

    Replace with

        'session' => [
            'save' => 'memcached',
            'save_path' => '127.0.0.1:11211'
        ],
    

    If you get error like

    Warning: SessionHandler::read(): open(127.0.0.1:6379/sess_0s39482mnk2hhoflf4d6odjuv2, O_RDWR) failed: No such file or directory (2) in /vendor/magento/framework/Session/SaveHandler/Native.php on line 22
    

    edit your php.ini and set “session.save_handler” from “files” to “memcached”.

    php session memcached

    See if memcached working

    To see if memcached is caching, you can use

    telnet 127.0.0.1  11211
    stats items
    

    memcached telnet
    To exit, type

    quit
    

    See Magento, Memcached

  • Secure Memcached on CentOS/RHEL 7

    Secure Memcached on CentOS/RHEL 7

    By default memcached on CentOS 7 is set to run on all IP address on the server. This allow attackers to abuse the service.

    To set memcached to only listen to localhost (127.0.0.1), edit file

    vi /etc/sysconfig/memcached
    

    Find

    OPTIONS=""
    

    Replace with

    OPTIONS="-l 127.0.0.1"
    

    See memcached

  • Install memcached on CentOS 7

    To install memcached on CentOS 7, run

    yum -y install memcached

    Set memcached to start on boot

    systemctl enable memcached

    Start memcached with

    systemctl start memcached

    Installing PHP Module

    You need remi repository installed, which provide a memcached module for all versions of php they support.

    For PHP 5.6, run

    yum install php-memcache

    Now phpinfo() will show

    centos 7 php memcache

    See memcached

  • memcached

    To see stats, run

    [root@centos-1gb-nyc1-01 ~]# echo "stats settings" | nc localhost 11211
    STAT maxbytes 67108864
    STAT maxconns 1024
    STAT tcpport 11211
    STAT udpport 11211
    STAT inter NULL
    STAT verbosity 0
    STAT oldest 0
    STAT evictions on
    STAT domain_socket NULL
    STAT umask 700
    STAT growth_factor 1.25
    STAT chunk_size 48
    STAT num_threads 4
    STAT num_threads_per_udp 4
    STAT stat_key_prefix :
    STAT detail_enabled no
    STAT reqs_per_event 20
    STAT cas_enabled yes
    STAT tcp_backlog 1024
    STAT binding_protocol auto-negotiate
    STAT auth_enabled_sasl no
    STAT item_size_max 1048576
    STAT maxconns_fast no
    STAT hashpower_init 0
    STAT slab_reassign no
    STAT slab_automove 0
    END
    [root@centos-1gb-nyc1-01 ~]#