Linux, Windows, Virtualization, OpenSource & Blogging

How to Install Memcached on Debian 9

6
Install Memcached on Debian 9
Install Memcached on Debian 9

Memcached is a free and open source general purpose distributed memory object caching system used for speeding up dynamic database-driven web applications by alleviating database loads to physical memory through caching.

Memcached is simple yet powerful. It is widely used by top busiest websites such Google, Youtube, Wikipedia, etc.

Here are the simple steps to install Memcached on Debian 9. Also, this should work on previous versions of Debian, i.e., Debian 8 and Debian 7.

Open a Terminal and then switch to root user.

$ su -

OR

$ sudo su -

Install Memcached:

Update the repository index.

apt-get update

Memcached is available in Debian repositories. Install Memcached using the apt-get command.

apt-get -y install memcached netcat

Verify whether the Memcached service is started or not.

ps -ef | grep -i memcached

Output:

memcache  4107     1  0 02:56 ?        00:00:00 /usr/bin/memcached -m 64 -p 11211 -u memcache -l 127.0.0.1
root      4178  3896  0 03:05 pts/0    00:00:00 grep -i memcached

Configure Memcached:

The default configuration file of Memcached is /etc/memcached.conf.

nano /etc/memcached.conf

Change the settings, if necessary. Example settings for 256MB caching.

# Start with a cap of 64 megs of memory. It's reasonable, and the daemon default
# Note that the daemon will grow to this size, but does not start out holding this much
# memory
-m 256

# 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 127.0.0.1

Restart Memcached process.

systemctl restart memcached

To auto-start Memcached on booting.

systemctl enable memcached

Confirm the Memcached running status.

echo "stats settings" | nc localhost 11211

Output:

STAT maxbytes 268435456
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
[. . .]

Firewall:

Run the following commands to allow the incoming connection on Port no 11211.

FirewallD:

firewall-cmd --permanent --zone=public --add-port=11211/tcp
firewall-cmd --reload

UFW:

ufw allow 11211/tcp
ufw reload

Check the remote connectivity.

echo stats | nc memcache_host_name_or_ip 11211

Install PHP Module:

Install Memcached PHP module to work with PHP.

apt-get install -y php php-memcache

Now restart the Memcached and Apache service to take effect.

systemctl restart memcached
systemctl restart apache2

That’s all!

You might also like
  • Dmtrs Rightnow

    with apt-get install -y php php-memcache

    i get

    E: Unable to locate package php
    E: Unable to locate package php-memcache

    any ideas?

    • Raj

      Make sure you have updated the repository index.

      apt-get update

      • Dmtrs Rightnow

        unfortunately, it’s up to date.. :/

        any suggested repo to switch to?

        • Raj

          This PHP package is available on stretch main repository. So you should get if your system is properly configured with all the debian repositories.

          Try this

          apt-cache policy php-memcache

          Please share me the output

          • Dmtrs Rightnow

            N: Unable to locate package php-memcache

          • Raj

            Is Stretch main repository enabled in your system?

Shares