본문 바로가기

CentOS

Redis Server 설치하기

Redis를 설치해보자.

최신 버전은 Redis 공식 홈페이지에서 찾아볼 수 있다.

https://redis.io/download

 

Redis

*Download Redis uses a standard practice for its versioning: major.minor.patchlevel. An even minor marks a stable release, like 1.2, 2.0, 2.2, 2.4, 2.6, 2.8. Odd minors are used for unstable releases, for example 2.9.x releases are the unstable versions of

redis.io

yum에는 5.0.3 버전이 올라와있다. 나름 최신 버전의 패키지를 배포하는 epel, remi 저장소에도 5.0.3이 최신 버전이므로, 5.0.7 버전의 소스를 받아서 설치하자.

# cd /home/build
# curl -o redis-5.0.7.tar.gz http://download.redis.io/releases/redis-5.0.7.tar.gz
# tar -zxvf redis-5.0.7.tar.gz
# cd redis-5.0.7

 

간단하게 make 명령어로 build 하면 된다.

# make
...
Hint: It's a good idea to run 'make test' ;)
...

설치가 끝났다면 요구사항대로 make test를 실행해보자.

# make test

 

별 이상 없이 테스트를 끝마쳤다면 redis 폴더를 /usr/local로 옮긴다.

# cd ..
# mv redis-5.0.7 /usr/local/redis

 

redis-server 서비스 등록 겸, 설정 파일 생성 겸 서버 설치 스크립트를 실행한다.

# /usr/local/redis/utils/install_server.sh
Please select the redis port for this instance: [6379]

6379가 redis-server의 Default Port이다. 나중에 한번 더 설명하겠지만 Redis는 외부에서 공격하기 쉬운 DB이므로 포트번호도 일단 바꾸자. 기본값 그대로 사용하려면 Enter만 쳐도 된다.

Please select the redis config file name [/etc/redis/6379.conf]

config 파일 경로를 설정한다. 취향껏 설정하자.

Please select the redis log file name [/var/log/redis_6379.log]

Redis-server 로그 파일 경로이다. 역시나 취향껏 설정하자.

Please select the data directory for this instance [/var/lib/redis/6379]

Redis-server의 데이터 덤프 파일 경로이다. 취존

Please select the redis executable path [/usr/local/bin/redis-server]

Redis-server의 실행 경로이다. 알아서 찾아서 설정된 값이므로 손대지 않는다.

Selected config: 
Port           : XXXX
Config file    : /etc/redis.conf
Log file       : /var/log/redis.log
Data dir       : /var/lib/redis
Executable     : /usr/local/bin/redis-server 
Cli Executable : /usr/local/bin/redis-cli 
Is this ok? Then press ENTER to go on or Ctrl-C to abort.

지금까지 설정한 값이 프린트되었다. 제대로 설정했다면 Enter를, 아니라면 Ctrl-C를 눌러 취소하자.

 

이제 추가로 기본적인 설정을 몇 가지 한다.

# vi /etc/redis.conf
...
#### NETWORK #####
...
# Examples:
#
# bind 192.168.1.100 10.0.0.1
# bind 127.0.0.1 ::1
...
bind 127.0.0.1

redis에 바인드 해줄 IP이다. Examples를 참고해서 적당하게 설정해준다.

 

protected-mode yes

보호 모드이다. 만약 bind ip를 명시하지 않고, 패스워드를 설정하지 않았다면 로컬 접속을 제외한 나머지 접속에 대해서 아래의 에러를 띄워버린다.

(error) DENIED Redis is running in protected mode because protected mode is enabled, no bind address was specified, no authentication password is requested to clients. In this mode connections are only accepted from the loopback interface. If you want to connect from external computers to Redis you may adopt one of the following solutions: 1) Just disable protected mode sending the command 'CONFIG SET protected-mode no' from the loopback interface by connecting to Redis from the same host the server is running, however MAKE SURE Redis is not publicly accessible from internet if you do so. Use CONFIG REWRITE to make this change permanent. 2) Alternatively you can just disable the protected mode by editing the Redis configuration file, and setting the protected mode option to 'no', and then restarting the server. 3) If you started the server manually just for testing, restart it with the '--protected-mode no' option. 4) Setup a bind address or an authentication password. NOTE: You only need to do one of the above things in order for the server to start accepting connections from the outside.

만약 bind ip를 0.0.0.0으로 설정했다면 protected-mode가 동작하지 않으니 주의하자.

 

timeout 0

대기 상태의 클라이언트의 연결을 끊어버린다. 0은 그대로 유지한다. 만약 프로그램에서 redis를 pool을 사용해 연결 중이라면 타임아웃 설정에 유의하자.

 

#### GENERAL ####

# By default Redis does not run as a daemon. Use 'yes' if you need it. 
# Note that Redis will write a pid file in /var/run/redis.pid when daemonized. 
daemonize yes

redis를 데몬으로 띄울지 말지 결정한다. 서비스로 띄울 예정이기 때문에 yes로 바꾼다.

 

pidfile /var/run/redis.pid
...
loglevel notice

취향껏 설정하자.

 

#### SNAPSHOTTING  ####

# Save the DB on disk: 
...
#   In the example below the behaviour will be to save: 
#   after 900 sec (15 min) if at least 1 key changed 
#   after 300 sec (5 min) if at least 10 keys changed 
#   after 60 sec if at least 10000 keys changed 
...
save 900 1 
save 300 10 
save 60 10000
...
dbfilename dump.rdb
...
dir /var/lib/redis/

redis가 RAM을 사용하는 DB이기는 하지만, 휘발성 메모리이므로 주기적으로 DISK에 데이터를 덤프 한다. 그래서 서버를 재부팅하거나, redis server를 재시작하더라도 DISK에 덤프 해둔 데이터를 읽어오기 때문에 redis에 저장해둔 데이터가 복구된다. 그런데 데이터 갱신이 자주 일어나는 상황에서 데이터 사이즈가 무척 커지면 DISK에 부하가 커진다. 굳이 덤프를 해야 할 상황이 아니라면 해당 옵션을 꺼도 상관없다.

 

#### SECURITY ####
...
requirepass XXXX

설명에도 나오지만, REDIS는 처리속도가 매우 빠른 DB이므로 패스워드 무차별 대입 공격(Brute Force Attack)에 취약하다. 보안을 위해선 매우 강력한 패스워드를 써야 한단다.

 

나머지 튜닝이나 리플리케이션이나 클러스터링, 기타 등등에 대해선 알맞게 수정한다.

 

이제 저장하고 나와 방화벽을 설정한다.

# firewall-cmd --permanent --add-service redis
# cp /usr/lib/firewalld/services/redis.xml /etc/firewalld/services/redis.xml
# vi /etc/firewalld/services/redis.xml

port를 아까 /etc/redis.conf에 설정한 번호로 바꾸고 firewalld를 재시작한다.

# systemctl restart firewalld

 

이제 마지막 단계. install_server 스크립트를 사용하면 서비스 이름이 이상해진다. 서비스 이름을 redis로 변경하자.

# cd /etc/init.d
# mv redis_XXXX redis
# systemctl daemon-reload

 

드디어 끝났다. redis service를 실행해보자

# systemctl start redis

 

server가 제대로 실행되었는지 체크해보자. Redis Client는 redis-cli 명령어로 사용한다.

redis-cli -h 127.0.0.1 -p XXXX ping
PONG

귀여운 PONG 메시지가 돌아왔다면 모든 과정이 끝난 것이다.

 

'CentOS' 카테고리의 다른 글

RRDtool 설치하기  (0) 2020.03.01
MariaDB 튜닝하기  (0) 2020.03.01
MariaDB Trouble Shooting  (0) 2020.02.29
MariaDB 설치하기  (0) 2020.02.28
SNMP 설치하기  (0) 2020.02.28