How To Install And Configure MemCached On Ubuntu
In my previous blog , I have explained How to Setup Memcached cluster in AWS
In this guide , We will see how to install and configure memcached on Ubuntu.
What Is Memcached?
Memcached is an open sources distributed memory caching system.
It is used to speed up and improve the performance of the applications by storing the information temporarily in memory.
By doing so , It will also helps to reduce the connection to the database.
Lets go ahead and install Memcached on Ubuntu operating systems.
Make sure you have sudo or root privileges to the system.
Update the ubuntu’s default APT package repository using the command,
sudo apt-get update
Using the below command , We can install memcached on the system.
sudo apt-get install memcached libmemcached-tools -y
Optionally , We can install Memcache module for PHP functionality.
sudo apt-get install php-memcached -y
After the packages are installed , We can check the version of memcached installed on the system,
To verify whether Memcache PHP module is installed on the system, Execute the command.
php -m |grep memcache
The above command should reply as memcached
While installing memcached packages , The systemd unit file memcached.service will be created by itself , using which we can easily manage the memcached service.
To start the memcached service,
systemctl start memcached
To check the status of the service,
systemctl status memcached
To enable memcached service to automatically start on system boot up,
systemctl enable memcached
By default , Memcached runs on the Port 11211 and listens only to Localhost.
You can check the same using the below command.
netstat -nltp |grep memcache
At some point , If you want to change the settings of the Memcached service , We can do it by modifying /etc/memcached.conf file.
We can change the Port , Allow remote connections , Memory for Caching.
sudo vi /etc/memcached.conf
Keeping the same flags for memory (-m) , Port (-p) and listen address (-l) , We can modify their values.
After changing the configurations for memcached , Restart the memcached service for the changes to take effect.
systemctl restart memcached
Using Memcached With PHP & Apache
Let’s test this, By installing PHP and Apache web server to use the Memcached service.
To install Apache and PHP packages , Use the below command.
apt-get install apache2 libapache2-mod-php7.2 php7.2-cli php7.2 -y
After the packages are installed , We can verify the Apache and PHP installations using the below command.
systemctl status apache2
Next step is to create a sample php file – phpinfo.php under /var/www/html folder.
Add these content to the file,
Save and close the file And then restart the Apache service.
systemctl restart apache2
Lets verify the installation , Go to your browser and type the URL as shown below.
You will see the information related to PHP and Memcached modules on te following page.
We have successfully installed and Configured memcached on the Ubuntu operating system.
Also We have tested the same by installing PHP module and Apache Web server to use Memcached.
If you found it helpful.Please check out other blogs.