Back to Home

Step-by-Step Guide: Installing and Configuring Redis as a PHP Session Handler on Ubuntu with PHP 8.3

Here is a step-by-step guide to installing Redis and setting it up as a session handler for a PHP application on an Ubuntu machine running PHP 8.3.

Step 1: Update Your System

Before installing any new packages, it’s always a good idea to update your system to ensure you have the latest versions of all installed software.

sudo apt-get update
sudo apt-get upgrade

Step 2: Install Redis Server

Install Redis, which will be used to store PHP session data.

sudo apt-get install redis-server

After installation, Redis should start automatically. You can check its status with:

sudo systemctl status redis

If Redis is not running, start it with:

sudo systemctl start redis

Enable Redis to start on boot:

sudo systemctl enable redis

Step 3: Install PHP Redis Extension (php-redis)

Now, install the Redis extension for PHP (php-redis), which allows PHP to interface with Redis. Since you are using PHP 8.3, you need to ensure that the extension is compatible.

  1. Install the required PHP extension: Run the following command to install the Redis extension:

    sudo apt-get install php-redis
    

    If php-redis isn't available through the package manager for PHP 8.3, you can install it via PECL (PHP Extension Community Library):

    sudo apt-get install php-pear php-dev
    sudo pecl install redis
    
  2. Enable the Redis extension: After installation, enable the extension by adding it to your PHP configuration files if necessary:

    echo "extension=redis.so" | sudo tee /etc/php/8.3/apache2/conf.d/20-redis.ini
    echo "extension=redis.so" | sudo tee /etc/php/8.3/cli/conf.d/20-redis.ini
    
  3. Restart Apache to load the new extension:

    sudo systemctl restart apache2
    

Step 4: Verify the Redis Extension Installation

To check if the Redis extension is successfully installed, create a PHP file to display the loaded PHP modules.

  1. Create a file named phpinfo.php in your web root:

    sudo nano /var/www/html/phpinfo.php
    
  2. Add the following content:

    <?php
    phpinfo();
    ?>
    
  3. Open the file in a browser by navigating to http://your_server_ip/phpinfo.php, and search for "Redis" in the page to confirm the extension is loaded.

Step 5: Configure PHP to Use Redis for Session Storage

Now that Redis is installed and the PHP Redis extension is enabled, you can configure PHP to use Redis as the session handler.

  1. Edit the PHP configuration file (e.g., /etc/php/8.3/apache2/php.ini):

    sudo nano /etc/php/8.3/apache2/php.ini
    
  2. Update the session settings: Find and update the following session-related settings:

    session.save_handler = redis
    session.save_path = "tcp://127.0.0.1:6379"
    
  3. Restart Apache to apply the changes:

    sudo systemctl restart apache2
    

Step 6: Test Session Handling with Redis

To ensure that PHP sessions are now being stored in Redis, you can create a simple PHP script to start a session and store some data.

  1. Create a new PHP file named session_test.php:

    sudo nano /var/www/html/session_test.php
    
  2. Add the following code to start a session and store data:

    <?php
    session_start();
    $_SESSION['username'] = 'JohnDoe';
    echo 'Session stored in Redis: ' . $_SESSION['username'];
    ?>
    
  3. Open the file in a browser: http://your_server_ip/session_test.php. If everything is configured correctly, you should see Session stored in Redis: JohnDoe.

  4. Check Redis for session data: To verify that the session data is being stored in Redis, you can use the Redis CLI:

    redis-cli
    

    In the Redis CLI, run the following command to list the keys:

    keys *
    

    You should see session keys like PHPREDIS_SESSION:<session_id>.

Step 7: Configure Redis for Session Expiration (Optional)

By default, Redis stores session data indefinitely unless explicitly configured to expire. You can configure session expiration by setting the gc_maxlifetime in the PHP configuration file.

  1. Edit the php.ini file again:

    sudo nano /etc/php/8.3/apache2/php.ini
    
  2. Find the session.gc_maxlifetime directive and set it to the desired session lifetime (e.g., 1 hour = 3600 seconds):

    session.gc_maxlifetime = 3600
    
  3. Restart Apache:

    sudo systemctl restart apache2
    

This ensures that session data in Redis will expire after the set time.

Step 8: (Optional) Securing Redis

Redis by default does not require authentication for local connections. For production environments, it’s recommended to secure Redis:

  1. Enable password authentication: Edit the Redis configuration file /etc/redis/redis.conf and uncomment/add the following line:

    sudo nano /etc/redis/redis.conf
    

    Find # requirepass foobared and set a strong password:

    requirepass your_secure_password
    
  2. Update the PHP session save path: Update the session.save_path in the php.ini to include the password:

    session.save_path = "tcp://127.0.0.1:6379?auth=your_secure_password"
    
  3. Restart Redis and Apache:

    sudo systemctl restart redis
    sudo systemctl restart apache2
    

Conclusion

You’ve successfully installed Redis on an Ubuntu server and configured PHP 8.3 to use Redis as a session handler. With this setup, session data will be stored in Redis, which is much more scalable and faster than file-based session handling, especially for high-traffic applications.