|
|
在 CentOS 8.5 上从源代码安装 Redis 涉及多个步骤,包括安装依赖项、编译和安装 Redis。下面是一个详细的指南:: m, S' \% c0 C4 X {
7 {8 e; T9 T8 B3 p" f5 L
步骤 1: 安装依赖项
- l" s" _- W4 v! k V' ]首先,你需要安装编译 Redis 所需的依赖项。打开终端并运行以下命令:; d& v& V C* l2 |4 g
1 x* v5 L0 p/ _ y; v
[root@asset ~]# dnf install -y gcc make tcl 6 s9 z! d- u, ~; V2 u
% E9 r% f% o; j/ P1 r9 G0 Z2 T
# dnf install -y systemd-devel gcc make
% h, J8 ` Y$ R9 N4 T% w; \3 m) w( g. r$ `; A; {! @- {
步骤 2: 下载 Redis 源码* ?5 s- S, {( i0 O: L4 n
你可以从 Redis 的官方网站或 GitHub 仓库下载最新的源码。使用以下命令下载最新版本的源码:) A' s7 B1 [/ D' X* U
' b/ d3 ?5 h v& E2 p' B1 U
7 B* S7 y+ g7 J
wget https://download.redis.io/releases/redis-8.2.2.tar.gz" p. I* E) F' M3 V
或者使用 curl:. F a4 ?' Z3 U$ B9 v8 M
9 E+ Q* [! X* q& t/ }curl -O https://download.redis.io/releases/redis-8.2.2.tar.gz
+ t! s+ |- G T步骤 3: 解压源码包
7 ^1 v7 z. X/ ?2 i下载完成后,解压源码包:! D" y/ f; s9 U1 {& \+ i
1 Z7 { O: V: o( R7 k9 c
bash2 A" y% m% m+ Z1 N8 ?( Q0 {2 ?
# tar -zxvf redis-8.2.2.tar.gz* c9 Q* `- m& J$ X1 d
3 M% ~7 n6 c5 h, H! o# V9 b
cd redis-8.2.2/
# y( u! G% a/ r& \* f
8 z1 m8 b0 q3 @# m/ f' P6 z4 C6 G% N步骤 4: 编译 Redis- P3 G0 A& l& R
在源码目录中,运行以下命令来编译 Redis:' P/ H! `4 f; O& `
$ N- i; F! V1 e( N: d4 v3 p8 O- S[root@asset redis-8.2.2]# make PREFIX=/usr/local/redis install 2 ?5 @* {( o4 q# B4 L) R( R6 b
make USE_SYSTEMD=yes PREFIX=/usr/local/redis install/ o& `- s. E: c1 q) U) i* a
步骤 5: 安装 Redis
& D4 {: I* e1 {) n- V- j+ g! B+ { @编译完成后,使用以下命令安装 Redis:
8 s3 s& `7 S0 a6 t8 w# y8 ]9 x" W) [' ~
; {' C9 [% I9 g }2 z- o6 K; n Usudo make install 以上即可安装成功,这步可以不做。 只有上面只执行了make 需要做。! @6 E$ m. [' X5 M( w2 R1 H
步骤 6: 创建 Redis 用户(可选)
" v6 K: w) o' V) i为了安全起见,你可以创建一个专门用于运行 Redis 的用户。执行以下命令:5 t9 p% g7 B7 V
# `: z( i0 I2 a6 l5 j* t2 b ^% \
useradd -r -s /sbin/nologin -M redis/ a+ m" W( k3 @2 x4 G. c
步骤 7: 配置 Redis(可选)5 q9 d9 N: ~# O6 i8 p/ X- D, A
你可以通过编辑 /etc/redis/redis.conf 来配置 Redis。如果这个文件不存在,你可以从 /usr/local/bin 中复制一个示例配置文件:2 @$ U+ V9 R0 o' A/ _& F! L! I
/ g9 O& `% y5 y( K
做软连接,让系统可以调用redis服务:
r) k. G$ d0 c8 Q+ m0 J+ ?+ }2 M
$ u9 O- X W3 w! \+ j
[root@asset ~]# ln -s /usr/local/redis/bin/redis-server /usr/bin/3 Y+ Q3 F6 U3 Q: h, W
- j1 X9 U& a! H3 B; w# F
[root@asset ~]# ln -s /usr/local/redis/bin/redis-cli /usr/bin/
, W" n, i7 c4 G) x2 U7 Q' h# {: _6 ?7 F1 `, l( M3 _
验证命令:
% h" h [2 I/ s, R0 F[root@asset ~]# redis-server
% _; h! c: q0 L7 a1720147:C 20 Mar 2026 20:53:29.549 # WARNING Memory overcommit must be enabled! Without it, a background save or replication may fail under low memory condition. Being disabled, it can also cause failures without low memory condition, see https://github.com/jemalloc/jemalloc/issues/1328. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
0 E6 E' ^; j4 X1 r8 n/ X& W1720147:C 20 Mar 2026 20:53:29.549 * oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo4 R; s: Y- H; B: Q1 `2 e
1720147:C 20 Mar 2026 20:53:29.549 * Redis version=8.2.2, bits=64, commit=00000000, modified=0, pid=1720147, just started
: Z4 X2 M7 h% f# ^% a4 B9 n1720147:C 20 Mar 2026 20:53:29.549 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
! P! |3 i. w, z# {! V1 k1720147:M 20 Mar 2026 20:53:29.550 * Increased maximum number of open files to 10032 (it was originally set to 1024).$ D) }2 r& [# t" l
1720147:M 20 Mar 2026 20:53:29.550 * monotonic clock: POSIX clock_gettime
" }* x% F& @$ T% G _._
; _% q) g' Y: V8 d _.-``__ ''-._
! c; O1 ]- L; s4 y- F. ?+ m+ E" Q( i _.-`` `. `_. ''-._ Redis Open Source
2 F9 H6 W' ]+ ?; C .-`` .-```. ```\/ _.,_ ''-._ 8.2.2 (00000000/0) 64 bit
8 a# C6 a; t {. P7 q$ e ( ' , .-` | `, ) Running in standalone mode0 Z) P+ W2 w* H2 a: B/ T7 K* l; Q& \
|`-._`-...-` __...-.``-._|'` _.-'| Port: 6379
- Y; l( M3 b9 h! O) K | `-._ `._ / _.-' | PID: 1720147 H R h! k( Y2 c6 V+ V6 L) e
`-._ `-._ `-./ _.-' _.-'
) y2 w8 v, q9 `' f9 l |`-._`-._ `-.__.-' _.-'_.-'| . M- A# g* c9 X ?" r
| `-._`-._ _.-'_.-' | https://redis.io
+ v+ C9 r8 @1 r/ m" W `-._ `-._`-.__.-'_.-' _.-' 6 p6 w; i( S: l3 a: `! q5 t6 E
|`-._`-._ `-.__.-' _.-'_.-'| 4 v2 p" B, m' ~% w9 D; ]3 B
| `-._`-._ _.-'_.-' | " O5 A& F, i$ j! U; F
`-._ `-._`-.__.-'_.-' _.-' ; @" o' r6 [% l
`-._ `-.__.-' _.-'
. M4 Z3 I0 ^1 n8 c' W& g `-._ _.-' 0 y9 [) d( @' I `' D& U5 y1 Z
`-.__.-' / e \$ e5 L" H$ G0 a0 d
/ m3 V0 t/ [4 {2 s
1720147:M 20 Mar 2026 20:53:29.551 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128." i) y7 x0 O }; m3 G% c' ]4 l% G
1720147:M 20 Mar 2026 20:53:29.551 * Server initialized
) C' V+ P$ R b* T1720147:M 20 Mar 2026 20:53:29.551 * Ready to accept connections tcp# _( t. l" t4 T) r! @
^C1720147:signal-handler (1774011210) Received SIGINT scheduling shutdown...! q5 Y9 x1 ]) k! v: l
1720147:M 20 Mar 2026 20:53:30.962 * User requested shutdown..." ?4 n6 H6 m7 Q/ Y& {: k: d; {" k
1720147:M 20 Mar 2026 20:53:30.962 * Saving the final RDB snapshot before exiting. h) N7 L0 f! g+ v
1720147:M 20 Mar 2026 20:53:30.964 * DB saved on disk) b1 G1 s/ F2 } w" p, ?0 t4 z4 |
1720147:M 20 Mar 2026 20:53:30.964 # Redis is now ready to exit, bye bye...0 D% \. P$ t3 Y$ @5 a5 {: c
[root@asset ~]# redis-cli
! A" C3 l" P( @ [Could not connect to Redis at 127.0.0.1:6379: Connection refused5 I; T! f2 O% N. e
not connected> quit$ `0 Q9 _- x" s( J; d
; t( Y9 |# m! \: w0 d; ^) @: a: m
2 y( N4 C1 q% b8 L8 s
u, _) e& F" N8 J$ E7 [新建redis的目录:+ w* S( }: S. I9 z" v$ u, k( G, x9 M% f
0 i, Z3 L- h( ~3 P% z1 Y5 {mkdir /etc/redis// F- I+ ~; ^7 K3 r$ d" S
( E9 r6 T3 ], A/ U. A4 r8 C" Q4 }
cp /usr/src/redis-8.2.2/redis.conf /etc/redis/
- Y- L0 S6 c3 R ls -al /etc/redis/redis.conf 2 ^ W8 B) |! f3 I+ b
-rw-r--r-- 1 root root 111227 3月 20 20:56 /etc/redis/redis.conf
% l3 Q: D) u5 D6 V' r0 r( G0 W v" _% V8 E/ p
( }% I; S( `9 L: ~8 [! W) T' C# L) ]/ g. u+ Q
chown redis:redis /etc/redis/redis.conf2 ? O+ I0 X; k4 q8 d' t
+ g! H3 b8 o& Z1 }/ r( A步骤 8: 启动 Redis 服务(可选)) l w; L: b( O: r4 _
你可以使用以下命令手动启动 Redis 服务:/ X8 n& K' B- ~4 j
( O8 ]9 T9 c7 U/ H0 p redis-server /etc/redis/redis.conf: N- c( m, u, z
. W6 k' |# O( k. W或者,你也可以编写一个 systemd 服务单元来管理 Redis 服务。& r% ^1 L, O- L k
9 [( a7 c0 a2 g0 O: Q6 X创建一个新的 systemd 服务文件:/ y1 h$ ~# G: m6 ]! B
2 h: T- G* _9 ^" T, f* `3 [ S6 y2 e3 v
vim /etc/systemd/system/redis.service' m3 c0 x" x; e' F# O
在文件中添加以下内容:% n: T/ t0 X1 z$ I
5 Q' w* Q& J/ U$ e' h c: k6 L
8 `# E ~. Y( a0 n, ] V' X0 |) j7 n[Unit] a7 r8 I1 N6 ~
Description=Redis In-Memory Data Store' Y$ x. r5 G6 I/ J" O; l0 X
After=network.target" U1 J+ N/ K& {' B _
$ ]8 i% i. D1 |# U& F- \[Service]
- E! j6 c% V. v) I9 }- UUser=redis$ d; M/ G. F0 h6 @) S$ z- g, ]& E
Group=redis
/ p0 ? `+ ~, U+ c, S/ k# L& EExecStart=/usr/local/bin/redis-server /etc/redis/redis.conf7 B" [2 @ J/ v! \9 P
ExecStop=/bin/kill -s TERM $MAINPID8 H8 Q0 O! n& u! J
Restart=on-failure* W- J! I, ?* a
Type=notify0 W0 p" x; u7 M! D) p v$ Z$ E
PIDFile=/var/run/redis_6379.pid # 根据需要修改端口号和PID文件位置5 q7 E1 Y5 S" J7 L- U
PermissionsStartOnly=true: h+ v" z; N, O6 W Y
Restart=always
+ c: U1 O$ [$ ILimitNOFILE=65535 # 根据需要调整文件描述符限制,默认是10240,Redis可能需要更多。! O& K4 q9 X# W) S% w
LimitCORE=infinity # 让Redis可以产生core dump,以便于调试。根据需要调整。* l: M; Z' A7 f' s) T! W% k
TimeoutStopSec=0 # 不等待正常停止,直接杀掉进程。根据需要调整。
7 R! P$ s2 X7 MPrivateTmp=true # 使用临时目录。根据需要调整。7 U, I! S' N: S
PrivateDevices=yes # 不允许访问设备文件。根据需要调整。
0 B1 e5 g2 \) P# M: fProtectHome=true # 不允许访问home目录。根据需要调整。
. o+ n# V8 ]- g# g! C! EProtectSystem=full # 不允许访问系统目录。根据需要调整。
+ h" q& L. T- H! P. d3 WProtectControlGroups=true # 不允许访问控制组。根据需要调整。
, D+ P, ^7 ^4 z$ X x5 e9 lProtectKernelTunables=yes # 不允许修改内核参数。根据需要调整。+ N/ [# M. R1 Y u0 t" n- \' g
ProtectKernelModules=yes # 不允许装载内核模块。根据需要调整。
7 o0 q# W6 J) b3 A I9 ?ProtectKernelLogs=yes # 不允许访问内核日志。根据需要调整。# F. w" K9 x+ ~$ _0 P
NoNewPrivileges=yes # 不允许提升权限。根据需要调整。: z8 f ]. w" t8 c& L( l3 ~% a
ReadWriteDirectories=-/var/lib/redis -/var/log/redis # 设置可读写的目录。根据需要调整。7 K9 h" Z7 o/ T" j
ReadOnlyDirectories=-/etc/redis # 设置只读的目录。根据需要调整。 # 设置# b# y% ~! y, {7 {2 F, Y! t& y
$ b+ B" y, F# v g( u
( D# K. w6 C5 \7 B' H5 d8 p2 b3 d( N) ?
' x, o6 c4 ]& Y* `2 |7 l% l5 L最终的:
7 \* t$ i O. j6 ?8 r0 n4 b7 i9 Q: j2 {' q3 c; ]" q
/etc/systemd/system/redis.service
( j1 B5 ?3 W& c- X2 S! V, o( R) h- B3 S+ C$ [: V( h6 k4 u
cat /etc/systemd/system/redis.service
k: m/ f" V1 h$ cini
7 {5 @% Q0 [4 |, T, N+ s[Unit]. G/ w/ \8 @1 p0 G8 d% I9 m4 {
Description=Redis In-Memory Data Store
3 \7 g* P: T! E3 @After=network.target
& ~" [3 X4 j A( o. J
% C# A/ F. m! Z! X[Service]
9 A* K- S& A/ L7 dUser=redis [* [2 S) w3 j. i; {
Group=redis8 ^/ {3 D& S. ]9 W+ r6 A
ExecStart=/usr/bin/redis-server /etc/redis/redis.conf
- ~* L- {* B( U. U" W& e JExecStop=/bin/kill -s TERM $MAINPID% n( R8 a- h/ ~. A/ e; t
Restart=on-failure1 x5 y, M, e& U3 T! g _
Type=notify9 `4 `; ]4 Y3 {2 o Q
PIDFile=/var/run/redis/redis_6379.pid
8 R% i; }+ j" r1 H3 P#PermissionsStartOnly=true7 H, O) D' B5 m
Restart=always
+ ~: u2 \9 Y- iLimitNOFILE=65535
( ]: W- x1 H; S% {LimitCORE=infinity 9 j( _: G/ o: x, l
TimeoutStopSec=0
0 f1 J8 Z9 z1 p& `PrivateTmp=true 4 a- d. G% T+ n
PrivateDevices=yes / i" e/ N) ^8 d4 V1 G1 }
ProtectHome=true
2 l. o- ?6 L9 m: j& qProtectSystem=full ) _, s- K! J) P. S
ProtectControlGroups=true , w% A) h% c2 v- ?
ProtectKernelTunables=yes
: [! V6 \. j2 o! N/ x6 I6 YProtectKernelModules=yes + l1 L& _( a2 L2 D* Y/ w
NoNewPrivileges=yes
; E" m: f" J: u1 l5 y: K- GReadWritePaths=-/var/lib/redis -/var/log/redis 1 d" S' z5 p, h
ReadOnlyPaths=-/etc/redis
8 A2 C% {6 L# C* b5 H, H) z, r( |6 r, f2 p& |( U4 Z+ a$ p
5 u! r! u3 ~, [8 a8 e+ o
/ ^- I9 ?* K( i$ x
3 q5 M @! K+ h9 B u" N, L' I$ @1 D) ~ H
1 a) z8 ]9 t! U |
|