VOOZH about

URL: https://www.digitalocean.com/community/tutorials/how-to-set-up-a-redis-server-as-a-session-handler-for-php-on-ubuntu-14-04

⇱ How to Set Up a Redis Server as a Session Handler for PHP on Ubuntu 14.04 | DigitalOcean


How to Set Up a Redis Server as a Session Handler for PHP on Ubuntu 14.04

Published on August 21, 2015
πŸ‘ How to Set Up a Redis Server as a Session Handler for PHP on Ubuntu 14.04

Introduction

Redis is an open source key-value cache and storage system, also referred to as a data structure server for its advanced support for several data types, such as hashes, lists, sets, and bitmaps, amongst others. It also supports clustering, which makes it often used for highly-available and scalable environments.

In this tutorial, we’ll see how to install and configure an external Redis server to be used as a session handler for a PHP application running on Ubuntu 14.04.

The session handler is responsible for storing and retrieving data saved into sessions - by default, PHP uses files for that. An external session handler can be used for creating scalable PHP environments behind a load balancer, where all application nodes will connect to a central server to share session information.

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products

About the author(s)

πŸ‘ Erika Heidi
Erika Heidi
Author
Developer Advocate
See author profile

Dev/Ops passionate about open source, PHP, and Linux. Former Senior Technical Writer at DigitalOcean. Areas of expertise include LAMP Stack, Ubuntu, Debian 11, Linux, Ansible, and more.

Current fan and former Editorial Manager at DigitalOcean. Hi! Expertise in areas including Ubuntu, Docker, Rails, and more.

Still looking for an answer?

Was this helpful?

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Can I firstly say, great how-to!

Secondly, more of a comment on the PPA. Did you know ubuntu-14.04 has redis-server bundled and can support everything you do in the tutorial with sudo apt-get install redis-server from vanilla 14.04?

I know it’s probably not the latest stable, but sometimes distro-maintained is a good thing. Unless there is a CVE the new stable release fixes, and most of the time distro-maintained will backport security patches.

Anyway, my comment is largely irrelevant, I just wondered if you knew, and wanted to say well done on the tutorial.

Does anyone have experience or data as to how this performs vs memcached or standard session handling?

…Will this create any conflict if i have Memcached installed?

Great how to! Easy to follow for a novice. Would love to see a how to for getting a php page to make redis set/get calls! I am not having much luck following what’s out on the web.

Thanks, Jason

So in a server environment with a web server and a database server, where would you advise Redis to be installed for best performance?

Also in this tutorial is redis performing object-caching?

Thanks, JL

FYI:

I had used apg to generate a 72 character-long password for my Redis Server and found that tcp:// requests were failing. The password string contained several non-alphanumeric (ie special) characters.

Solved by:

Encode the string before sending to a port. I had success with:

  • php: urlencode() - longer
  • -and-
  • js: encodeURIComponent() - shorter

If you are using PHP 7.0 FPM, whether you are using Nginx or Apache, if you realise the instructions above doesn’t work for you, and redis-cli doesn’t show your saved session.

you should go to the following file

 /etc/php/7.0/mods-available/redis.ini

and add the following lines

session.save_handler = redis
session.save_path = "tcp://127.0.0.1:6379?auth=supercomplexpasswordthatyousetbefore"

save and exit, and restart php fpm

sudo service php7.0-fpm restart

Your session should now show when you type keys *

I’m using Ubuntu 16.04

Hi,

Can anyone help me with the setup of multi master node redis setup?

How do you handle cleaning up expired sessions using this method? Will allkeys-lru maxmemory-policy work, or does an explicit expiration need to be set?

@erikaheidi Hey Erika! Thanks for this much!

I am trying to setup redis in an environment, where I need wordpress to connect securely to a VPS (Digital Ocean droplet) hosting a redis server.

I have managed this:

Webserver - > SSH RSA PK AUTH - > Redis VPS

Now I need this:

Authenticated Web Server - > Redis Instance - > Object Cache - > Session Handling - > DB store

Is that doable?

All default TCP connections are blocked on the droplet Only SSH via RSA is allowed on the droplet

πŸ‘ Creative Commons
This work is licensed under a Creative Commons Attribution-NonCommercial- ShareAlike 4.0 International License.
  • Deploy on DigitalOcean

    Click below to sign up for DigitalOcean's virtual machines, Databases, and AIML products.

Become a contributor for community

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

DigitalOcean Documentation

Full documentation for every DigitalOcean product.

Resources for startups and AI-native businesses

The Wave has everything you need to know about building a business, from raising funding to marketing your product.

Get our newsletter

Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

New accounts only. By submitting your email you agree to our Privacy Policy

The developer cloud

Scale up as you grow β€” whether you're running one virtual machine or ten thousand.

Start building today

From GPU-powered inference and Kubernetes to managed databases and storage, get everything you need to build, scale, and deploy intelligent applications.

Β© 2026 DigitalOcean, LLC.Sitemap.
Dark mode is coming soon.