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.
Install the required PHP extension: Run the following command to install the Redis extension:
sudo apt-get install php-redisIf
php-redisisn'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 redisEnable 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.iniRestart 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.
Create a file named
phpinfo.phpin your web root:sudo nano /var/www/html/phpinfo.phpAdd the following content:
<?php phpinfo(); ?>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.
Edit the PHP configuration file (e.g.,
/etc/php/8.3/apache2/php.ini):sudo nano /etc/php/8.3/apache2/php.iniUpdate the session settings: Find and update the following session-related settings:
session.save_handler = redis session.save_path = "tcp://127.0.0.1:6379"session.save_handler: This tells PHP to use Redis for session handling.session.save_path: This specifies the Redis server address (in this case, localhost on the default port 6379).
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.
Create a new PHP file named
session_test.php:sudo nano /var/www/html/session_test.phpAdd the following code to start a session and store data:
<?php session_start(); $_SESSION['username'] = 'JohnDoe'; echo 'Session stored in Redis: ' . $_SESSION['username']; ?>Open the file in a browser:
http://your_server_ip/session_test.php. If everything is configured correctly, you should seeSession stored in Redis: JohnDoe.Check Redis for session data: To verify that the session data is being stored in Redis, you can use the Redis CLI:
redis-cliIn 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.
Edit the
php.inifile again:sudo nano /etc/php/8.3/apache2/php.iniFind the
session.gc_maxlifetimedirective and set it to the desired session lifetime (e.g., 1 hour = 3600 seconds):session.gc_maxlifetime = 3600Restart 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:
Enable password authentication: Edit the Redis configuration file
/etc/redis/redis.confand uncomment/add the following line:sudo nano /etc/redis/redis.confFind
# requirepass foobaredand set a strong password:requirepass your_secure_passwordUpdate the PHP session save path: Update the
session.save_pathin thephp.inito include the password:session.save_path = "tcp://127.0.0.1:6379?auth=your_secure_password"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.