Article Directory
One, install Memcached
Two, configure Memcached
2.1 remote access
Three, connect to Memcached
3.1 PHP
3.2 Python
Four, summary
Memcached is a free and open source high-performance memory key-value database. It is mainly used for system caching to improve the response speed of the application by caching the results in the database.
This article shows how to install and configure Memcached on Ubuntu 20.04.
The Memcached package is included in the default Ubuntu 20.04 software source. To install it, run the following command as root or another sudo user:
sudo apt update
sudo apt install memcached libmemcached-tools
This one`libmemcached-tools`The package contains various command line tools to manage the Memcached server.
Once the installation is complete, memcached will start automatically. To check the server status, enter:
sudo systemctl status memcached
The output will look like this:
● memcached.service - memcached daemon
Loaded:loaded(/lib/systemd/system/memcached.service; enabled; vendor preset: enabled)
Active:active(running) since Mon 2020-07-1319:32:01 UTC; 23s ago
That's it, you have installed memcached on your Ubuntu 20.04 server, and you can start using it.
Memcached can be configured by editing the /etc/memcached.conf
file. By default, Memcached is configured to only listen on the local localhost. If the client and server connecting to the server are on the same host, you do not need to modify the default configuration file.
The default configuration file is sufficient for most users.
If the client connecting to memcached and memcached are running on the same host, you should not allow remote access.
When Memcached is not properly configured, it can be used to perform denial of service attacks (DDos). If you want to allow remote access to Memcached service, you need to configure your firewall and only allow trusted clients to access Memcached through port 11211 UDP.
In the following example, suppose you want to connect to your Memcached server through a LAN. The server IP address is 192.168.100.20
, and the client IP address is 192.168.100.30
.
The first step is to edit the memcached configuration file and set up the service to monitor the server's LAN interface:
To do this, open the memcached.conf
configuration file:
sudo nano /etc/memcached.conf
Locate this line, start with -l 127.0.0.1
, and replace 127.0.0.1
with 192.168.100.20
/etc/memcached.conf
- l 192.168.100.20
Restart the Memcached service to make the application take effect:
sudo systemctl restart memcached
Once the server is configured, the next step is to open the memcached
port on the firewall.
sudo ufw allow from192.168.100.30 to any port 11211
There are many different memcached clients, suitable for different programming languages.
To use Memcached as the cache database for your PHP applications such as Wordpress, Drupal, Joomla or Magento, you need to install the php-memcached
extension:
sudo apt install php-memcached
There are several Python libraries that can interact with memcache. You can use pip to install your favorite library:
pip install pymemcache
pip install python-memcached
We have shown you how to install Memcached on Ubuntu 20.04. For more information on this topic, check the Memcached Wiki.
Recommended Posts