How to Install Memcached on Ubuntu Linux

computer 1459654 640

This post shows students and new users steps to install and use Memcached on Ubuntu Linux. Memcached is a free, open source, high-performance in-memory key-value data store. It allows repeated PHP object or database calls to be cached in system memory to help speed up dynamic web applications.

Memcached can be used with dynamic web applications like WordPress, Drupal and others to speed up applications by caching various objects from the results of API and database calls.

There’s an alternative to Memcached called Redis, but Memcached is lightweight, matured and suitable for most applications. If you’re going to be running a web application or portal and need to improve its performance, installing Memcached might be something you’ll want to implement.

Also, for students and new users learning Linux, the easiest place to start learning is on Ubuntu Linux. Ubuntu is the modern, open source Linux operating system for desktop, servers and other devices.

To get started with installing Memcached on Ubuntu Linux, follow the steps below.

How to install Memcached on Ubuntu Linux

Memcached package is included with Ubuntu default repositories and the installation process is pretty straightforward. Simply run the commands below to install it along with its support tools.

sudo apt update
sudo apt install memcached libmemcached-tools

Memcached tools provide several command line tools for managing the Memcached server. You’ll mostly want to install it with Memcached server.

After running the commands above, Memcached server should be installed and ready to use. To check its status run the commands below:

sudo systemctl status memcached

You should see similar lines as below:

● memcached.service - memcached daemon
   Loaded: loaded (/lib/systemd/system/memcached.service; enabled; vendor preset: enabled)
   Active: active (running) since Thu 2019-06-06 10:36:25 CDT; 27s ago
     Docs: man:memcached(1)
 Main PID: 19852 (memcached)
    Tasks: 10 (limit: 4682)
   CGroup: /system.slice/memcached.service
           └─19852 /usr/bin/memcached -m 64 -p 11211 -u memcache -l 127.0.0.1 -P /var/run/memcached/memcached.pid

Jun 06 10:36:25 ubuntu1804 systemd[1]: Started memcached daemon.

The server should be running and should respond to requests. The commands below can be used to stop, start and enable Memcached.

sudo systemctl stop memcached.service
sudo systemctl start memcached.service
sudo systemctl enable memcached.service

How to configure Memcached on Ubuntu Linux

Now that the Memcached is installed, its configuration file can be found at /etc/memcached.conf

The default settings in the file should be enough for most environments and applications. However, for more advanced settings, open the file and make changes you want to apply to your environment.

For example, Memcached listens on the server’s local IP address (127.0.0.1). If you want to only have it listen on a different IP, edit the lines in the file to look similar to the one below:

sudo nano /etc/memcached.conf

Then replace the local sever IP with the one you want to use. You can also change its default port number as well.

# Default connection port is 11211
-p 11211

# Run the daemon as root. The start-memcached will default to running as root if no
# -u command is present in this config file
-u memcache
# Specify which IP address to listen on. The default is to listen on all IP addresses
# This parameter is one of the only security measures that memcached has, so make sure
# it's listening on a firewalled interface.
-l 192.168.2.1

Save the file and exit. then restart Memcached services for the changes to apply.

How to block remote access to Memcached

When Memcached server is improperly configured, it can be used to perform a distributed denial-of-service (DDoS) attacks. If you’re going to be allowing remote access you need to make sure that only trusted client can access it remotely.

You can define the remote client IP in the configuration file above. IP that are not in the file above are automatically denied access remotely.

You can also setup Ubuntu firewall to block all remote clients except for those are explicit allowed on port 11211.

sudo ufw allow from 192.168.2.1 to any port 11211

Th

To use Memcached as a caching database for your PHP application such as WordPress, Drupal, Joomla or Magento, you need to install the php-memcached extension.

Run the commands below to install PHP Memcached PHP extension.

sudo apt install php-memcached

To use Memcached with Python, install the extension below.

pip install pymemcache

pip install python-memcached

That should do it!

Conclusion:

This post showed you how to install and use Memcached on Ubuntu Linux. If you find any error above or have something to add, please use the comment form below.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.