Skip to main content
  1. Posts/

Install Redis di Linux

·2 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
port 6379
tcp-backlog 65535
timeout 0
tcp-keepalive 30

maxmemory 2gb
maxmemory-policy allkeys-lru

daemonize yes
loglevel notice
logfile /var/log/redis/redis-server.log

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
dir /var/lib/redis

appendonly yes
appendfsync everysec
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

unixsocket /var/run/redis/redis.sock
unixsocketperm 700

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