
Memcached is a high-performance distributed memory object caching system that speeds up web applications by caching data and objects in memory (RAM). Memcached works by reducing the need to query an external data source such as a database with similar data requests making it a preferred solution for enhancing application performance and responsiveness on your server.
This article explains how to install Memcached on a FreeBSD 14.0 server and secure it with SSL certificates to ensure safe data transmission between applications.
Before you begin:
Deploy a FreeBSD 14.0 server instance on Vultr.
Access the server using SSH as a non-root user with sudo privileges.
Memcached is available in the default FreeBSD package repositories with the latest stable version information. You can also install a specific Memcached version using FreeBSD ports that contain custom build files. Follow the steps to install the latest Memcached version on your FreeBSD server.
Update your FreeBSD package repository index.
Install Memcached.
View the installed Memcached version on your server.
Verify that your Memcached version displays like the one below.
Start the Memcached service.
sysrc is a command-line utility that manages system services on your FreeBSD server and allows you to set options such as memory usage, port, and environment configurations. Follow the steps below to configure Memcached and enable it to run on the server.
Enable Memcached to start automatically at boot time.
Configure Memcached to listen for connections on your server interface. Then, adjust the memory and connection limits depending on your server resources. For example, run the following command to use the default port 11211, assign 128MB as caching memory, and allow up to 1024 simultaneous connections.
Restart Memcached to apply the configuration changes.
Securing Memcached with SSL is essential when protecting cached data and ensuring secure communications on your server. SSL mitigates a range of security risks and helps in managing the confidentiality, integrity, and authenticity of processed data. SSL certificates ensure encrypted connections between Memcached and any application requests. Follow the steps below to secure Memcached and install SSL certificates to use on your server.
Install the OpenSSL package.
Create a new directory to store SSL certificates.
Switch to the directory.
Generate a new SSL certificate and private key.
Enter your desired self-signed SSL certificate request details when prompted.
When successful, the above command creates a new self-signed SSL certificate saved as memcached-key.pem and an RSA private key memcached-cert.pem. The certificate is valid for 365 days, and the private key is not protected with a passphrase.
View the running Memcached process on the default port 1121 and verify the associated user.
Output:
Based on the above output, Memcached runs with the nobody profile on your server.
Grant the nobody profile full privileges to the SSL directory to ensure that Memcached can access all files.
Long list the directory contents to verify the new file permissions.
You should see something like this:
Configure Memcached using the sysrc utility to use the new SSL certificate and key.
Within the above command:
-Z: Enables SSL support.-o ssl_chain_cert=/etc/ssl/memcached/memcached-cert.pem: Specifies the SSL certificate file.-o ssl_key=/etc/ssl/memcached/memcached-key.pem: Specifies the SSL key file.Restart Memcached to apply the new configuration changes.
Memcached is compatible with popular frameworks such as PHP and Python to cache frequently requested application data. Follow the steps below to connect to Memcached on your server on the default port 11211 using PHP.
Install PHP and the Memcached module on your server.
View the installed PHP version on your server.
Output:
Run the following PHP command to test access to Memcached on your server.
Output:
You have installed Memcached on FreeBSD 14.0 and connected to the application to perform caching on your server. Memcached enables faster data retrieval and improved performance when integrated with your web applications. For more information about memory allocation, eviction policies, and integrating Memcached with your web applications, please visit the Memcached documentation.
0 Comments
Be the first to comment and share your perspective with the community.