Install Redis NoSQL/Object Cache on Ubuntu Server 18 & 20 LTS

This post is a quick post to serve as a note. Don’t expect long redundant explanations of Redis, NoSQL, Object Caching, or deployment suggestions. I would suggest a visit to their site for more detailed information. For this scenario, we are simply installing Redis on the same local machine as Apache; the following settings should be secure as long as Redis is bound to the localhost. For dedicated setups, you will require some additional configuration in the form of authentication, interface bindings, firewall permissions, and more aggressive hardware specs, specifically RAM. If you encounter any issues, feel free to leave a comment below.

*** Note: No, Redis does not automagically take a PHP application and auto-populate it with objects From a MySQL database. This optimization is done through application logic, in other words, Redis can only be taken advantage of if an application has built-in support for it. This support varies from application to application; from a typical LAMP stack, an application can leverage Redis’s in-memory database to offload or mitigate common queries/datasets from your traditional RDBMS. PHP has a Redis module like MySQL’s PDO module, which has a built-in class that can safely interact with Redis. You can learn more about it using Redis in your PHP app here.***

Step 1: Bash Into root User


1
2
    sudo bash
    ("sudo -su" if you prefer)

Enter your password to enter the root superuser account.

Step 2: Preparation

As root enter


1
     apt-get update -y && apt-get upgrade -y && apt-get dist-upgrade -y && apt-get autoremove -y && apt-get autoclean -y

Step 3: Installing Redis


1
     apt-get install redis-server -y

Once the command completes its cycle, proceed to edit redis.conf


1
     vim /etc/redis/redis.conf

Assuming your configuration is clean, you will need to edit the following lines:


1
2
     #Line 147: change default to:
     supervised systemd

1
2
     #Line 559: change default to:
     maxmemory 128mb

1
2
    #Line 590: change default to:
    maxmemory-policy allkeys-lru

Step 4: Testing The Install

To test the Redis install log into Redis’s command-line interface, enter the following command in your terminal window:


1
     redis-cli

To check if you have any data/keys set:


1
     keys *

The last command should have returned 0 or nothing. So let’s make sure Redis can record data, still inside your Redis CLI, enter the following command:


1
     SET dude "BRO"

Let’s query Redis for our stored keypair, inside Redis CLI:


1
     GET dude

You should have gotten a response of “BRO”, if not, 50 pushups noob. You can find a list of all the commands for Redis here.

Step S: Disable Transparent Huge Pages

Transparent Huge Pages support is enabled by default in Ubuntu. Like some database environments, it is recommended to disable THP where Redis is installed.

Inside the terminal run the following command:


1
     echo never > /sys/kernel/mm/transparent_hugepage/enabled

add the same command to a new line inside /etc/rc.local


1
     vim /etc/rc.local

Save and reboot.


1
     shutdown -r now (only cool kids use "reboot")

Step 5 (Optional): Install PHP Module

Redis (Native PHP)


1
     apt-get install php-redis

Redis (PHP8)


1
     apt-get install php8.0-redis

Questions, comments, memes, below.