Install REDIS on web server

Photo of author

By seocheckfree

Learn how to install Redis, a fast and versatile database system, on different web servers and platforms. Follow the step-by-step instructions and code examples to set up Redis on Apache, Nginx, cpanel, and Aapanel. Redis can improve the performance and functionality of your web applications by providing in-memory data storage, caching, message brokering, and more.

Redis is a database system that stores data in memory and supports various types of data structures. It is often used as a cache, message broker, or pub/sub-system. According to the DB-Engines ranking, Redis is currently the 8th most widely used database management system, as well as the most popular choice worldwide for key-value databases.

Why Use REDIS?

Some of the reasons why people use Redis are:

  • It is very fast, since it operates on in-memory data.
  • It supports a wide range of data structures, such as strings, lists, sets, hashes, bitmaps, hyperloglogs, streams, and more.
  • It offers high availability and scalability through replication, clustering, and sentinel.
  • It provides persistence and durability by saving data to disk periodically or on every write.
  • It supports transactions, scripting, pub/sub, and other advanced features.

REDIS Pros and Cons

Some of the pros and cons of Redis are:

Pros:

Cons:

How to Install Redis on Apache Server?

To install Redis on Apache, you need to follow these steps:

  • Install Redis on your server using the appropriate package manager for your operating system. For example, on Ubuntu you can use sudo apt-get install redis-server.
  • Configure Redis to listen on a port that is not used by Apache. You can edit the /etc/redis/redis.conf file and change the port parameter to a different value, such as 6379.
  • Restart Redis to apply the changes using sudo service redis-server restart.
  • Install a PHP extension for Redis, such as phpredis or predis. You can use sudo pecl install redis or sudo composer require predis/predis respectively.
  • Enable the extension in your php.ini file by adding a line like extension=redis.so or extension=predis.so.
  • Restart Apache to load the extension using sudo service apache2 restart.
  • Test the connection to Redis from your PHP code using a simple script like this:
<?php
// create a new Redis object
$redis = new Redis();
// connect to Redis server on port 6379
$redis->connect('127.0.0.1', 6379);
// set a key-value pair
$redis->set('foo', 'bar');
// get the value of the key
echo $redis->get('foo');
?>

How To Install Redis on Nginx Server?

To install Redis on Nginx, you need to follow these steps:

  • Install Redis on your server using the appropriate package manager for your operating system. For example, on Ubuntu you can use sudo apt-get install redis-server.
  • Configure Redis to listen on a port that is not used by Nginx. You can edit the /etc/redis/redis.conf file and change the port parameter to a different value, such as 6379.
  • Restart Redis to apply the changes using sudo service redis-server restart.
  • Install a Lua module for Nginx, such as lua-resty-redis or lua-resty-redisc. You can use sudo luarocks install lua-resty-redis or sudo luarocks install lua-resty-redisc respectively.
  • Configure Nginx to load the Lua module and access Redis from your server blocks. You can edit the /etc/nginx/nginx.conf file and add lines like these:
# load the Lua module
load_module modules/ngx_http_lua_module.so;
# set the Lua package path
lua_package_path '/usr/local/share/lua/5.1/?.lua;;';
# create a Lua shared dict for caching Redis connections
lua_shared_dict redis 10m;
# example server block
server {
    listen 80;
    server_name example.com;
    location / {
        # create a new Lua object
        content_by_lua_block {
            local redis = require "resty.redis"
            local red = redis:new()
            # set timeout for connecting to Redis
            red:set_timeout(1000)
            # connect to Redis server on port 6379
            local ok, err = red:connect("127.0.0.1", 6379)
            if not ok then
                ngx.say("failed to connect: ", err)
                return
            end
            # set a key-value pair
            ok, err = red:set("foo", "bar")
            if not ok then
                ngx.say("failed to set foo: ", err)
                return
            end
            # get the value of the key
            local res, err = red:get("foo")
            if not res then
                ngx.say("failed to get foo: ", err)
                return
            end
            # close the connection or put it into the connection pool
            local ok, err = red:set_keepalive(10000, 100)
            if not ok then
                ngx.say("failed to set keepalive: ", err)
                return
            end
            # output the result
            ngx.say(res)
        }
    }
}
  • Restart Nginx to apply the changes using sudo service nginx restart.
  • Test the connection to Redis from your browser by visiting your domain name or IP address. For example, you can use http://example.com/ or http://127.0.0.1/.

Install Redis on cPanel

To install Redis on cPanel, you need to follow these steps:

  • Log in to your cPanel account and go to the Software section.
  • Click on the Setup Python App icon and create a new application with the following settings:
    • Python version: choose any version you prefer, such as 3.9.
    • Application root: enter a name for your application directory, such as redis_app.
    • Application URL: enter a URL for your application, such as /redis.
    • Application startup file: enter a name for your main Python file, such as app.py.
    • Application entry point: enter a name for your main Python function, such as app.
    • Passenger log file: leave it blank or enter a name for your log file, such as passenger.log.
  • Click on the Create button and wait for the application to be created.
  • Click on the Manage button next to your application and go to the Modules tab.
  • Search for redis in the search box and click on the Install button next to it. Wait for the module to be installed.
  • Go back to the Overview tab and click on the Open in Terminal button. A new terminal window will open in your browser.
  • In the terminal, create a new Python file with the name you entered in the application startup file. For example, you can use nano app.py.
  • In the Python file, write some code to connect to Redis and perform some operations. For example, you can use this code:
# import Redis module
import redis
# create a Flask app object
from flask import Flask
app = Flask(__name__)
# create a Redis object
r = redis.Redis()
# define a route for the root URL
@app.route("/")
def index():
    # set a key-value pair
    r.set("foo", "bar")
    # get the value of the key
    value = r.get("foo")
    # return the value as a response
    return value
  • Save and exit the file using Ctrl+O and Ctrl+X.
  • Restart your application using the Restart button in the cPanel interface.
  • Test the connection to Redis from your browser by visiting your application URL. For example, you can use http://example.com/redis.

How To install Redis on aaPanel

How To install Redis on aaPanel

To install Redis on aaPanel, you need to follow these steps:

  • Log in to your aaPanel account and go to the Software Store section.
  • Click on the Redis icon and click on the Install button. Wait for Redis to be installed.
  • Click on the Config File button and edit the /www/server/redis/redis.conf file. Change the port parameter to a different value, such as 6379, if you want to use a custom port for Redis. Save and close the file.
  • Click on the Restart button to restart Redis.
  • Click on the PHP Extensions icon and install a PHP extension for Redis, such as phpredis or predis. You can use pecl install redis or composer require predis/predis respectively.
  • Enable the extension in your PHP configuration by adding a line like extension=redis.so or extension=predis.so.
  • Restart PHP to load the extension using the Restart button in aaPanel.
  • Test the connection to Redis from your PHP code using a simple script like this:
<?php
// create a new Redis object
$redis = new Redis();
// connect to Redis server on port 6379
$redis->connect('127.0.0.1', 6379);
// set a key-value pair
$redis->set('foo', 'bar');
// get

Leave a Comment