Memcached is a free to use and open source in-memory, high-performance and distributed key-value store for small chunks of arbitrary data (objects, strings) from results of database calls, API calls, or page rendering. Memcached is intended for use in speeding up dynamic web applications by alleviating database load. Memcached API is available for most popular programming languages.

In this short post we will be covering the installation of Memcached on Amazon Linux 2 server. There is a Memcached package available in the default Amazon Linux 2 repositories but the version is a bit old. In this guide we will download the latest release of Memcached source, extract and build the binary from it.

Install Memcached on Amazon Linux 2

We will be doing the manual installation of Memcached on Amazon Linux 2. We start by ensuring all dependencies are installed, then perform the actual building and installation of latest Memcached Server on Amazon Linux 2.

Step 1: Install Build Dependencies

Install build dependencies using yum commands below.

sudo yum -y install '@Development Tools' openssl-devel libevent-devel

You can also also build libevent manually if you need the latest release – optional.

curl -s https://api.github.com/repos/libevent/libevent/releases/latest | grep browser_download_url | grep stable.tar.gz |  cut -d '"' -f 4 |  wget -i -
tar xvf libevent-*-stable.tar.gz
cd libevent-*/
./configure
make
sudo make install

# Create symlink of  libevent kernel library.
sudo ln -s /usr/lib/libevent-<ver>.so.7 /usr/lib64/libevent-<ver>.so.7

Step 2: Install Memcached on Amazon Linux 2

Download the latest Memcached release source code.

wget https://memcached.org/latest -O memcached-latest.tar.gz

Once the file is downloaded extract it with tar command line tool.

tar xvf memcached-latest.tar.gz

Extract the downloaded archive file.

cd memcached-*/

Run configuration command:

./configure

Make and install.

./configure --prefix=/usr/local/memcached
make
make test
sudo make install

Step 3: Configure Systemd service file

Create Memcached systemd environment configuration file:

sudo tee /etc/sysconfig/memcached <<EOF
# These defaults will be used by every memcached instance, unless overridden
# by values in /etc/sysconfig/memcached.<port>
USER="nobody"
MAXCONN="1024"
CACHESIZE="64"
OPTIONS=""

# The PORT variable will only be used by memcached.service, not by
# [email protected] services, which will use the xxxxx
PORT="11211"
EOF

Create new Memcached Systemd unit file.

sudo vim /etc/systemd/system/memcached.service

Paste below contents:

[Unit]
Description=memcached daemon
After=network.target

[Service]
EnvironmentFile=/etc/sysconfig/memcached
ExecStart=/usr/local/bin/memcached -p ${PORT} -u ${USER} -m ${CACHESIZE} -c ${MAXCONN} $OPTIONS
PrivateTmp=true
ProtectSystem=full
NoNewPrivileges=true
PrivateDevices=true
CapabilityBoundingSet=CAP_SETGID CAP_SETUID CAP_SYS_RESOURCE
RestrictAddressFamilies=AF_INET AF_INET6 AF_UNIX

[Install]
WantedBy=multi-user.target

Reload Systemd.

sudo systemctl daemon-reload

Start Systemd service.

$ sudo systemctl status memcached -l
● memcached.service - memcached daemon
   Loaded: loaded (/etc/systemd/system/memcached.service; disabled; vendor preset: disabled)
   Active: active (running) since Thu 2020-11-12 19:13:06 UTC; 6s ago
 Main PID: 19529 (memcached)
   CGroup: /system.slice/memcached.service
           └─19529 /usr/local/bin/memcached -p 11211 -u nobody -m 64 -c 1024

Nov 12 19:13:06 mail.hirebestengineers.com systemd[1]: Started memcached daemon.
Nov 12 19:13:06 mail.hirebestengineers.com systemd[1]: Starting memcached daemon...

Step 4: PHP and Python usage

A Client specific to your programming language is required when connecting to Memcached server from your application.

For PHP

PHP Module installation:

sudo amazon-linux-extras enable php7.4
sudo yum install php-memcached

Accept dependency and package installation:

....
Dependencies Resolved

==================================================================================================================================================================
 Package                                   Arch                          Version                                   Repository                                Size
==================================================================================================================================================================
Installing:
 php-pecl-memcached                        x86_64                        3.1.5-1.amzn2                             amzn2extra-php7.4                         63 k
Installing for dependencies:
 libmemcached                              x86_64                        1.0.16-5.amzn2.0.2                        amzn2-core                               238 k
 libzip                                    x86_64                        1.3.2-1.amzn2.0.1                         amzn2-core                                62 k
 php-common                                x86_64                        7.4.11-1.amzn2                            amzn2extra-php7.4                        1.1 M
 php-json                                  x86_64                        7.4.11-1.amzn2                            amzn2extra-php7.4                         71 k
 php-pecl-igbinary                         x86_64                        3.1.2-1.amzn2                             amzn2extra-php7.4                         82 k
 php-pecl-msgpack                          x86_64                        2.1.0-1.amzn2                             amzn2extra-php7.4                         38 k

Transaction Summary
==================================================================================================================================================================
Install  1 Package (+6 Dependent packages)

Total download size: 1.7 M
Installed size: 14 M
Is this ok [y/d/N]: y

For Python:

With Python 3:
sudo yum -y install python3 python3-pip

Install Memcached Python module:

$ sudo pip3 install  python-memcached
Collecting python-memcached
  Downloading https://files.pythonhosted.org/packages/f5/90/19d3908048f70c120ec66a39e61b92c253e834e6e895cd104ce5e46cbe53/python_memcached-1.59-py2.py3-none-any.whl
Collecting six>=1.4.0 (from python-memcached)
  Downloading https://files.pythonhosted.org/packages/ee/ff/48bde5c0f013094d729fe4b0316ba2a24774b3ff1c52d924a8a4cb04078a/six-1.15.0-py2.py3-none-any.whl
Installing collected packages: six, python-memcached
Successfully installed python-memcached-1.59 six-1.15.0
With Python 2:
sudo yum install -y python2 python2-pip
sudo pip python-memcached
sudo pip2 install python-memcached

More articles on Amazon Linux:

How To Install Zimbra Server on Amazon Linux 2

Install wkhtmltopdf & wkhtmltoimage on Amazon Linux 2

How To Install PostgreSQL 13 on Amazon Linux 2

LEAVE A REPLY

Please enter your comment!
Please enter your name here

3 × four =