In web development, efficient session management is crucial for maintaining user state across requests. Redis, a high-performance, in-memory data store, is an excellent choice for handling sessions in PHP applications.

Redis offers several advantages when used as a session handler:

  • In-Memory Storage
  • Atomic Operations
  • Expiration Policies
  • Data Persistence

In this guide, we will set up a Redis server as a session handler for PHP on Ubuntu 22.04, ensuring seamless and scalable session management.

Step 1 – Install Redis Server

By default, the Redis package is included in the Ubuntu default repository. You can install it using the following command.

apt-get install redis-server -y

Once the Redis server is installed, verify the Redis installation using the following command.

redis-cli ping

You will see the following output.

PONG

Step 2 – Configure Redis Server

By default, Redis only allows connections to localhost. You will need to change this configuration to allow connections coming from other servers on the same private network as the Redis server.

First, edit the Redis configuration file.

nano /etc/redis/redis.conf

Change the following line:

bind localhost your-server-ip

Next, change the following line to secure your Redis with a password:

requirepass securepassword

Save and close the file, then restart the Redis service to apply the configuration.

systemctl restart redis

Step 3 – Verify Redis Connection and Authentication

Now, you will need to test Redis to see if the above configuration works. Run the following command to connect your Redis server.

redis-cli -h your-server-ip

Next, authenticate Redis with password:

AUTH securepassword

Now, try to access the data.

KEYS *

You should see the following output.

(empty array)

Step 4 – Install Apache and Redis Extension

Next, you will need to install the Apache and Redis PHP extensions to connect Redis using PHP. You can install them with the following command.

apt-get install apache2 libapache2-mod-php php php-redis -y

Next, edit the PHP configuration file.

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

Define your default session handler and session path:

session.save_handler = redis
session.save_path = "tcp://your-server-ip:6379?auth=securepassword"

Save and close the file, then restart the Apache service to apply the changes.

systemctl restart apache2

Step 5 – Verify Redis Session Handling

To verify Redis session handling, you will need to create a PHP script to store information on sessions. Let’s create a test.php file.

nano /var/www/html/test.php

Add the following code:

<?php
//simple counter to test sessions. should increment on each page reload.
session_start();
$count = isset($_SESSION['count']) ? $_SESSION['count'] : 1;
echo $count;

$_SESSION['count'] = ++$count;

Save and close the file.

Next, open your web browser and access the PHP script using the URL http://your-server-ip/test.php. Now, you should have session information stored on the Redis server. To verify it, connect to your Redis server using the following command.

redis-cli -h your-server-ip

Authenticate Redis using a password.

AUTH securepassword

Next, fetch the content from the Redis server:

KEYS *

If everything is fine, you will see the following output.

1) "PHPREDIS_SESSION:1d00u2bebh1b258e7971lt6hjp"

Conclusion

Congratulations! You have successfully set up a Redis server as a session handler for PHP on Ubuntu 22.04. By leveraging the power of Redis for session management, you enhance the scalability and performance of your web applications. Redis provides not only fast and reliable session storage but also opens the door to other advanced features offered by Redis. Explore further to unlock the full potential of Redis in your PHP projects on dedicated server hosting from Atlantic.Net!