Table of Contents
Building Redis
Installation
System parameters
redis.conf
daemonize
pidfile
port
loglevel
logfile
dir
Security
Unix sockets
requirepass
rename-command
Snapshot
Start at boot
什么是Linux系统
Home Database Redis How to install Redis server on CentOS 7

How to install Redis server on CentOS 7

May 31, 2023 am 08:25 AM
linux redis server

Redis is an open source multi-platform data storage software written in ANSI C. Redis can support Lua, C, Java, Python, Perl, PHP and many other languages.

Building Redis

redis currently does not have an official RPM installation package. We need to compile from source code, and in order to compile, we need to install Make and GCC.

If GCC and Make have not been installed, use yum to install them.

yum install gcc make
Copy after login

Download the tar compressed package from the official website.

curl http://download.redis.io/releases/redis-3.0.4.tar.gz -o redis-3.0.4.tar.gz
Copy after login

Unzip.

tar zxvf redis-3.0.4.tar.gz
Copy after login

Enter the decompressed directory.

cd redis-3.0.4
Copy after login

Use Make to compile source files.

make
Copy after login

Installation

Enter the directory of the source file.

cd src
Copy after login

Copy the Redis server and client to /usr/local/bin.

cp redis-server redis-cli /usr/local/bin
Copy after login

It is best to copy sentinel, benchmark and check.

cp redis-sentinel redis-benchmark redis-check-aof redis-check-dump /usr/local/bin
Copy after login

Create redis configuration folder.

mkdir /etc/redis
Copy after login

Create a valid directory to save data under /var/lib/redis

mkdir -p /var/lib/redis/6379
Copy after login
System parameters

In order for redis to work properly, some kernel parameters need to be configured.

Configure vm.overcommit_memory to 1, which can avoid data being truncated. See here for details.

sysctl -w vm.overcommit_memory=1
Copy after login

Modify the maximum number of backlog connections to exceed the tcp-backlog value in redis.conf, which is the default value of 511. More information about sysctl-based IP network tunneling can be found at kernel.org.

sysctl -w net.core.somaxconn=512
Copy after login

Cancel support for transparent huge pages, because this will cause delays and memory access problems during the use of redis.

echo never > /sys/kernel/mm/transparent_hugepage/enabled
Copy after login
Copy after login

redis.conf

redis.conf is the configuration file of redis. However, you will see that the name of this file is 6379.conf, and this number is the network port that redis listens on. To run multiple redis instances, the following naming scheme is recommended.

Copy the sample redis.conf to /etc/redis/6379.conf.

cp redis.conf /etc/redis/6379.conf
Copy after login

Now edit this file and configure parameters.

vi /etc/redis/6379.conf
Copy after login
daemonize

Set daemonize to no, systemd needs it to run in the foreground, otherwise redis will hang suddenly.

daemonize no
Copy after login
pidfile

Set pidfile to /var/run/redis_6379.pid.

pidfile /var/run/redis_6379.pid
Copy after login
port

If you do not plan to use the default port, you can modify it.

port 6379
Copy after login
loglevel

Set the log level.

loglevel notice
Copy after login
logfile

Modify the log file path.

logfile /var/log/redis_6379.log
Copy after login
dir

Set the directory to /var/lib/redis/6379

dir /var/lib/redis/6379
Copy after login

Security

There are several operations that can improve security.

Unix sockets

Because the client program and the server program usually run on the same machine, there is no need to listen to the network socket. If this is similar to your use case, you can use a unix socket instead of a network socket. To do this, you need to configure port to 0, and then configure the following options to enable the unix socket.

Set the socket file of unix socket.

 unixsocket /tmp/redis.sock
Copy after login

Restrict the permissions of socket files.

unixsocketperm 700
Copy after login

Now in order for redis-cli to be accessible, the -s parameter should be used to point to the socket file.

redis-cli -s /tmp/redis.sock
Copy after login
requirepass

You may need remote access, if so, then you should set a password so that it is required before each operation.

requirepass "bTFBx1NYYWRMTUEyNHhsCg"
Copy after login
rename-command

Imagine the output of the following command. Yes, this will output the server's configuration, so you should deny this access whenever possible.

CONFIG GET *
Copy after login

You can use the "rename-command" command to limit or prohibit the use of this or other instructions. You must provide a command name and an alternative name. To make it safer to ban a command, its alternative name should be set to an empty string so that no one can guess the name of the command.

rename-command FLUSHDB "FLUSHDB_MY_SALT_G0ES_HERE09u09u"rename-command FLUSHALL ""rename-command CONFIG "CONFIG_MY_S4LT_GO3S_HERE09u09u"
Copy after login

如何在CentOS 7上安装Redis服务器

Use password to access through unix socket, and modify the command

Snapshot

By default, redis The data set will be periodically dumped to the dump.rdb file in the directory we set. You can configure the frequency of dumps using the save command, whose first parameter is the time frame in seconds and the second parameter is the number of modifications to be made on the data file.

Every 15 minutes and the key has been modified at least once.

save 900 1
Copy after login

Every 5 minutes and the key has been modified at least 10 times.

save 300 10
Copy after login

Every 1 minute and the key has been modified at least 10,000 times.

save 60 10000
Copy after login

The file /var/lib/redis/6379/dump.rdb contains the dump data of the in-memory dataset since the last save. Because it first creates a temporary file and then replaces the previous dump file, there is no problem of data corruption. You don't have to worry, you can just copy the file.

Start at boot

You can use systemd to add redis to the system boot list.

Copy the example init_script file to /etc/init.d, pay attention to the port number represented by the script name.

cp utils/redis_init_script /etc/init.d/redis_6379
Copy after login

Now we want to use systemd, so create a unit file named redis_6379.service under /etc/systems/system.

vi /etc/systemd/system/redis_6379.service
Copy after login

Fill in the following content, see systemd.service for details.

[Unit]Description=Redis on port 6379[Service]Type=forkingExecStart=/etc/init.d/redis_6379 startExecStop=/etc/init.d/redis_6379 stop[Install]WantedBy=multi-user.target
Copy after login

现在添加我之前在 /etc/sysctl.conf 里面修改过的内存过量使用和 backlog 最大值的选项。

vm.overcommit_memory = 1net.core.somaxconn=512
Copy after login

对于透明巨页内存支持,并没有直接 sysctl 命令可以控制,所以需要将下面的命令放到 /etc/rc.local 的结尾。

echo never > /sys/kernel/mm/transparent_hugepage/enabled
Copy after login
Copy after login

这样就可以启动了,通过设置这些选项你就可以部署 redis 服务到很多简单的场景,然而在 redis.conf 还有很多为复杂环境准备的 redis 选项。在一些情况下,你可以使用 replication 和 Sentinel 来提高可用性,或者将数据分散在多个服务器上,创建服务器集群。

什么是Linux系统

Linux是一种免费使用和自由传播的类UNIX操作系统,是一个基于POSIX的多用户、多任务、支持多线程和多CPU的操作系统,使用Linux能运行主要的Unix工具软件、应用程序和网络协议。

The above is the detailed content of How to install Redis server on CentOS 7. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Linux Architecture: Unveiling the 5 Basic Components Linux Architecture: Unveiling the 5 Basic Components Apr 20, 2025 am 12:04 AM

The five basic components of the Linux system are: 1. Kernel, 2. System library, 3. System utilities, 4. Graphical user interface, 5. Applications. The kernel manages hardware resources, the system library provides precompiled functions, system utilities are used for system management, the GUI provides visual interaction, and applications use these components to implement functions.

How to check the warehouse address of git How to check the warehouse address of git Apr 17, 2025 pm 01:54 PM

To view the Git repository address, perform the following steps: 1. Open the command line and navigate to the repository directory; 2. Run the "git remote -v" command; 3. View the repository name in the output and its corresponding address.

How to run java code in notepad How to run java code in notepad Apr 16, 2025 pm 07:39 PM

Although Notepad cannot run Java code directly, it can be achieved by using other tools: using the command line compiler (javac) to generate a bytecode file (filename.class). Use the Java interpreter (java) to interpret bytecode, execute the code, and output the result.

How to run sublime after writing the code How to run sublime after writing the code Apr 16, 2025 am 08:51 AM

There are six ways to run code in Sublime: through hotkeys, menus, build systems, command lines, set default build systems, and custom build commands, and run individual files/projects by right-clicking on projects/files. The build system availability depends on the installation of Sublime Text.

laravel installation code laravel installation code Apr 18, 2025 pm 12:30 PM

To install Laravel, follow these steps in sequence: Install Composer (for macOS/Linux and Windows) Install Laravel Installer Create a new project Start Service Access Application (URL: http://127.0.0.1:8000) Set up the database connection (if required)

How to use the Redis cache solution to efficiently realize the requirements of product ranking list? How to use the Redis cache solution to efficiently realize the requirements of product ranking list? Apr 19, 2025 pm 11:36 PM

How does the Redis caching solution realize the requirements of product ranking list? During the development process, we often need to deal with the requirements of rankings, such as displaying a...

git software installation git software installation Apr 17, 2025 am 11:57 AM

Installing Git software includes the following steps: Download the installation package and run the installation package to verify the installation configuration Git installation Git Bash (Windows only)

Laravel8 optimization points Laravel8 optimization points Apr 18, 2025 pm 12:24 PM

Laravel 8 provides the following options for performance optimization: Cache configuration: Use Redis to cache drivers, cache facades, cache views, and page snippets. Database optimization: establish indexing, use query scope, and use Eloquent relationships. JavaScript and CSS optimization: Use version control, merge and shrink assets, use CDN. Code optimization: Use Composer installation package, use Laravel helper functions, and follow PSR standards. Monitoring and analysis: Use Laravel Scout, use Telescope, monitor application metrics.

See all articles