Installation of Redis on Ubuntu Server

Last updated December 6th, 2023 00:15

If you run a WordPress website, for example, using your own VPS, you may have already heard about object caching. After all, WordPress itself will alert you to its usage in the site health section. Object caching in WordPress, such as using Redis, is a powerful tool for optimizing performance and speeding up the loading of web pages. Installation of Redis on Ubuntu server is not significantly complicated. Let’s take a look at how to do it.

Installation of Redis on Ubuntu Server

Object caching

When a visitor loads a page in WordPress, the system has to perform various operations, such as retrieving data from the database, generating content, and creating the HTML page. This process can be time-consuming and may slow down page loading.

This is where object caching comes into play. Simply put, a cache is a memory storage that stores the results of frequently used operations. When a visitor requests a page, WordPress first checks if the result of the operation is stored in the cache.

Using Redis as an object cache has the advantage that Redis is a fast and scalable in-memory database. Redis stores data in a key-value format, ensuring fast retrieval and storage of data.

When the result of an operation is stored in Redis, WordPress retrieves it directly from the cache instead of performing the entire operation again. This significantly speeds up page loading and reduces the server load.

That’s about object caching and Redis in WordPress (the article will also cover it separately). Now let’s move on to the actual installation.

Installation

First, as always, update the list of available packages:

				
					sudo apt update
				
			

Immediately after, you can enter the command to install Redis:

				
					sudo apt install redis-server
				
			

This command will download and install Redis along with its dependencies. Now you will need to make an important configuration change. Using the nano text editor, open the Redis configuration file:

				
					sudo nano /etc/redis/redis.conf
				
			

In the redis.conf file, locate the “supervised” directive and change its value from “no” to “systemd”.

				
					# If you run Redis from upstart or systemd, Redis can interact with your
# supervision tree. Options:
#   supervised no      - no supervision interaction
#   supervised upstart - signal upstart by putting Redis into SIGSTOP mode
#   supervised systemd - signal systemd by writing READY=1 to $NOTIFY_SOCKET
#   supervised auto    - detect upstart or systemd method based on
#                        UPSTART_JOB or NOTIFY_SOCKET environment variables
# Note: these supervision methods only signal "process is ready."
#       They do not enable continuous liveness pings back to your supervisor.
supervised systemd
				
			

Once you make this change, save the new configuration by using the CTRL + X shortcut. Then confirm the change by pressing the letter Y and pressing Enter.

Now you can restart the Redis service for the configuration change to take effect.

				
					sudo systemctl restart redis.service
				
			

Test Redis

First, use the command below to display the status of the Redis service:

				
					sudo systemctl status redis
				
			

You should receive an output similar to the one shown below. The important item for you is the “Active” field, which should display “active (running)”.

				
					redis-server.service - Advanced key-value store
     Loaded: loaded (/lib/systemd/system/redis-server.service; enabled; vendor preset: enabled)
     Active: active (running) since Thu 2020-04-30 23:26:54 UTC; 4s ago
       Docs: http://redis.io/documentation,
             man:redis-server(1)
    Process: 36552 ExecStart=/usr/bin/redis-server /etc/redis/redis.conf (code=exited, status=0/SUCCESS)
   Main PID: 36561 (redis-server)
      Tasks: 4 (limit: 2345)
     Memory: 1.8M
     CGroup: /system.slice/redis-server.service
             └─36561 /usr/bin/redis-server 127.0.0.1:6379
				
			

To test it, connect to the server using redis-cli:

				
					redis-cli
				
			

Now you can test the connectivity using the ping command:

				
					ping
				
			

In response, the Redis server should humorously reply with the word “PONG”. Now you can try setting keys. Enter the command:

				
					set test "It's working!"
				
			

You will receive “OK” as the output. Now, try to retrieve the value back to see if it works:

				
					get test
				
			

Here, you will receive the pre-defined text “It’s working!” as the output. Use the “exit” command to end the connection to Redis and restart the service.

				
					sudo systemctl restart redis
				
			

If you want to test whether the values persist on the server even after restarting the service, you can reconnect for the test once again and retrieve the same value you entered a while ago.

				
					redis-cli
				
			
				
					get test
				
			

Even after the previous service restart, Redis should return the previously set value “It’s working!” as the output.

Binding to localhost

By default, Redis should be bound only to localhost. However, if you installed Redis following a different guide, you may have misconfigured settings. To fix or at least check the Redis configuration, open its configuration file:

				
					sudo nano /etc/redis/redis.conf
				
			

Find the line below and make sure there is no deleted comment character “#” before the line. If not, delete the “#” character.

				
					bind 127.0.0.1 ::1
				
			

Save the changes in the configuration file again using CTRL+X. Once you have modified the configuration file, restart Redis again.

				
					sudo systemctl restart redis
				
			

Perform a check using the netstat command:

				
					sudo netstat -lnp | grep redis
				
			

You should receive the following output as the correct result:

				
					tcp        0      0 127.0.0.1:6379          0.0.0.0:*               LISTEN      14222/redis-server  
tcp6       0      0 ::1:6379                :::*                    LISTEN      14222/redis-server 
				
			

Note: The netstat command may not be available on your server. If that’s the case, you need to install the netstat tool first.

				
					sudo apt install net-tools
				
			

The netstat test above should show you that the Redis server is bound only to localhost (127.0.0.1). If you see a different IP address in the output, perform a double check to ensure that you correctly removed the “#” character on the correct line (bind 127.0.0.1 ::1) and saved the configuration changes properly. Then, restart Redis again.

Redis Installation on Ubuntu Server. Conclusion

Now you have Redis up and running on Ubuntu, ready to be used. We will demonstrate how to enable object caching using Redis, for example, in WordPress, in a separate article.

The website is created with care for the included information. I strive to provide high-quality and useful content that helps or inspires others. If you are satisfied with my work and would like to support me, you can do so through simple options.

Byl pro Vás tento článek užitečný?

Klikni na počet hvězd pro hlasování.

Průměrné hodnocení. 0 / 5. Počet hlasování: 0

Zatím nehodnoceno! Buďte první

Jak užitečný vidíte tento článek.

Sledujte mě na sociálních médiích.

Je mi líto, že pro Vás nebyl článek užitečný.

Jak mohu vylepšit článek?

Řekněte mi, jak jej mohu zlepšit.

newsletter

Subscribe to the Newsletter

Stay informed! Join our newsletter subscription and be the first to receive the latest information directly to your email inbox. Follow updates, exclusive events, and inspiring content, all delivered straight to your email.

Odebírat
Upozornit na
guest
0 Komentáře/ů
Nejstarší
Nejnovější Nejvíce hlasováno
Vložené zpětné vazby.
Zobrazit všechny komentáře.

Pokud mi chcete napsat rychlou zprávu, využije, prosím, níže uvedený
kontaktní formulář. Děkuji.

Další Kontaktní údaje