Memcached is a high-performance, distributed memory caching system used to improve the speed and scalability of web applications. In this article, we will guide you through installing Memcached on an Ubuntu VPS and provide an example of how to use it for caching data effectively in your applications
10 min
Edited:12-10-2024
Memcached is an open-source, high-performance, distributed memory object caching system. It is designed to cache data and objects in RAM to reduce the number of database requests, improving the response times of dynamic web applications. Memcached is lightweight and fast, making it popular for scenarios where frequently accessed data needs to be quickly retrievable without reloading from a slower back-end store. Memcached can store any arbitrary data, such as results from database queries, API calls, or complex calculations, making it particularly useful for high-traffic websites and applications where speed is critical.
To install and configure Memcached on an Ubuntu-based VPS, follow the steps below.
Start by ensuring that your system is up-to-date:
1. sudo apt update
2. sudo apt upgrade
You can install Memcached using the default Ubuntu package repository. Use the following command:
1. sudo apt install memcached
Once installed, you can verify if Memcached is running by checking its status:
1. sudo systemctl status memcached
You should see output indicating that the service is active and running. If Memcached is not running, you can start it with:
1. sudo systemctl start memcached
You can also enable Memcached to start on system boot:
1. sudo systemctl enable memcached
The default configuration file for Memcached is located at /etc/memcached.conf. You may want to modify certain settings depending on your server specifications and requirements.
Open the configuration file using a text editor like nano:
1. sudo nano /etc/memcached.conf
Key settings you might want to adjust:
Memory Allocation: The amount of memory Memcached uses. By default, it uses 64MB. You can adjust this as needed (e.g., -m 256 to allocate 256MB).
Connection Address: By default, Memcached listens only on 127.0.0.1 (localhost). If you need external access, you can change this to your server's IP. However, it is recommended to keep Memcached bound to localhost unless you’re using advanced networking configurations.
After making your changes, save and exit the file (press CTRL + X, then Y, followed by ENTER), and restart Memcached to apply the new settings:
1. sudo systemctl restart memcached
Depending on your programming environment, you may need to install a Memcached client library. For example, if you are using PHP, you can install the PHP Memcached extension as follows:
1. sudo apt install php-memcached
Let’s walk through an example of using Memcached to store and retrieve cached data.
You can use telnet to connect to the Memcached server and perform simple operations. First, install telnet if it’s not already available:
1. sudo apt install telnet
Now, connect to the Memcached server (running on localhost by default):
1. telnet 127.0.0.1 11211
Once connected, you can perform basic Memcached operations like storing and retrieving data.
To store data in Memcached, you use the set command, which follows this syntax:
1. set key_name flags expiration_time data_size
2. data
Here’s an example:
1. set example_key 0 900 9
2. memcached
3. STORED
In this example:
In this example:
0 represents the flags (this is typically left as 0).
900 is the expiration time in seconds (the value will expire after 15 minutes).
9 is the length of the data to store.
memcached is the actual data to store.
After the command, you should see the response STORED, indicating that the data was successfully cached.
To retrieve the cached data, use the get command:
1. get example_key
Memcached will return:
1. VALUE example_key 0 9
2. memcached
3. END
This shows that the key example_key is stored with the value memcached.
You can delete a key using the delete command:
1. delete example_key
2. DELETED
If you're developing in PHP, here's an example of how to use Memcached within your PHP code:
1. <?php
2. // Create a new Memcached instance
3. $memcached = new Memcached();
4. // Connect to the Memcached server
5. $memcached->addServer('127.0.0.1', 11211);
6. // Set a value in the cache
7. $memcached->set('example_key', 'Hello, Memcached!', 900);
8. // Retrieve the value from the cache
9. $value = $memcached->get('example_key');
10. echo $value;
11. ?>
In this example:
The addServer function connects to the Memcached server running on localhost.
The set method stores a value in Memcached, with a key (example_key) and an expiration time of 900 seconds.
The get method retrieves the value associated with the key example_key.
Memcached, by default, is not secure for external use as it does not provide authentication or encryption. To secure Memcached:
1. Bind Memcached to localhost: Ensure Memcached is only accessible from localhost by making sure the following line is in /etc/memcached.conf:
1. -l 127.0.0.1
2. Use a Firewall: Restrict access to Memcached using a firewall such as ufw. Allow access only from trusted sources if you need remote access:
1. sudo ufw allow from your_trusted_ip to any port 11211
3. Memcached SASL Authentication: For a more secure setup in multi-user environments, you can enable SASL authentication (available in some versions of Memcached).
Memcached is a powerful tool for speeding up applications by caching frequently accessed data in memory. By following this guide, you should now have Memcached installed and configured on your Ubuntu VPS. With basic commands and client-side integration, you can leverage Memcached’s caching capabilities to improve the performance and scalability of your web applications.
14-10-2024
This article offers a detailed guide on installing and configuring IPTables on an Ubuntu VPS. IPTables is a powerful firewall tool that helps secure your server by controlling inbound and outbound traffic. Learn how to set up rules for traffic filtering, configure basic security policies, and apply custom rules to protect your VPS.
IPtables
security
12 min
This article offers a comprehensive guide on installing and configuring ModSecurity, a powerful web application firewall (WAF), on an Ubuntu VPS. Learn how to secure your server by filtering and monitoring HTTP requests, set up ModSecurity with Nginx or Apache, and apply rules to protect against common web attacks.
Modsecurity
security
10 min
14-10-2024
This article provides a comprehensive guide on installing and configuring PHP-FPM (FastCGI Process Manager) on an Ubuntu VPS. Learn how to optimize PHP performance for your web applications by configuring PHP-FPM with Nginx or Apache, managing pools, and fine-tuning settings for efficient processing of PHP scripts.
PHP-FPM
speed
optimise
12 min