Memcached is a high-performance distributed memory cache service that is primarily used to speed up sites that make heavy use of databases. It does so by caching data and objects in RAM, which reduces the need to access the database on subsequent requests. This article will guide you through the process of setting up a Memcache server on Ubuntu 22.04.
1. Update System Packages
It’s always recommended to update the system packages before you install any new service:
sudo apt update && sudo apt upgrade -y
2. Install Memcached
The Memcache service packages are available under default system repositories. You can install it directly without adding any thried party repository. To install the Memcached service and its required libraries, run:
sudo apt install memcached libmemcached-tools -y
3. Configure Memcached
The default configuration file for Memcached is located at /etc/memcached.conf.
To edit this file, you can use your preferred editor. For this guide, we’ll use nano:
sudo nano /etc/memcached.conf
Key configurations to consider:
- Memory Usage: Adjust the -m setting to control how much memory Memcached can use (in MB). The default is usually 64 MB.
- Listening IP Address: By default, Memcached listens on all IP addresses (0.0.0.0). If you want it to listen on the local machine only, change the -l setting to 127.0.0.1.
- Port: By default, Memcached listens on port 11211. You can change this with the -p setting if needed.
Once you’ve made your desired changes, save and close the file.
4. Restart Memcached
To ensure that your configuration changes take effect, restart the Memcached service:
sudo systemctl restart memcached
5. Adjust Firewall Rules (If UFW is Active)
If you’re using UFW (Uncomplicated Firewall) and need external machines to connect to Memcached, you’ll need to allow the Memcached port (default: 11211). However, for security reasons, it’s advisable to restrict connections only to trusted IPs:
sudo ufw allow from YOUR.TRUSTED.IP.ADDRESS/32 to any port 11211
Replace YOUR.TRUSTED.IP.ADDRESS with the IP address of the machine that will connect to the Memcached server.
6. Testing the Installation
To check if Memcached is working as expected, use the memcached-tool:
memcached-tool 127.0.0.1:11211 stats
This will show you various stats related to your Memcached server, such as the number of connections, cache hits, and cache misses.
7. Securing Memcached
Given the open nature of Memcached, it’s crucial to secure it properly:
- Bind to localhost: If you don’t need external access, bind Memcached to 127.0.0.1 to prevent unwanted external access.
- Use a Firewall: As mentioned earlier, only allow trusted IPs to connect to your Memcached server.
- Disable UDP: If you don’t need it, you can disable UDP by adding -U 0 in the configuration file.
Conclusion
Installing and configuring Memcached on Ubuntu 22.04 is straightforward. Once set up, this powerful tool can help speed up web applications by reducing database load. As with any server service, ensure you take the necessary security precautions to protect your data and infrastructure.