Home  >  Article  >  Database  >  How to install and configure redis

How to install and configure redis

PHPz
PHPzforward
2023-05-31 16:40:281016browse

Simply speaking, redis is a database, but unlike traditional databases, redis data is stored in memory, so the read and write speed is very fast, so redis is widely used in caching.

How to install and configure redis

Installation

Download, unzip, and compile:

$ wget http://download.redis.io/releases/redis-4.0.10.tar.gz
$ tar xzf redis-4.0.10.tar.gz
$ mv redis-4.0.10 /usr/local/redis
$ cd /usr/local/redis
$ make

The binary file is in the src directory after the compilation is completed

$ ll -a src | grep redis
-rw-rw-r-- 1 root root 2.4K Jun 13 19:02 redisassert.h
-rwxr-xr-x 1 root root 2.6M Sep 14 12:05 redis-benchmark
-rw-rw-r-- 1 root root  29K Jun 13 19:02 redis-benchmark.c
-rw-r--r-- 1 root root 129K Sep 14 12:05 redis-benchmark.o
-rwxr-xr-x 1 root root 6.0M Sep 14 12:05 redis-check-aof
-rw-rw-r-- 1 root root 7.0K Jun 13 19:02 redis-check-aof.c
-rw-r--r-- 1 root root  38K Sep 14 12:05 redis-check-aof.o
-rwxr-xr-x 1 root root 6.0M Sep 14 12:05 redis-check-rdb
-rw-rw-r-- 1 root root  14K Jun 13 19:02 redis-check-rdb.c
-rw-r--r-- 1 root root  68K Sep 14 12:04 redis-check-rdb.o
-rwxr-xr-x 1 root root 2.8M Sep 14 12:05 redis-cli
-rw-rw-r-- 1 root root  99K Jun 13 19:02 redis-cli.c
-rw-r--r-- 1 root root 450K Sep 14 12:05 redis-cli.o
-rw-rw-r-- 1 root root  22K Jun 13 19:02 redismodule.h
-rwxr-xr-x 1 root root 6.0M Sep 14 12:05 redis-sentinel
-rwxr-xr-x 1 root root 6.0M Sep 14 12:05 redis-server
-rwxrwxr-x 1 root root  65K Jun 13 19:02 redis-trib

Where redis-server is the server program and redis-cli is the client program.

Create service

The binary file is in the src directory after compilation. Start the Redis service through the following command:

$ ln -s /usr/local/redis/src/redis-cli /usr/bin/redis
$ ln -s /usr/local/redis/src/redis-server /usr/bin/redisd

Output version

$ redis --version
redis-cli 4.0.10
$ redisd --version
Redis server v=4.0.10 sha=00000000:0 malloc=jemalloc-4.0.3 bits=64 build=e53a76b77e60d5b0

So far , indicating that your redis has been installed.

Configuration

Why should the configuration be listed with a separate title?

This is because Redis does not optimize security to the greatest extent, but does its best to optimize high performance and ease of use.

When authentication is not enabled, Redis opens port 6379 by default, which may lead to unauthorized access operations. So we need to do some security configuration here

New users

Never use a user with higher permissions to start Redis. If Redis is not secure, your high-privileged users may become an entry point for attacks, causing the server to be attacked.

So we need to create a redis account, start the redis service through this account, and configure the account to prohibit login.

$ adduser redis
$ vim /etc/passwd
redis:x:1001:1001:,,,:/home/redis:/usr/sbin/nologin
$ chown -R redis:redis /usr/local/redis

Modify the configuration file

Here you need to have basic operations on vim

$ vim redis.conf# 第92行左右   修改端口port 7379# 第171行左右  日志文件logfile /var/log/redis/redis.log# 第263行左右  设置快照文件目录,切勿设置成一个redis用户没有权限的目录dir /usr/local/redis/# 第500行左右  设置密码requirepass YOUR_PASSWORD
$ mkdir /var/log/redis
$ chown -R redis:redis /var/log/redis/

Configure a startup program

redis-server does not start as a background program by default , so we need to configure a startup program for it.

$ vim /usr/bin/goredisd
nohup /usr/bin/redisd /usr/local/redis/redis.conf >> /var/log/redis/goredisd.log 2>&1 &
$ goredisd
$ ps -axu | grep redis
redis 19498 0.0 0.1 145304 2580 pts/0 Sl 10:49 0:09 /usr/bin/redisd *:7379

The above is the detailed content of How to install and configure redis. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete