Skip to main content
  1. Posts/

Install Redis di Linux

·3 mins·
linux php linux
Table of Contents

Redis (Remote Dictionary Server) adalah penyimpanan data in-memory yang dapat digunakan sebagai database key–value, cache, dan message broker.

Redis juga sering digunakan untuk meningkatkan performa WordPress, misalnya dengan memanfaatkan plugin cache berbasis Redis.

1. Install Redis dan ekstensi PHP Redis
#

dnf -y install redis php72-php-pecl-redis5.x86_64
Sesuaikan versi PHP (php72) dengan versi PHP yang Anda gunakan di server.

2. Konfigurasi Redis
#

Edit file konfigurasi Redis:

nano /etc/redis.conf

Tambahkan atau sesuaikan konfigurasi berikut:

bind 127.0.0.1
protected-mode yes
port 0
tcp-backlog 511
unixsocket /var/run/redis/redis.sock
unixsocketperm 777
timeout 60
tcp-keepalive 300
daemonize no
supervised no
pidfile "/var/run/redis_0.pid"
loglevel notice
logfile ""
databases 16
always-show-logo no
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename "dump.rdb"
rdb-del-sync-files no
dir "/var/lib/redis"
replica-serve-stale-data yes
replica-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-diskless-load disabled
repl-disable-tcp-nodelay no
replica-priority 100
acllog-max-len 128
rename-command KEYS ""
maxclients 10000
maxmemory 0
maxmemory-policy noeviction
maxmemory-samples 5
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
replica-lazy-flush no
lazyfree-lazy-user-del no
io-threads 1
io-threads-do-reads no
oom-score-adj no
oom-score-adj-values 0 200 800
appendonly no
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
aof-use-rdb-preamble yes
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-size -2
list-compress-depth 0
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
stream-node-max-bytes 4096
stream-node-max-entries 100
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit replica 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
dynamic-hz yes
aof-rewrite-incremental-fsync yes
rdb-save-incremental-fsync yes
jemalloc-bg-thread yes

3. Enable dan Jalankan Redis
#

systemctl enable --now redis

Pastikan Redis berjalan:

systemctl status redis

4. Restart Apache dan PHP-FPM
#

systemctl restart httpd php72-php-fpm
Sesuaikan nama service PHP-FPM jika menggunakan versi PHP yang berbeda.

5. Membuat Beberapa Socket Redis (Multi-Instance)
#

Jika Anda menjalankan server seperti shared hosting, Redis dapat dijalankan dalam beberapa instance terpisah, masing-masing menggunakan Unix socket dan dijalankan oleh user tertentu agar aksesnya lebih aman dan terisolasi.

Template Konfigurasi Redis Instance
#

Buat template konfigurasi Redis:

nano /etc/redis/redis-template.conf

Isi dengan konfigurasi berikut:

bind 127.0.0.1
port 0

unixsocket /home/%i/redis/redis.sock
unixsocketperm 700

daemonize no
dir /home/%i/redis
dbfilename dump.rdb

appendonly yes
appendfsync everysec

maxmemory 512mb
maxmemory-policy allkeys-lru

loglevel notice
logfile /home/%i/redis/redis.log

always-show-logo no

save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes

auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb

lazyfree-lazy-eviction yes
lazyfree-lazy-expire yes
lazyfree-lazy-server-del yes

io-threads 4
io-threads-do-reads yes
%i akan otomatis diganti dengan nama instance (app1, app2, dll).

Buat systemd Template Service
#

Buat file:

nano /etc/systemd/system/redis@.service

Isi dengan:

[Unit]
Description=Redis Instance for %i
After=network.target

[Service]
User=%i
Group=%i
ExecStart=/usr/bin/redis-server /etc/redis/redis-%i.conf
Restart=always
LimitNOFILE=65535

[Install]
WantedBy=multi-user.target

Buat Konfigurasi per User
#

Contoh untuk user app1:

cp /etc/redis/redis-template.conf /etc/redis/redis-app1.conf

Buat direktori Redis:

mkdir -p /home/app1/redis
chown -R app1:app1 /home/app1/redis
chmod 700 /home/app1/redis

Ulangi langkah ini untuk user lain (app2, app3, dst).

Jalankan Redis Instance
#

Reload systemd:

systemctl daemon-reload

Jalankan Redis untuk app1:

systemctl enable --now redis@app1

Cek status:

systemctl status redis@app1

Related

Install Memcached di Linux
·1 min
linux php linux
Install APCu di Linux
·1 min
linux php linux
Access File Share with smbclient
·1 min
linux linux
Configure Scheduling Class and Priority Disk I/O in Linux
·2 mins
linux linux
Find Linux Package That Provides Specific File
·1 min
linux linux ubuntu centos
Mengatasi Masalah Interface Ethernet Tidak Terdeteksi
·1 min
linux linux