openec2 Article Description

Linux 2023, Nginx 1.27 and memcached

Configure memcached

I absolutely do not know how all this works, let alone to test it, or if we even need it. Perhaps we do need it to reduce pressure on our instance resources. The configurations are a little easier in Debian 11, but you would need to search for file names using cd /;find . -name *memcach* -print to see where to edit anything.

We are not installing memcache, rather, memcached.

Installing memcached on Linux 2023 was hit and miss. We can only use memcached with a WordPress plugin such as W3 Total Cache. The plugin’s “Setup Guide” will show if you can use it. Opcache will go hand in hand with it once it is working.

[These may not all be required...]

[Note: for Debain 11 or 12, depending on what php version installed, use apt install php-memcached and php8.2-memcached or phpMyAdmin will not work.]

dnf install libevent libevent-devel memcached libmemcached-tools libmemcached-devel
dnf install php-memcached
echo "/usr/local/lib/" > /etc/ld.so.conf.d/libevent.conf
ldconfig -v

/bin/echo 'extension="memcached.so"' > /etc/php.d/41-memcached.ini

cat /usr/lib/sysusers.d/memcached.conf
  u memcached - "memcached daemon" -

vi /etc/sysconfig/memcached
  PORT="11211"
  USER="memcached"
  MAXCONN="1024"
  CACHESIZE="64"
  OPTIONS="-l 127.0.0.1 -U 0,::1"

[save and exit]

vi /etc/php.ini

;;;;;;;;;;;;;;;;;;;;;;
; Dynamic Extensions ;
;;;;;;;;;;;;;;;;;;;;;;
  
  extension=memcached.so;

  # session.save_handler = files
  session.save_handler = memcached
  session.save_path = "127.0.0.1:11211"

[save and exit]

vi /etc/php-fpm.d/www.conf

  ; php_value[session.save_handler] = files
  php_value[session.save_handler] = memcached
  php_value[session.save_path]    = 127.0.0.1:11211

[Save and Exit]

Your EC2 instance Security Group needs this:

Type            Protocol       Port range     Source
Custom TCP      TCP            11211          127.0.0.0/16

systemctl daemon-reload	
systemctl start memcached      (or restart)
systemctl enable memcached

ps -ef|grep memcached

memcach+  136762       1  0 11:03 ?        00:00:01 /usr/bin/memcached -p 11211 -u memcached -m 64 -c 1024 -l 127.0.0.1 -U 0,::1

If the -u -m -c options are missing, edit the memcached file above to include them.

This is slightly different to how we edit the files in Debian 11.

systemctl status -l memcached

Next you must look at https://mydomain.com/info.php (this is the file we made in Debian 11):

Here is the code:

echo " < ?phpZphpinfo(); ? > "|sed 's/ //g'|sed 's/Z/ /g' > info.php

Search for session.save and see it is listed as using memcached:

session.save_handler	memcached	memcached
session.save_path	127.0.0.1:11211	127.0.0.1:11211

You can also check opcache is running: opcache.enable On –> and so on.

We now need to edit nginx to use memcached. I have tested many configurations given on the Internet, but much was not working, or I could not find full examples. The following “seems” to work so that we can use memcached in WordPress.

cd /etc/nginx
vi memcached.conf

upstream memcached_backend {
    server 127.0.0.1:11211; # Address and port of your Memcached server
}

upstream remote {
 server 127.0.0.1:443;
 }

[save and exit]

[The following seems to point to the domain name that we wish to use memcached with]

vi nginx.conf

[In the http section at the top:]

  include /etc/nginx/memcached.conf;

[In the port 443 section within the "location / {"  stanza: (nginx -t will show if errors)]

        set $memcached_key "$uri?$args";
        set $memc_key $arg_key;
        default_type text/html;
        set $memc_key $arg_key;
        set $memc_flags $arg_flags; # defaults to 0
        set $memc_exptime $arg_exptime; # defaults to 0
        error_page     404 = @remote;
 
 [In the stanza below this, create a new stanza:]

        location @remote {
        internal;
        access_log /var/log/nginx/server.fallback.access.log;
        proxy_pass http://remote;
        proxy_set_header Connection "";
        }   

[Save and exit]

nginx -t

[You can restart nginx and have a look at /var/log/nginx/server.fallback.access.log, and the /var/cache directories it may reference.
Good to check the nginx error and access file at this stage.]

You should now be able to use the WordPress W3 Total Cache plugin (or any that configure with memcached) to do its “Settings Guide” and use memcached.

If this does not work, I had also played around with the following installation, then I used dnf to remove it, (removed memcached), then did the above standard installation, and that worked.

dnf install -y gcc-c++ make automake zlib-devel libmemcached-devel php-devel
cd /home/ec2-user
wget https://pecl.php.net/get/memcached-3.2.0.tgz    [The pecl.php website shows the current stable version]
tar xvf memcached-3.2.0.tgz
cd memcached-3.2.0
dnf remove memcached
pecl remove memcached
phpize
./configure --disable-memcached-sasl
make
make install
make test
dnf install  libmemcached
./configure --disable-memcached-sasl
make
make install
make test
systemctl restart memcached

[I then removed memcached, and went back to the proper installation as above, and it all seemed to work.
But what the gaps or problems are in these configurations I have no idea.]