· Kalpa Madhushan · documentation · 3 min read

How to Install Redis Server and Access It from Anywhere

Learn how to install and configure Redis Server for remote access, along with a C example using hiredis to interact with the Redis database.

Learn how to install and configure Redis Server for remote access, along with a C example using hiredis to interact with the Redis database.

Setting Up Redis Server and Accessing It from Anywhere

Redis is an in-memory data structure store, used as a database, cache, and message broker. This article guides you through the steps to install Redis on an Ubuntu server, configure it for remote access, and connect to it from another server using hiredis in C.

Step 1: Install Redis Server

First, update your package list to ensure you have the latest information about available packages:

sudo apt update

This command fetches the latest package information from the repositories.

Next, install the Redis server package:

sudo apt install redis-server

This command installs the Redis server and its dependencies.

Step 2: Start and Enable Redis Server

Start the Redis server to begin using it:

sudo systemctl start redis-server

This command starts the Redis server.

Enable Redis to start on boot:

sudo systemctl enable redis-server

This command ensures that Redis will automatically start when the server boots up.

Check the status of the Redis server:

sudo systemctl status redis-server

This command provides the current status of the Redis server, confirming whether it is active and running.

Step 3: Configure Redis for Remote Access

Edit the Redis configuration file to allow remote connections:

sudo nano /etc/redis/redis.conf

Find the bind directive and change it to 0.0.0.0 to listen on all interfaces. You can also specify a specific IP address if needed, but in some cases, this might cause issues:

bind 0.0.0.0

Disable protected mode to allow remote access:

protected-mode no

Save and close the file.

Step 4: Restart Redis Server

After making configuration changes, restart the Redis server:

sudo systemctl restart redis-server

This command restarts the Redis service to apply the new configuration settings.

Step 5: Allow Redis Port in Firewall

Allow traffic on the Redis port (default is 6379) through the firewall:

sudo ufw allow 6379

Enable the firewall if it is not already enabled:

sudo ufw enable

(Optional) Set a Password for Redis

For added security, you can set a password in the Redis configuration file, but this step is optional:

requirepass YourStrongPasswordHere

Every time you make a change to the configuration, restart the Redis server to apply the changes:

sudo systemctl restart redis-server

Connecting to Redis Server with hiredis in C

Here is an example of how to connect to the Redis server from another server using the hiredis library in C.

Code Example

#include <stdio.h>
#include <stdlib.h>
#include <hiredis/hiredis.h>

int main() {
    const char *srchostname = "REDIS_IP";
    int redisport = 6379; // Default Redis port
    struct timeval rtimeout = { 1, 500000 }; // 1.5 seconds
    redisContext *rc;

    rc = redisConnectWithTimeout(srchostname, redisport, rtimeout);
    if (rc == NULL || rc->err) {
        if (rc) {
            printf("Redis Connection error: %s\n", rc->errstr);
            redisFree(rc);
        } else {
            printf("Redis Connection error: can't allocate redis context\n");
        }
        exit(1);
    }
    printf("Redis connection Success \n");

    // Ping Pong
    redisReply *reply;
    reply = redisCommand(rc, "PING");
    printf("PING: %s\n", reply->str);
    freeReplyObject(reply);

    // Set and Get example
    reply = redisCommand(rc, "SET mykey myvalue");
    printf("SET: %s\n", reply->str);
    freeReplyObject(reply);

    reply = redisCommand(rc, "GET mykey");
    printf("GET mykey: %s\n", reply->str);
    freeReplyObject(reply);

    // Cleanup
    redisFree(rc);
    return 0;
}

Replace REDIS_IP with the IP address of your Redis server.

Conclusion

By following these steps, you have installed Redis, configured it for remote access, and connected to it using hiredis in C. This setup allows you to utilize Redis as a powerful, in-memory database accessible from anywhere.

Back to Blog

Related Posts

View All Posts »
How to Install hiredis and Use in C

How to Install hiredis and Use in C

Learn how to install the hiredis library and use it in your C projects to interact with Redis, ensuring efficient and effective database management.