Using Redis to store PHP session with Plesk VPS

Harish Sharma
3 min readJan 30, 2022

During initial growth days of Snap Homework App when traffic and active users begin to go northwards, to quickly solve the challenge we did Vertical scaling but I knew that it is not going to work for long, as we were growing rapidly and with Vertical scale we will soon hit the limit.

Horizontal Scaling for Application Server..

I decided with team that we need to get our DB and Application server ready to be scaled horizontally. For database we went to Master-Slave replication architecture as we have more reads happening as compared to the writes. For Application server, since our application was using sessions, we needed a separate server to store sessions and for that purpose we decided to go with Redis.

Load Balancer Setup — PHP — Redis

Redis Setup

I have used GCP for all my VM, so for this one too used GCP. And i personally like the CentOS over all other unix flavors. Once i had the CentOS7 up and running, installing Redis was easy and there are tons of tutorials available. I used this one — https://www.digitalocean.com/community/tutorials/how-to-install-secure-redis-centos-7 to complete my initial setup.

In nutshell, you need to execute following commands on your CentOS server-

1- sudo yum update

2-sudo yum install epel-release

3-sudo yum install redis -y

4-sudo systemctl start redis.service

5- sudo systemctl enable redis

6- sudo systemctl status redis.service

That’s it! Your Redis server is up and running…

Connecting Application Server (Plesk) and Redis

The session handler is responsible for storing and retrieving data saved into sessions —PHP by default uses files for that. A central session handler like Redis is used, which acts as central location to share sessions information in scalable applications behind load balancer.

Now as you add multiple VM for your application server behind load balancer and your application will store sessions, these sessions need to stored on single location, so that all PHP application (in our case we are using PHP, with Plesk managed server) running on your multiple application server can access it.

Now as Plesk comes pre-loaded with PHP Redis extension, so you do not have to install or configure any additional settings on your PHP server. Go to your Plesk server admin login, select the domain where you wish to setup and perform following steps -

Step1 -

Go to PHP settings

Step2-

Update session.save.path

Save and Voila!

Now you should have session information stored on the Redis server. To verify if PHP sessions are being saved on redis machine, open SSH/Terminal of Redis server and on command line type:

redis-cli -h serverIP (this will prompt for password)

server.ip> *keys *

And you should get an output similar to this:

Output

1) “PHPREDIS_SESSION:j9rsgtde6st2rqb6lu5u6f4h83”

This shows that the session information is being stored on the Redis server. You can now connect your additional PHP servers to the Redis server in a similar fashion.

--

--