← All posts

Tutorial Backend

How to Install and Configure Memcached for PHP Session Caching on LAMP

Install Memcached on LAMP and configure PHP to store sessions in memory for faster performance and horizontal scaling.

Difficulty Intermediate Time 25 min
Memcached in-memory session storage on a LAMP server with PHP session caching

By default, PHP stores sessions as files on disk. This works fine for single servers, but file-based sessions are slow and do not scale across multiple servers. In this tutorial, I will install Memcached on a LAMP server and configure PHP to store sessions in memory instead of on disk. By the end, your sessions will be faster and ready to scale horizontally across a load-balanced cluster.

Prerequisites

  • A LAMP server running Ubuntu 20.04 or later
  • PHP 7.4 or later installed and working with Apache
  • Root or sudo access
  • A working PHP application that uses sessions

Step 1 - Install Memcached Server

Memcached is a high-performance in-memory key-value store. Install it from your distribution package manager. On Ubuntu or Debian, run:

sudo apt update sudo apt install memcached -y

On CentOS or RHEL, use:

sudo yum install memcached -y

Once installed, start the Memcached service and enable it to start on boot:

sudo systemctl start memcached sudo systemctl enable memcached

Verify that Memcached is running:

sudo systemctl status memcached

You should see the service is active and running. By default, Memcached listens on 127.0.0.1 port 11211.

Step 2 - Install the PHP Memcached Extension

PHP needs the memcached extension to communicate with the Memcached server. Install it now. On Ubuntu or Debian:

sudo apt install php-memcached -y

On CentOS or RHEL:

sudo yum install php-pecl-memcached -y

Restart Apache to load the new extension:

sudo systemctl restart apache2

On CentOS or RHEL, the Apache service name is httpd:

sudo systemctl restart httpd

Verify the extension is loaded:

php -m | grep memcached

If you see memcached in the output, the extension is active.

Step 3 - Locate and Back Up Your php.ini

Find the php.ini file that Apache uses:

php -i | grep "Loaded Configuration File"

You will see a path like /etc/php/8.1/apache2/php.ini. Back it up before making changes:

sudo cp /etc/php/8.1/apache2/php.ini /etc/php/8.1/apache2/php.ini.backup

Now open the file in your editor:

sudo nano /etc/php/8.1/apache2/php.ini

Step 4 - Configure PHP to Use Memcached for Sessions

In php.ini, search for the session.save_handler directive. Change it from files to memcached:

session.save_handler = memcached

Next, find or add the session.save_path directive and set it to the Memcached server address:

session.save_path = "127.0.0.1:11211"

If you have multiple Memcached servers, list them separated by commas:

session.save_path = "127.0.0.1:11211, 192.168.1.10:11211"

For a single-server setup, one address is enough.

Step 5 - Set Session Configuration Options

Add or verify these directives in php.ini:

memcached.sess_locking = On memcached.sess_lock_wait_min = 1000 memcached.sess_lock_wait_max = 2000 memcached.sess_lock_retries = 5

These settings enable session locking to prevent race conditions. The lock wait times are in milliseconds.

Save and close php.ini.

Step 6 - Restart Apache and Test

Restart Apache to apply the new session configuration:

sudo systemctl restart apache2

Or on CentOS and RHEL:

sudo systemctl restart httpd

Create a test PHP script in your web root. The file should increment a counter on each page load to verify sessions work correctly. Visit the script in your browser and refresh several times to confirm the counter increments.

Delete the test script after confirming it works.

Step 7 - Verify Sessions Are Stored in Memcached

To confirm sessions are in Memcached, connect to the server:

telnet 127.0.0.1 11211

Once connected, type:

stats

You will see Memcached statistics. Look for curr_items. To exit telnet, type:

quit

If telnet is not installed:

sudo apt install telnet -y

Step 8 - Secure Memcached for Production

By default, Memcached binds to 127.0.0.1. To restrict access, edit the configuration:

sudo nano /etc/memcached.conf

Ensure the -l line binds only to localhost:

-l 127.0.0.1

Never expose Memcached to the public internet without authentication.

Restart Memcached after any configuration changes:

sudo systemctl restart memcached

Step 9 - Monitor Memcached Performance

Monitor Memcached to ensure it has enough memory allocated. Check memory usage:

telnet 127.0.0.1 11211 stats

Look for bytes and limit_maxbytes. If bytes approaches the limit, Memcached will evict old sessions.

To increase the memory limit, edit /etc/memcached.conf and change the -m value:

-m 128

This allocates 128 MB. Restart Memcached after changing this value.

Wrap-up

You have installed Memcached on your LAMP server and configured PHP to store sessions in memory. Sessions are now faster, and if you add more web servers, they can share the same Memcached instance for distributed session storage. Monitor Memcached memory usage and restrict network access in production.