×
Community Blog How to Set Up Memcached with PHP on Ubuntu 18.04

How to Set Up Memcached with PHP on Ubuntu 18.04

In this guide, we will be setting up a Memcached server on an ECS Ubuntu 18.04 instance and will show you how you can use it on your PHP code to speed up operations.

By Francis Ndungu, Alibaba Cloud Community Blog author.

Memcached is an open-source memory caching system that you can use to speed up web applications and dynamic websites. Memcached works by storing frequently accessed data in RAM hence reducing the time an application reads data from databases, files or API interfaces. If your website, web application or API server has high read calls and infrequent writes, Memcached can be a great tool for you.

Since its development in 2003, the technology has been very successful and is deployed by a number of great websites like LiveJournal. To use the Memcached technology, your code should support the technology. If you are a programmer, you can rewrite your program to take advantage of Memcached technology.

In this guide, we will take you through the steps of setting up a Memcached server on your Alibaba Cloud Elastic Compute Service (ECS) Ubuntu 18.04 instance and show you how you can use it on your PHP code to speed up operations.

Prerequisites

Before you begin, make sure you have the following:

  • An Alibaba Cloud Account. Signing up is free. Create an Alibaba Cloud account and enjoy a free trial worth up to $1200 to test over 50 Alibaba Cloud products.
  • An Alibaba Cloud Elastic Compute Service (ECS) instance running Ubuntu 18.04 as the Operating System. You may follow the initial ECS setup guide to create one
  • A non-root user that can perform sudo privileges on the ECS instance.

Step 1: Installing Apache Web Server

The first step is installing Apache web server. You may skip this step if Apache is already running on your server. SSH to your Alibaba Cloud ECS instance and run the commands below:

$ sudo apt-get update 
$ sudo apt-get install -y apache2

Once the web server is installed and running, you will now go ahead and install PHP.

Step 2: Installing PHP

Next, install PHP by running the command below. As with the previous step, you may skip installing PHP if it is already running on your system. The command below also installs libapache2-mod-php module that links Apache web server with PHP.

$ sudo apt-get install -y php libapache2-mod-php

Next, install php-memcached extension. This module provides clients(e.g. PHP code) with access to the Memcached server.

$ sudo apt-get install -y php-memcached

Restart Apache for the changes to be effected.

$ sudo systemctl restart apache2

With PHP and Memcached extension installed, you can move on to installing the Memcached server

Step 3: Installing and Configuring Memcached Server

Since Memcached package is available on the default Ubuntu software repository, you will use apt to install it. You will also install libmemcached-tools . This is a set of lightweight library and tools used by the Memcached server.

To install Memcached, run the command below:

$ sudo apt-get install -y memcached libmemcached-tools

Once installed, you can edit Memcached settings by modifying the /etc/memcached.conf file using nano text editor

$ sudo nano /etc/memcached.conf

Let's go over some of the configuration settings used by Memcached

-d : This option allows Memcached to run as a daemon

logfile: Memcached logs errors and its activities in this file.

-m : The value after -m specifies the maximum memory that can be held by the Memcached server although Memcached will not hold the total memory when initially started, The default value is 64 MB. You can change the cache size depending on the available RAM on your Alibaba Cloud ECS instance.

-p : Memcached will listen on the port specified with this option. The default value is 11211.

-u : This option specifies the user under which Memcached will run under. The default value is memcache

-i : This specifies the IP address that Memcached server will listen on. The default value on Ubuntu 18.04 server is localhost set with the IP address 127.0.0.1

-c : This option can be used to limit the number of simultaneous connections to the Memcached server. The default value is 1024.

If you make any changes to the file, you should restart Memcached server using the command below:

$ sudo systemctl restart memcached

Then, restart Apache web server

$ sudo systemctl restart apache2

Memcached server should now be working as expected. In the next step, you will write a simple PHP script to check if PHP can indeed support the Memcached server.

Step 4: Checking Memcached Support

Once you have installed and configured Memcached, the next step is to verify if your Apache web server and PHP can recognize it.

To do this, create an info.php file in the root of your website.

$ sudo nano /var/www/html/info.php

Then, paste the content below in the file

<?php
 phpinfo();
?>

Save the file by pressing CTRL+X, Y and Enter.

Then, on your web browser, visit the file path as shown below. Remember to replace 172.16.0.1 with the public/internet IP address associated with your Alibaba Cloud ECS instance.

172.16.0.1/info.php

Find the memcached header and you should see information similar to the one shown below. This verifies that Memcached is supported by PHP.

1

Next, run the command below to verify that Memcached server is working as expected on your server.

echo stats | nc 127.0.0.1 11211

You should see an output similar to the one shown below:

STAT pid 9493
STAT uptime 725
STAT time 1570086409
...
STAT moves_within_lru 0
STAT direct_reclaims 0
STAT lru_bumps_dropped 0
END

After testing that Memcached server is working, you can now move on to creating a PHP script that uses the Memcached library.

Step 5: Testing Memcached with a PHP Script

In this step, you will create a simple PHP script and use the Memcached class to cache some data. Create a /var/www/html/test_memcached.php file on the root of your website using nano text editor

$ sudo nano /var/www/html/test_memcached.php

Next, paste the below PHP code on the file and press CTRL+X, Y and Enter to save the file.

<?php

try

{
    $memcached = new Memcached();
    $memcached->addServer("127.0.0.1", 11211); 
    $response = $memcached->get("sample_key");
 
    if($response==true) 
    {
      echo $response;
    } 

    else

    {
    echo "Cache is empty";
    $memcached->set("sample_key", "Sample data from cache") ;
    }
}
catch (exception $e)
{
echo $e->getMessage();
}
?>

Memcached server uses a key/value pair to store cached data. The code above instructs PHP to create an instance of the Memcached class and add a server with the IP address 127.0.0.1 via the 11211 port.

The code then tries to retrieve the value of a key named sample_key. If the key is present on the server, its value will be returned, else, the code will try to create the key and add a value of "Sample data from cache" to it.

Visit the file path on your browser to see Memcached in action. Replace 172.16.0.1 with the internet IP address associated with the Alibaba ECS instance.

172.16.0.1/test_memcached.php

When you run the code for the first time, you will get the output shown below.

2

Next, refresh the page and this time, Memcached should retrieve the value it cached when the code ran for the first time.

3

That's it when it comes to setting up and using Memcached with PHP. You can extend the code to cache data from databases, external web sources (e.g. API) or from the file system.

Conclusion

In this guide, we have taken you through the steps of setting up and using Memcached with PHP Ubuntu 18.04 server hosted on Alibaba Cloud. This is just one of the few technologies that you can use to optimize the performance of your websites and web applications. You may also consider reading about How to Optimize MySQL Queries for Speed and Performance on Alibaba Cloud ECS.

0 0 0
Share on

francisndungu

31 posts | 8 followers

You may also like

Comments

francisndungu

31 posts | 8 followers

Related Products