Redis Server and Php Extension Installation on Ubuntu 22.04 LTS

Redis is an open-source, in-memory data storage tool used by millions of developers as a cache, vector database, document database, streaming engine, and message broker. If you want to install Redis on your system, you are in the right place. In this article, we will explain the easiest way to install Redis on your system.

Where is Redis used ?

Thanks to its high performance, Redis is used to save data in cache (RAM). You can also use Redis to cache high-traffic pages. Since once cached data is no longer read from the disk but from the RAM, the reading speed performance increases at an admirable rate. Redis database is generally used in the following situations.

  • In cases where your application changes dynamically very frequently, such as user roles and permissions and session information, and you do not want the user to constantly go to the database.
  • As a message distributor in online messaging systems such as Chat.
  • In statistical operations such as site traffic data collection.
  • To keep statistics of failed logins.
  • To keep the temporary code in cache after users are sent codes such as forgot password or reset code.
  • To impose a security measure such as a rate limit on your API application.
  • To create a sorted list of scores in a game project using the Redis Sorted Set data structure.
  • or even as a message broker like RabbitMQ.

Installation

Before installing Redis server on Ubuntu, use the following command to update the system and all dependencies.

sudo apt update

Redis Server package data is available in Linux’s apt (Advanced Package Tool) repository and Redis can be installed on Ubuntu using the following command.

sudo apt install redis-server

After Redis installation, check the status of the service and whether it is running or not. So run the command below.

sudo service redis status
redis-server.service - Advanced key-value store
     Loaded: loaded (/lib/systemd/system/redis-server.service; enabled; vendor preset: enabled)
     Active: active (running) since Sat 2024-02-24 14:24:02 +03; 4h 19min ago
       Docs: http://redis.io/documentation,
             man:redis-server(1)
   Main PID: 963 (redis-server)
     Status: "Ready to accept connections"
      Tasks: 5 (limit: 4554)
     Memory: 5.6M
        CPU: 13.571s
     CGroup: /system.slice/redis-server.service
             └─963 "/usr/bin/redis-server 0.0.0.0:6379" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" ">

If you want the redis server to start automatically every time you restart your system, run the command below.

sudo systemctl enable redis-server

Local Environment Configuration

When you use Redis as a cache, you need to change some parameters in the default configuration file located in /etc/redis/redis.conf. Since there will be no need for operations such as setting a password in the Redis Configuration for your local server, it will be sufficient to make the following settings. Run the following command to edit in the terminal.

vim /etc/redis/redis.conf

The following settings will allow you to access Redis from a remote computer without any password.

bind 0.0.0.0
protected-mode no

After the changes made, you will need to restart the Redis server.

service redis-server restart

Production Environment Configuration

For security reasons in the production environment, we recommend that you close the Redis server to the outside or use it with a VPN. And you should definitely set a password. The following ip address configuration only allows access to the Redis server from the local server and denies remote access requests.


bind 127.0.0.1 ::1
protected-mode yes

Find the Require pass line and write your password in the value opposite it.

requirepass write_your_secure_password

PHP Redis Extension Installation

If you want to use the Redis server with PHP-based applications, you must install the PHP Redis module using the command below. Before installing the module, PHP must be installed on the system.

sudo apt install php-redis

Run the command below to activate Redis module PHP.

sudo phpenmod redis

If you want to disable the module, try the following command.

sudo phpdismod redis

Multiple Php Versions

If you have multiple PHP versions on your system and need to activate the PHP Redis extension for another version, follow the steps below. In this example; We assume that you have Redis installed for PHP 8.1 on your system and that you need to activate Redis for PHP 8.2.

sudo apt-get install php8.2-redis
phpenmod -v 8.2 redis

Test Redis Connection

You can test whether the Redis server is working properly using the Redis CLI, run the command below.

redis-cli ping
// PONG

Some Useful Redis-Cli Commands

redis-cli info stats
redis-cli info server

You can use the following command to list all registered data keys on the Redis server.

redis-cli --scan --pattern '*'
oloma:policies:privacy-policy:tr
oloma:policies:privacy-policy:en
oloma:policies:end-user-license-agreement:en
oloma:policies:data-protection-and-processing-policy:en
oloma:policies:cookie-policy:en
oloma:policies:gdpr-information-text:en
demo_app:App\Model\PermissionModel:findPermissions
tr_oloma_news_feed_data
en_oloma_news_feed_data

The following command will quickly solve your problem to delete/clear all saved cache on the server.

FLUSHALL

Redis Auth

It is not possible to run redis commands in a production environment without authentication. For this you need to use the auth command.

auth your-password

Redis Desktop Applications

Finally, let’s talk about desktop applications with a visual interface that will allow you to easily manage your Redis database.

Resp.app

If you are using Windows, you can connect to the redis server locally or remotely with the resp.app application, which has an easy interface, and you can view the data you have created in the database in different formats with this free application.

You can download the resp.app application from the Microsoft store for Windows operating systems from the link below.
https://apps.microsoft.com/detail/9ndk76zvz3tm?hl=en-US&gl=TM

RedisInsight

RedisInsight Developed by Redis; A redis interface with advanced features that can also run on operating systems such as Windows, macOS (including M1), Linux.

You can download the RedisInsight application from the following link.
https://redis.com/redis-enterprise/redis-insight/

Conclusion

Redis Server is an open source software used mainly for data storage and object caching. As we mentioned above, Redis Server can be easily installed on Ubuntu/Linux operating systems. In this article, you have installed and configured Redis, verified whether your Redis installation is working properly, and completed the Redis installation steps safely for the production environment.

Note that once someone logs into your server, it is very easy to bypass the Redis-specific security features we have implemented. Therefore, the most important security measure on your Redis server in the production environment is access to the server through a firewall or a private network such as a VPN.

Node.js Installation with NVM Manager on Ubuntu 22.04 LTS

Node.js is a JavaScript execution tool for server-side programming. It allows developers to create scalable backend functionality using JavaScript, a language most people are already familiar with from browser-based web development.

In this article, we will show you the most flexible way to install Node.js on an Ubuntu 22.04 server with Nvm (Node Version Manager).

Before starting the installation, let’s learn the latest nvm version from the official nvm repository https://github.com/nvm-sh/nvm/releases. The current official version appears to be 0.39.7.

NVM Installation

You can use the apt package manager to get the new version of nvm. First refresh your local package directory by typing:

sudo apt update

Then download the Nvm installation sh file according to the current version:

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash

The above code will install the nvm script into your user account. To use this, you first need to source your .bashrc file:

source ~/.bashrc

Use the nvm –version command to see which nvm version we are using.

nvm --version
0.39.7

You can now ask NVM which Node versions are available and proceed with the installation steps for Node.js based on the latest version:

nvm list-remote
// output
        v20.4.0
        v20.5.0
        v20.5.1
        v20.6.0
        v20.6.1
        v20.7.0
        v20.8.0
        v20.8.1
        v20.9.0   (LTS: Iron)
       v20.10.0   (LTS: Iron)
       v20.11.0   (LTS: Iron)
->     v20.11.1   (Latest LTS: Iron)
        v21.0.0
        v21.1.0
        v21.2.0
        v21.3.0
        v21.4.0
        v21.5.0
        v21.6.0
        v21.6.1
        v21.6.2

Node.js Installation

Copy the latest updated version from the versions listed above and run the following command to install this Node version:

nvm install 20.11.1

If there is more than one node version on your machine, you can make nvm use the version you want with the nvm use command.

nvm use 20.11.1

The version selected with the Use command will only have effect in your current session. If you want to make the version you selected permanent as the default, run the command below.

nvm alias default node

You can see the different versions you have installed and which current version you are currently using with the nvm list command:

nvm list
->     v20.11.1
default -> node (-> v20.11.1)
node -> stable (-> v20.11.1) (default)
stable -> 20.11 (-> v20.11.1) (default)

Uninstalling Node.js

To uninstall a version of Node.js that you installed using nvm, first determine whether it is the current active version:

nvm current
v20.11.1

If the version you are targeting is not the current active version, run the following command:

nvm uninstall v19.1.0
// Output
Uninstalled node v19.1.0

This command will remove the selected Node.js version. If the version you want to remove is the currently active version, you will first need to disable nvm to activate your changes:

nvm deactivate

After this process, you can remove the current version using the uninstall command used previously.

Conclusion

There are many ways to install and run Node.js on your Ubuntu 22.04 server. While it’s easiest to use the bundled version from the Ubuntu repository, using Nvm or NodeSource PPA offers additional flexibility.

For more information about programming with Node.js, please see the Node.js api documentation.