How To Install Memcached on Ubuntu 18.04
18.04
Memcached is a memory object caching system that allows you to cache information in a memory which is very useful for optimizing websites. It also improves app speeds and prevents poor performance on your dedicated server.
This tutorial will show you how to install and configure Memcached on an Ubuntu 18.04 LTS server.
Prerequisites
This guide will assume that you have the basic knowledge of Linux and most importantly, your site is hosted on your own VPS.
- Ensure your server is set in the root account. If not, you may need to add sudo to your commands to get root privileges.
- One Ubuntu 18.04 server.
With these essential items in place, you will be ready to start the installation of your Memcached server.
Step 1- Installing Memcached
To get started with Memcached, gather all the necessary components from the Ubuntu repository. To get the latest components, start by updating your system with the following command:>
$ sudo apt-get update
Now, install Memcached as follows:
$ sudo apt-get install memcached
You can also install libmemcached-tools, which is a library set that features several tools that work well with the Memcached server:
$ sudo apt-get install libmemcached-tools
At this point, Memcached should be successfully installed on your server, along with the essential tools that will allow you to test the connectivity easily. Now, let’s proceed to configure Memcached settings.
Step 2 – Configuring Memcached
Before configuring Memcached, you need to ensure that it is listening to the local interface 127.0.0.1. For default level configuration, we will check for the following settings under the Memcached configuration file found in /etc/memcached.conf.
The latest version of Memcached that comes with Ubuntu and Debian features the -1 parameter set on the local interface. This parameter defines the IP address to Memcached and prevents denial of service attacks that may come from the network.
We can check this setting to confirm it is working properly:
Use nano to open /etc/memcached.conf file:
$ sudo nano /etc/memcached.conf
To check the interface setting, look for the line below in your file:
/etc/memcached.conf
. . .
-l 127.0.0.1
If the setting shows -1 127.0.0.1, there is no need to modify this line.
Now, save and exit from the file once you’re done.
Go ahead and restart Memcached service to apply these changes:
$ sudo systemctl restart memcached
Confirm that Memcached service is listening to the local interface on TCP connections using the command below:
$ sudo netstat -plunt
You should get the following results:
Output Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name . . . tcp 0 0 127.0.0.1:11211 0.0.0.0:* LISTEN 2383/memcached . . .
This output shows that memcached is on TCP connections through 127.0.0.1.
Step 3 – Verifying Memcache Setup
To check and verify that your Memcached service is working properly with your machine, run the command below. This will display the current statistics of the Memcached server.
$ echo "stats settings" | nc localhost 11211
You should see the following output:
The values may differ from the results below:
STAT maxbytes 134217728 STAT maxconns 1024 STAT tcpport 11211 STAT udpport 11211 STAT inter 127.0.0.1 STAT verbosity 0 STAT oldest 0 STAT evictions on STAT domain_socket NULL STAT umask 700 STAT growth_factor 1.25 STAT chunk_size 48 STAT num_threads 4 STAT num_threads_per_udp 4 STAT stat_key_prefix : STAT detail_enabled no STAT reqs_per_event 20 STAT cas_enabled yes STAT tcp_backlog 1024 STAT binding_protocol auto-negotiate STAT auth_enabled_sasl no STAT item_size_max 1048576 STAT maxconns_fast no STAT hashpower_init 0 STAT slab_reassign no STAT slab_automove 0 STAT lru_crawler no STAT lru_crawler_sleep 100 STAT lru_crawler_tocrawl 0 STAT tail_repair_time 0 STAT flush_enabled yes STAT hash_algorithm jenkins STAT lru_maintainer_thread no STAT hot_lru_pct 32 STAT warm_lru_pct 32 STAT expirezero_does_not_evict no END
Once you’ve verified Memcache setup, install the PHP module for Memcached.
Step 4 – Installing Memcached PHP Module
On your Ubuntu system, install the current PHP version from ppa:ondrej/php. If you’ve already installed PHP, skip this step.
Otherwise, run the commands below:apt
$ sudo add-apt-repository ppa:ondrej/php $ sudo apt-get update $ sudo apt-get install -y php php-dev php-pear libapache2-mod-php
Next, install the required PHP extension for Memcached:
$ sudo apt-get install -y php-memcached
Now, restart Apache to apply the changes:
$ sudo service apache2 restart
Test whether Memcache PHP service is working properly. Make sure you create a test file labelled info.php with the content below:
<?php phpinfo(); ?>
Now, try to access info.php on your web interface. If the PHP page is on your browser, then everything is working fine, and you are set to proceed further.
Conclusion
Congratulations, you’ve successfully installed and configured Memcached on Ubuntu 18.04. We hope this tutorial was useful. For additional help, we recommend you to visit the official Memcached page.