Page 1 of 1

Cài Đặt Redis Từ Source Trên Ubuntu 18.04

PostPosted:01 Jul 2018 08:40
by nguyenoanh
Bài viết này sẽ nói về cách cài đặt Redis từ source. Trước khi bắt đầu cài đặt, bạn phải login SSH vào server với sudo user. Các bước cài đặt:

1. Cài đặt các gói cần thiết:

Để cài đặt phiên mới nhất của Redis trên Ubuntu 18.04, chúng ta sẽ phải compile và cài đặt từ source. Trước khi compile, ta sẽ cài các gói cần thiết:
Code: Select all
$  sudo apt update
$ sudo apt install build-essential tcl
2. Download, Compile và Cài đặt Redis

Sau khi cài đặt gói phần mềm yêu cầu, ta có thể tải Redis và cài đặt nó.
Code: Select all
$ cd /tmp
curl -O http://download.redis.io/redis-stable.tar.gz
giải nén nó:
Code: Select all
tar xzvf redis-stable.tar.gz
cd tới thư mục vừa giải nén:
Code: Select all
cd redis-stable
compile nó:
Code: Select all
make
test trước khi cài đặt:
Code: Select all
make test
Và cài đặt:
Code: Select all
sudo make install
Sau khi cài đặt Redis xong, ta sẽ cấu hình nó. Trước tiên, ta phải tạo thư mục chứa config:
Code: Select all
sudo mkdir /etc/redis
Copy file config từ thư mục giải nén trước về đây
Code: Select all
sudo cp /tmp/redis-stable/redis.conf /etc/redis
dùng trình edit text và edit nó:
Code: Select all
sudo nano /etc/redis/redis.conf
Tìm đến đoạn supervised, edit tương tự:
Code: Select all
. . .

# 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

. . .
sau đó tìm đến config dir, ta cấu hình nơi Redis sử dụng để lưu cache của nó. Ta sử dụng giá trị: /var/lib/redis
Code: Select all
. . .

# The working directory.
#
# The DB will be written inside this directory, with the filename specified
# above using the 'dbfilename' configuration directive.
#
# The Append Only File will also be created inside this directory.
#
# Note that you must specify a directory here, not a file name.
dir /var/lib/redis

. . .
Save và close.
Ta sẽ tạo và thiết lập quyền ghi cho thư mục /var/lib/redis ở bước sau:

3 — Tạo a Redis systemd Unit File
Code: Select all
sudo nano /etc/systemd/system/redis.service
Nội dung file này:
Code: Select all
[Unit]
Description=Redis In-Memory Data Store
After=network.target

[Service]
User=redis
Group=redis
ExecStart=/usr/local/bin/redis-server /etc/redis/redis.conf
ExecStop=/usr/local/bin/redis-cli shutdown
Restart=always

[Install]
WantedBy=multi-user.target
Save và lưu lại.

4 — Tạo Redis User, Group, và Directories

Tạo user và group
Code: Select all
sudo adduser --system --group --no-create-home redis
tạo thư mục ta config trong bước 2:
Code: Select all
sudo mkdir -p  /var/lib/redis
thiết lập phân quyền cho redis có quyền đọc và ghi ở thư mục này
Code: Select all
sudo chown redis:redis /var/lib/redis
Chặn quyền truy cập của các uer khác vào đây:
Code: Select all
sudo chmod 770 /var/lib/redis
5. Start và Test Redis

Bật redis bằng lệnh:
Code: Select all
sudo systemctl start redis
Check redis:
Code: Select all
sudo systemctl status redis
Ta được kết quả tương tự:
Code: Select all
Output
● redis.service - Redis In-Memory Data Store
   Loaded: loaded (/etc/systemd/system/redis.service; disabled; vendor preset: enabled)
   Active: active (running) since Tue 2018-05-29 17:49:11 UTC; 4s ago
 Main PID: 12720 (redis-server)
    Tasks: 4 (limit: 4704)
   CGroup: /system.slice/redis.service
           └─12720 /usr/local/bin/redis-server 127.0.0.1:6379
. . .
Để test dịch vụ redis đã chạy chính xác, ta kết nối tới reddis server bằng lệnh:
Code: Select all
redis-cli
Check kết nối bằng lệnh ping
Code: Select all
127.0.0.1:6379 > ping
kết quả trả lại:
Code: Select all
PONG
Check key:
Code: Select all
127.0.0.1:6379> set test "It's working!"
Kết quả:
Code: Select all
Output
OK
Get key này:
Code: Select all
127.0.0.1:6379> get test 
Kết quả:
Code: Select all
Output
"It's working!"
Check xem Redis có thể lấy data sau khi stop và restart

Trước tiên khởi động lại redis:
Code: Select all
sudo systemctl restart redis
kết nối redis server
Code: Select all
redis-cli
Code: Select all
127.0.0.1:6379> get test 
Kết quả:
Code: Select all
Output
"It's working!"
edit:
Code: Select all
127.0.0.1:6379> exit 
Nếu kết quả bạn thực hiện giống như trên là cài đặt và cấu hình Redis cho Ubuntu 18.04 thành công.

Ta chạy lệnh dưới để redis tự khởi động mỗi khi reboot lại hệ thống:
Code: Select all
sudo systemctl enable redis
Nguồn: DO