Memcached is a high-performance, distributed memory object caching system, which is often used to speed up dynamic database-driven websites by caching data and objects in RAM to reduce the number of times the database must be read. In this article, we will guide you through the process of setting up a Memcache server on a Debian-based Linux distribution.
Prerequisites:
- A Debian-based system (e.g., Debian 12 “Bookworm”).
- Root or sudo access to the system.
1. Update Your System
It’s always a good practice to start by updating your system’s package list and then upgrade the existing packages:
sudo apt update
sudo apt upgrade
2. Install Memcached
To install Memcached on Debian, run the following command:
sudo apt install memcached
3. Configure Memcached
Once installed, you’ll want to configure Memcached. The configuration file for Memcached is located at /etc/memcached.conf.
Open the file using your preferred text editor, such as nano:
sudo nano /etc/memcached.conf
Here are some common settings you might want to adjust:
- Memory Usage: Adjust the memory that Memcached can use. For example, to limit it to 512 MB, you’d set:
-m 512
- Listening IP: By default, Memcached listens on 127.0.0.1. If you want to change it to listen on all available IPs, set:
-l 0.0.0.0
However, be cautious with this setting to prevent unauthorized access.
- Port: By default, Memcached listens on port 11211. If you wish to change it, adjust:
-p 11211
Once you’ve made your desired changes, save and close the file.
4. Start and Enable Memcached
To start the Memcached service:
sudo systemctl start memcached
To ensure Memcached starts on boot:
sudo systemctl enable memcached
You can check the status with:
sudo systemctl status memcached
5. Configure Firewall (Optional)
If you’re using a firewall like ufw, ensure you allow connections to Memcached. For instance, to allow from a specific IP (192.168.1.100):
sudo ufw allow from 192.168.1.100 to any port 11211
6. Test Memcached Installation
To test if Memcached is working, you can use the telnet or netcat utilities.
First, install them if they’re not already:
sudo apt install telnet
Connect to Memcached:
telnet 127.0.0.1 11211
You should see an output indicating a successful connection.
7. Secure Memcached
IMPORTANT: By default, Memcached doesn’t have any authentication mechanism. If you expose it to the public internet, you risk unauthorized access and potential misuse. Always ensure:
- It’s only accessible from trusted networks.
- If used across networks, consider setting up a VPN or other secure tunneling solutions.
- You can also use the `-U 0` option in the configuration file to disable the UDP listener if not needed, which can help mitigate certain types of DDoS attacks.
8. Client Setup
Memcached clients exist for various languages including PHP, Python, Ruby, Java, and others. For PHP, as an example, you’d install the Memcached extension:
sudo apt install php-memcached
After which, you can use the Memcached class within your PHP scripts.
Conclusion
Memcached is a versatile caching system that can greatly improve the response times of database-driven applications. When setting it up, always prioritize security and only allow trusted sources to access your Memcached server.
1 Comment
Thanks for helpful tutorial! Any idea how to do this for PHP 7.4 when using deb.sury.org as the repo?