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
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.
make
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 restart memcached
sudo systemctl enable memcached
Checking service status:
$ 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 python_memcached-1.59-py2.py3-none-any.whl (16 kB)
Collecting six>=1.4.0
Downloading six-1.16.0-py2.py3-none-any.whl (11 kB)
Installing collected packages: six, python-memcached
Successfully installed python-memcached-1.59 six-1.16.0
With Python 2:
sudo yum install -y python2 python2-pip
sudo pip python-memcached
sudo pip2 install python-memcached