PHP Sessions

How To Store PHP Sessions in Memcached on CentOS

Welcome back to the Greenhost.cloud blog! Today, we’re diving into an essential optimization technique for PHP developers: storing PHP sessions in Memcached on a CentOS server. Using Memcached for session storage can significantly improve the performance and scalability of your web applications. Let’s explore how to set this up step by step.

What is Memcached?

Memcached is a high-performance, distributed memory caching system designed to speed up dynamic web applications by alleviating database load. It allows you to store data objects in memory, enabling faster access to frequently requested data, such as PHP sessions.

Why Use Memcached for PHP Sessions?

  • Improved Performance: Storing sessions in memory reduces the overhead of reading and writing session data to disk.
  • Scalability: Memcached allows multiple servers to access the same session data, making it easier to scale your application horizontally.
  • Reduced Database Load: By offloading session storage from your database, you can improve the overall performance of your database.

Prerequisites

Before we get started, ensure you have the following:

  • A CentOS server (CentOS 7 or later is recommended).
  • PHP installed (version 7.0 or later).
  • Composer installed for PHP dependency management.

Step 1: Install Memcached

First, you need to install Memcached on your CentOS server. Open your terminal and run the following commands:

sudo yum install epel-release
sudo yum install memcached

Once the installation is complete, you can start Memcached and enable it to run at boot:

sudo systemctl start memcached
sudo systemctl enable memcached

To verify that Memcached is running, you can check its status:

sudo systemctl status memcached

Step 2: Install PHP Memcached Extension

Next, you need to install the PHP Memcached extension, which allows PHP to communicate with the Memcached server. You can do this using the following commands:

sudo yum install php-pecl-memcached

After the installation is complete, restart the web server to apply the changes:

sudo systemctl restart httpd  # For Apache
# OR
sudo systemctl restart php-fpm  # For Nginx with PHP-FPM

Step 3: Configure PHP to Use Memcached for Sessions

Now, we need to configure PHP to use Memcached for session management. Open your php.ini file, which is usually located at /etc/php.ini or /etc/php.d/. Find the [Session] section and modify the following settings:

session.save_handler = memcached
session.save_path = "127.0.0.1:11211"

Explanation:

  • session.save_handler: Set this to memcached to tell PHP to use Memcached for session handling.
  • session.save_path: This specifies the address of your Memcached server. The default port is 11211.

After making these changes, save the file and restart your web server again:

sudo systemctl restart httpd  # For Apache
# OR
sudo systemctl restart php-fpm  # For Nginx with PHP-FPM

Step 4: Test Your Configuration

To ensure that everything is working correctly, let’s create a simple PHP script to test session handling. Create a file called session_test.php in your web server’s root directory:

<?php
session_start();
$_SESSION['test'] = 'Hello, Memcached!';
echo $_SESSION['test'];
?>

Access this script through your web browser (e.g., http://your-server-ip/session_test.php). If you see the message “Hello, Memcached!”, congratulations! Your PHP sessions are now being stored in Memcached.

Conclusion

Storing PHP sessions in Memcached on CentOS can dramatically enhance the performance and scalability of your web applications. By following the steps outlined in this post, you can efficiently manage sessions and reduce load on your database.