Kiến Thức Linux Linux Nâng Cao

How to Install Memcached on Ubuntu

Memcached

Memcached is an open-source, high-performance, and general-purpose distributed memory caching system. It is an in-memory key-value store for small chunks of arbitrary data and used to speed up dynamic websites by caching data in RAM. Memcached is a great tool for you if your web application has high read calls and infrequent writes.

In this tutorial, we will show you how to install and configure Memcached on Ubuntu 20.04.

Prerequisites

  • A server running Ubuntu 20.04.
  • A root password is configured on your server.

Install Memcached

By default, the Memcached package is available in the Ubuntu default repository. You can install it using the following command:

apt-get install memcached libmemcached-tools -y

Once installed, start the Memcached service and enable it to start at reboot with the following command:

systemctl start memcached
systemctl enable memcached

You can also verify the Memcached service using the following command:

systemctl status memcached

You should get the following output:

? memcached.service - memcached daemon
     Loaded: loaded (/lib/systemd/system/memcached.service; enabled; vendor preset: enabled)
     Active: active (runningeneral-purpose distributed memory-caching system.g) since Thu 2020-05-14 03:03:35 UTC; 33s ago
       Docs: man:memcached(1)
   Main PID: 93483 (memcached)
      Tasks: 10 (limit: 2282)
     Memory: 1.6M
     CGroup: /system.slice/memcached.service
             ??93483 /usr/bin/memcached -m 64 -p 11211 -u memcache -l 127.0.0.1 -P /var/run/memcached/memcached.pid

May 14 03:03:35 ubuntu2004 systemd[1]: Started memcached daemon.

By default, Memcached is listening on port 11211. You can verify it with the following command:

ss -tulpn | grep :11211

You should see the following output:

tcp   LISTEN 0      1024                         127.0.0.1:11211        0.0.0.0:*                                                                                users:(("memcached",pid=93483,fd=26))                                          

Configure Memcached

Memcached default configuration file is located at /etc/memcached.conf. You can configure it as per your requirements.

The default settings is enough for most applications. However, if you want to allow remote access to your Memcached server and change the default port, edit the Memcached configuration file as shown below:

nano /etc/memcached.conf

Change the following values:

# Default connection port is 11211
-p 11211

-l your-server-ip

# Limit the number of simultaneous incoming connections. The daemon default is 1024
 -c 1024

Save and close the file then restart the Memcached service to implement the changes.

systemctl restart memcached

You can now verify the Memcached status with the following command:

memcstat --servers=your-server-ip

You should get the following output:

Server: your-server-ip (11211)
	 pid: 94106
	 uptime: 30
	 time: 1589425647
	 version: 1.5.22
	 libevent: 2.1.11-stable
	 pointer_size: 64
	 rusage_user: 0.036605
	 rusage_system: 0.006100
	 max_connections: 1024

Configure PHP to Use Memcached

At this point, Memcached is installed and configured. Now, you can configure PHP to use Memcached.

First, install Apache and PHP modules with the following command:

apt-get install apache2 libapache2-mod-php php php-cli php-memcached -y

Next, create a sample PHP file in the Apache root directory with the following command:

nano /var/www/html/phpinfo.php

Add the following code:

<?php 
phpinfo();
?>

Save and close the file then restart the Apache service to implement the changes:

systemctl restart apache2

Now, open your web browser and type the URL http://your-server-ip/phpinfo.php. You should see the following screen:

As you can see, Memcached support is enabled in PHP.

If you want to enable Python support, install the following package:

apt-get install python3-pymemcache -y

If you want to enable Perl support, install the following package:

apt-get install libcache-memcached-libmemcached-perl -y

Conclusion

In this guide, you learned how to install and configure Memcached on Ubuntu 20.04 server. You also learned how to enable PHP, Python and Perl support in Memcached. Feel free to ask me if you have any questions.

 

 

 

Đăng ký liền tay Nhận Ngay Bài Mới

Subscribe ngay

Cám ơn bạn đã đăng ký !

Lỗi đăng ký !

Tags

Add Comment

Click here to post a comment

Đăng ký liền tay
Nhận Ngay Bài Mới

Subscribe ngay

Cám ơn bạn đã đăng ký !

Lỗi đăng ký !