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:
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ó.
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
4 — Tạo Redis User, Group, và Directories
Tạo user và group
Bật redis bằng lệnh:
Trước tiên khởi động lại redis:
Ta chạy lệnh dưới để redis tự khởi động mỗi khi reboot lại hệ thống:
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
2. Download, Compile và Cài đặt Redis$ sudo apt update
$ sudo apt install build-essential tcl
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
giải nén nó:$ cd /tmp
curl -O http://download.redis.io/redis-stable.tar.gz
Code: Select all
cd tới thư mục vừa giải nén:tar xzvf redis-stable.tar.gz
Code: Select all
compile nó:cd redis-stable
Code: Select all
test trước khi cài đặt:make
Code: Select all
Và cài đặt:make test
Code: Select all
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:sudo make install
Code: Select all
Copy file config từ thư mục giải nén trước về đâysudo mkdir /etc/redis
Code: Select all
dùng trình edit text và edit nó:sudo cp /tmp/redis-stable/redis.conf /etc/redis
Code: Select all
Tìm đến đoạn supervised, edit tương tự:sudo nano /etc/redis/redis.conf
Code: Select all
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. . .
# 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
. . .
Code: Select all
Save và close.. . .
# 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
. . .
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
Nội dung file này:sudo nano /etc/systemd/system/redis.service
Code: Select all
Save và lưu lại.[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
4 — Tạo Redis User, Group, và Directories
Tạo user và group
Code: Select all
tạo thư mục ta config trong bước 2:sudo adduser --system --group --no-create-home redis
Code: Select all
thiết lập phân quyền cho redis có quyền đọc và ghi ở thư mục nàysudo mkdir -p /var/lib/redis
Code: Select all
Chặn quyền truy cập của các uer khác vào đây:sudo chown redis:redis /var/lib/redis
Code: Select all
5. Start và Test Redissudo chmod 770 /var/lib/redis
Bật redis bằng lệnh:
Code: Select all
Check redis:sudo systemctl start redis
Code: Select all
Ta được kết quả tương tự:sudo systemctl status redis
Code: Select all
Để test dịch vụ redis đã chạy chính xác, ta kết nối tới reddis server bằng lệnh: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
. . .
Code: Select all
Check kết nối bằng lệnh pingredis-cli
Code: Select all
kết quả trả lại:127.0.0.1:6379 > ping
Code: Select all
Check key:PONG
Code: Select all
Kết quả:127.0.0.1:6379> set test "It's working!"
Code: Select all
Get key này:Output
OK
Code: Select all
Kết quả:127.0.0.1:6379> get test
Code: Select all
Check xem Redis có thể lấy data sau khi stop và restart Output
"It's working!"
Trước tiên khởi động lại redis:
Code: Select all
kết nối redis serversudo systemctl restart redis
Code: Select all
redis-cli
Code: Select all
Kết quả:127.0.0.1:6379> get test
Code: Select all
edit:Output
"It's working!"
Code: Select all
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.127.0.0.1:6379> exit
Ta chạy lệnh dưới để redis tự khởi động mỗi khi reboot lại hệ thống:
Code: Select all
Nguồn: DOsudo systemctl enable redis
==> Đăng ký mã VPSSIM Business tại đây ! Thanks.