Cài đặt redis trên local

Nguyễn Quang Minh

2 min read
14 views

Dưới đây là hướng dẫn cài đặt và cấu hình Redis trên Ubuntu (máy local của bạn) để chạy tốt cho NestJS, Bull, ioredis:


✅ Bước 1: Cài đặt Redis

sudo apt update
sudo apt install redis-server -y

✅ Bước 2: Kiểm tra Redis đang chạy

sudo systemctl status redis

Nếu chưa chạy:

sudo systemctl start redis

Bật Redis khởi động cùng hệ thống:

sudo systemctl enable redis

✅ Bước 3: Cấu hình Redis (tuỳ chỉnh nếu cần)

1. Mở file cấu hình:

sudo nano /etc/redis/redis.conf

2. Những mục quan trọng:

👉 Cho phép Redis lắng nghe từ máy khác (nếu cần)

bind 0.0.0.0

Mặc định là 127.0.0.1 → chỉ chạy được từ local.
Để an toàn nếu không dùng public: giữ nguyên 127.0.0.1.

👉 Cài password nếu muốn bảo vệ Redis:

requirepass your_secure_password

✅ Bước 4: Restart Redis để áp dụng thay đổi

sudo systemctl restart redis

✅ Bước 5: Kiểm tra hoạt động

redis-cli
# nếu có password thì:
redis-cli -a your_secure_password

Test:

127.0.0.1:6379> ping
PONG

✅ Bước 6: Cấu hình .env trong dự án NestJS

REDIS_HOST=127.0.0.1
REDIS_PORT=6379
REDIS_PASSWORD=your_secure_password # hoặc bỏ nếu không set
REDIS_DB=0

✅ Bước 7: Dùng với Bull hoặc ioredis

Ví dụ với BullModule:

BullModule.forRootAsync({
  imports: [ConfigModule],
  useFactory: (configService: ConfigService) => ({
    redis: {
      host: configService.get('REDIS_HOST'),
      port: +configService.get('REDIS_PORT'),
      password: configService.get('REDIS_PASSWORD'),
      db: +configService.get('REDIS_DB') || 0,
    },
  }),
  inject: [ConfigService],
}),

✅ Bonus: Redis CLI cơ bản

# Xem tất cả key
keys *

# Set key
set mykey hello

# Get key
get mykey

Nguyễn Quang Minh

Passionate developer and writer sharing insights about technology, programming, and digital innovation. Follow for more content about web development and tech trends.

1.2K followers

Stay Updated

Get the latest articles and insights delivered to your inbox.

Ôn tập là dễ

Ôn tập là dễ

Nền tảng thi trắc nghiệm số 1 Việt Nam giúp bạn tạo, quản lý và chia sẻ bộ đề một cách nhanh chóng và dễ dàng.

© 2025 Ôn tập là dễ. Tất cả các quyền được bảo lưu.

    🔥 Mua sắm ngay trên Shopee!
    Giúp mình duy trì trang web! 🎉