如何在 Ubuntu Linux 上配置和安裝 Redis
在本文中,我們將學習如何配置 Redis。Redis 是一種記憶體中的鍵值儲存,因其靈活性和效能而廣受歡迎,並支援多種語言。我們將在 Ubuntu Linux 伺服器上配置它。為了演示,我們需要一個非 root 使用者,併為該使用者設定 Sudo 許可權以執行操作。
安裝構建和測試依賴項
為了獲取 Redis 的最新版本,我們將獲取最新的原始碼,並編譯和安裝軟體。為此,我們需要安裝軟體編譯的依賴項。
我們還需要從 Ubuntu 儲存庫安裝 build-essential 元軟體包,並下載用於測試二進位制檔案的“tcl”軟體包。
# sudo apt-get update # sudo apt-get install build-essentials tcl
Redis 下載、編譯和安裝
下載原始碼並解壓。在臨時資料夾中建立一個資料夾
# mkdir /tmp/redis # cd /tmp/redis
下載 Redis 的最新穩定版本,我們可以使用以下命令獲取
# curl -O http://download.redis.io/redis-stable.tar.gz # tar xzvf redis-stable.tar.gz # cd redis-stable
構建和安裝 Redis
我們將使用以下命令編譯和安裝 Redis 二進位制檔案
# make
原始碼編譯完成後,我們將獲得二進位制檔案,我們執行以下命令來測試套件
# make test
我們可以使用以下命令在系統上安裝所有二進位制檔案。
# make install
在 Ubuntu 上配置 Redis
由於我們剛剛安裝了 Redis,因此我們可以開始配置 Redis。我們需要建立目錄 /etc/redis
# mkdir /etc/redis
複製包含在 Redis 原始碼歸檔檔案中的 Redis 配置檔案
# cp /tmp/redis/redis-stable/redis.conf /etc/redis
使用編輯器開啟配置檔案
# vi /etc/redis/redis.conf
在配置檔案中,我們將找到一個名為“supervised”的指令,該指令設定為 no,因為我們使用 systemd 初始化系統,所以我們可以將其更改為 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
接下來,找到 dir 指令並將 dire 指令更改為 /var/lib/redis,此選項用於指定 Redis 將用於轉儲持久資料目錄的目錄。此位置具有寫許可權,普通使用者無法檢視。
# 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 the changes and close the file
為 Redis 建立 systemd 單元檔案
我們可以建立一個 **systemd** 檔案,以便 **init** 系統可以管理程序。
# touch /etc/systemd/system/redis.service # vi /etc/systemd/system/redis.service
我們從 [Unit] 部分開始,新增描述並定義一個需求,即我們需要網路能夠最先可用以啟動此服務。
[Unit] Description=Redis 記憶體資料儲存 After=network.target
在 [service] 部分,我們將指定服務的行為。出於安全原因,我們不會以 root 使用者身份執行服務。我們將專門分配一個使用者和組,他們將簡單地呼叫 Redis。
要啟動服務,我們需要呼叫指向我們配置的 redis-server 二進位制檔案。要停止服務,我們將使用 Redis shutdown 命令,該命令將使用 redis-cli 二進位制檔案執行。
[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] section, we define the systemd to target the service to attach to enable (configured to start at boot time). [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
建立 Redis 使用者、組和目錄
# add users --system --group --no-create-home redis # mkdir /var/lib/redis # chown redis:redis /var/lib/redis # chmod 770 /var/lib/redis
啟動和測試 Redis
我們已準備好啟動 Redis 伺服器
# systemctl start redis
要檢查服務是否無錯誤執行,我們可以執行以下命令 -
# systemctl status redis redis.service - Redis Server Loaded: loaded (/etc/systemd/system/redis.service; enabled; vendor preset: enabled) Active: active (running) since Wed 2016-05-13 11:42:00 EDT; 5min 03s ago Process: 7127 ExecStop=/usr/local/bin/redis-cli shutdown (code=exited, status=0/SUCCESS) Main PID: 7143 (redis-server) Tasks: 6 (limit: 512) Memory: 10864.0K CPU: 79ms CGroup: /system.slice/redis.service └─7143 /usr/local/bin/redis-server 127.0.0.1:6379
啟用 Redis 在啟動時啟動
# sudo systemctl enable redis
我們應該已在我們的環境中安裝並配置了一個 Redis 例項,以便我們可以將其用於記憶體資料結構儲存,以及用作資料庫、快取和訊息代理。
資料結構
網路
關係型資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP