How to Install Redis® on FreeBSD 14.0

Updated on August 16, 2024
How to Install Redis® on FreeBSD 14.0 header image

Introduction

Redis® is an in-memory NoSQL database used as a key-value store, document database, or vector database. It supports fast read and write operations with common data structures such as Strings, Hash, List, Set, Stream, and Bitmap. Redis® is a versatile data store used with different types of storage applications such as caching, streaming, and messaging.

This article explains how to install Redis® on a FreeBSD 14.0 server.

Prerequisites

Before you begin:

Install Redis®

Redis® is available in the default package repositories on FreeBSD 14.0. Follow the steps below to install the latest available package on your server.

  1. Update the server's package index.

    console
    $ pkg update
    
  2. Search for all available Redis® packages.

    console
    $ sudo pkg search ^redis
    

    Output:

    redis-7.2.5                    Persistent key-value database with built-in net interface
    redis-devel-7.2.5.20240530     Persistent key-value database with built-in net    interface
    redis62-6.2.14_1               Persistent key-value database with built-in net interface
    redis70-7.0.15_1               Persistent key-value database with built-in net interface
    redis_exporter-1.58.0_4        Prometheus exporter for redis stats
    redisdesktopmanager-2022.5     Redis DB management tool
    redisearch-2.2.10              Full- search over Redis
    redisearch20-2.0.15            Full- search over Redis
    redisearch22-2.2.10            Full- search over Redis
    redisjson-2.0.8_20             JSON data type for Redis
  3. Install the latest available package. For example, Redis® 7.2.5.

    console
    $ sudo pkg install -y redis-7.2.5
    
  4. Enable the Redis® service to automatically start at system boot.

    console
    $ sudo service redis enable
    
  5. Start the Redis® service on your server.

    console
    $ sudo service redis start
    

    Output:

    Starting redis.
  6. View the Redis® system service status and verify that it's running.

    console
    $ sudo service redis status
    

    Output:

    redis is running as pid 1373.

Configure Redis®

Redis® uses the /usr/local/etc/redis.conf file to run on your server with specific configurations. Follow the steps below to modify the configuration file and enable Redis® to listen for incoming connection requests.

  1. Open the Redis® configuration file /usr/local/etc/redis.conf using a text editor such as vi.

    console
    $ sudo vi /usr/local/etc/redis.conf
    
  2. Set the timeout configuration directive to close the connection when a client is idle.

    ini
    timeout 3600
    
  3. Set the daemonze directive to yes to run Redis® as a daemon.

    ini
    daemonize yes
    
  4. Set the server's verbosity level.

    ini
    loglevel notice
    
  5. Set the maximum number of databases to run on the server. By default 16.

    ini
    databases 16
    
  6. Set the save configuration directive to periodically take a snapshot of available databases to disk.

    ini
    save ""
    
  7. Set the maximum number of clients that can connect at the same time.

    ini
    maxclients 10000
    
  8. Set the maximum memory usage limit.

    ini
    maxmemory 3221225472
    
  9. Enable threading when using a multi-core server.

    ini
    io-threads 1
    
  10. Enable the Append Only File (AOF) option as an alternative persistence mode to avoid database write failures.

    ini
    appendonly yes
    
  11. Enable the Redis® instance as a cluster node.

    ini
    cluster-enabled yes
    
  12. Log queries that exceed the specified execution time (microseconds).

    ini
    slowlog-log-slower-than 1000000
    

    Save and close the configuration file.

  13. Restart the Redis® service to apply the configuration changes.

    console
    $ sudo service redis restart
    

    Output:

    Stopping redis.
    Waiting for PIDS: 1605.
    Starting redis.

Secure Redis®

Follow the steps below to secure Redis®.

  1. Open the main configuration file /usr/local/etc/redis.conf.

    console
    $ sudo vi /usr/local/etc/redis.conf
    
  2. Enable protected mode to allow only local connection requests and require password authentication for all users.

    ini
    protected-mode yes
    
  3. Set the default user password.

    ini
    requirepass set-redis-password
    
  4. Bind Redis® to a specific network address on your server such as 127.0.0.1.

    ini
    bind 127.0.0.1 ::1
    
  5. Set the following configuration directive to block insecure commands.

    ini
    enable-protected-configs no
    
  6. Remove sensitive privileges from regular user accounts.

    ini
    user newuser on +@all -CONFIG ~* >newuserpw
    

    Save and close the configuration file.

  7. Restart Redis® to apply the configuration changes.

    console
    $ sudo service redis restart
    

Access Redis®

Follow the steps below to test access to the Redis® shell.

  1. Access the Redis® CLI.

    console
    $ redis-cli
    
  2. Authenticate the default user using the password you set earlier.

    console
    $ AUTH set-redis-password
    

    Output:

    OK
  3. Send a PING request to the server and verify that it's accepted.

    console
    $ PING
    

    Output:

    PONG
  4. Select a database such as 1

    console
    $ SELECT 1
    

    Output:

    OK
  5. View all secure database user profiles.

    console
    $ ACL USERS
    

    Output:

    1) "default"
  6. Create a new key in the database with the value Greetings from Vultr.

    console
    $ SET hello "Greetings from Vultr"
    

    Output:

    OK
  7. View the key value.

    console
    $ GET hello
    

    Output:

    "Greetings from Vultr" 
  8. Exit the Redis CLI.

    console
    $ quit
    

Conclusion

You have installed Redis® on FreeBSD 14.0 and secured the server to enable authentication for all database users. You can integrate Redis® databases with dynamic applications to enable caching and improve your server performance. For more information, visit the Redis® documentation.