Ubuntu - Linux

Hướng dẫn cài Redis trên Ubuntu 18.04, 20.04

Redis là gì?

Redis là gì? – Redis (Remote Dictionary Server) là một mã nguồn mở được dùng để lưu trữ dữ liệu có cấu trúc, có thể sử dụng như một database, bộ nhớ cache hay một message broker.

Yêu cầu

Để thực hiện việc cài đặt Redis trên Ubuntu thì bạn phải có một server Ubuntu với một tài khoản có quyền root, và một chút ít kiến thức về terminal.

Nếu đã có tất cả những điều trên thì triển thôi.

Cài đặt & Cấu hình Redis

Để cài đặt phiên bản Redis mới nhất, mình sẽ sử dụng apt để cài đặt nó từ kho lưu trữ chính thức của Ubuntu.

sudo apt update
sudo apt install redis-server

Thao tác này sẽ tải xuống và cài đặt Redis và các dependencies của nó. Sau đây, có một thay đổi cấu hình quan trọng cần thực hiện trong tệp cấu hình Redis, tệp này được tạo tự động trong quá trình cài đặt.

sudo nano /etc/redis/redis.conf

Tìm đến supervised bạn sẽ thấy giá trị mặc định của nó là no. Do bạn đang sử dụng Ubuntu nên cần đổi sang systemd

. . .

# If you run Redis from upstart or systemd, Redis can interact with your
# supervision tree. Options:
#   supervised no      - no supervision interaction
#   supervised upstart - signal upstart by putting Redis into SIGSTOP mode
#   supervised systemd - signal systemd by writing READY=1 to $NOTIFY_SOCKET
#   supervised auto    - detect upstart or systemd method based on
#                        UPSTART_JOB or NOTIFY_SOCKET environment variables
# Note: these supervision methods only signal "process is ready."
#       They do not enable continuous liveness pings back to your supervisor.
supervised systemd

. . .

Lưu cấu hình và khởi động lại Redis.

sudo systemctl restart redis.service

Kiểm tra lại hoạt động Redis

Chúng ta sẽ xem xét một số cách để kiểm tra xem Redis có hoạt động chính xác không trong bước này.

sudo systemctl status redis

Nếu Redis hoạt động sẽ hiển thị như sau

Output● redis-server.service - Advanced key-value store
   Loaded: loaded (/lib/systemd/system/redis-server.service; enabled; vendor preset: enabled)
   Active: active (running) since Wed 2018-06-27 18:48:52 UTC; 12s ago
     Docs: http://redis.io/documentation,
           man:redis-server(1)
  Process: 2421 ExecStop=/bin/kill -s TERM $MAINPID (code=exited, status=0/SUCCESS)
  Process: 2424 ExecStart=/usr/bin/redis-server /etc/redis/redis.conf (code=exited, status=0/SUCCESS)
 Main PID: 2445 (redis-server)
    Tasks: 4 (limit: 4704)
   CGroup: /system.slice/redis-server.service
           └─2445 /usr/bin/redis-server 127.0.0.1:6379
. . .

Bạn cũng có thể kiểm tra port Redis đã hoạt động chưa bằng cách

sudo netstat -lnp | grep redis

Cấu hình mật khẩu cho Redis

Mật khẩu là một phần quan trọng để bảo mật cho Redis và nó được tích hợp sẵn trong Redis, bạn chỉ cần set lại mật khẩu trong /etc/redis/redis.conf là được.

sudo nano /etc/redis/redis.conf

Tìm đến requirepass, xoá dấu # và sửa lại mật khẩu cần sử dụng, hiện tại là: foobared

# Require clients to issue AUTH <PASSWORD> before processing any other
# commands.  This might be useful in environments in which you do not trust
# others with access to the host running redis-server.
#
# This should stay commented out for backward compatibility and because most
# people do not need auth (e.g. they run their own servers).
#
# Warning: since Redis is pretty fast an outside user can try up to
# 150k passwords per second against a good box. This means that you should
# use a very strong password otherwise it will be very easy to break.
#
# requirepass foobared

Lưu cấu hình và khởi động lại Redis để áp dụng.

Cùng chủ đề

Trả lời

Back to top button