Redis擴(kuò)展
① 下載 redis 和 phpredis-develop
[root@anonymous package]# wget http://download.redis.io/releases/redis-4.0.10.tar.gz
[root@anonymous package]# wget https://codeload.github.com/phpredis/phpredis/zip/develop
- 同時解壓剛剛下載的兩個文件 一個是redis 另一個是php的redis擴(kuò)展
[root@anonymous package]# ll
total 68312
-rw-r--r-- 1 root root 48985783 Mar 4 2018 mysql-boost-5.7.22.tar.gz
-rw-r--r-- 1 root root 1032345 Jan 2 16:51 nginx-1.16.0.tar.gz
drwxrwxr-x 19 root root 4096 Mar 25 12:59 php-7.4.3
-rw-r--r-- 1 root root 16471635 Mar 2 16:19 php-7.4.3.tar.gz
-rw-r--r-- 1 root root 285861 Mar 11 13:54 phpredis-develop.zip
-rw-r--r-- 1 root root 1738465 Jun 14 2018 redis-4.0.10.tar.gz
drwxrwxr-x 13 root root 4096 Mar 25 15:52 swoole-src-4.4.16
-rw-r--r-- 1 root root 1415368 Mar 25 15:42 v4.4.16.tar.gz
[root@anonymous package]# tar -zxvf redis-4.0.10.tar.gz && unzip phpredis-develop.zip
-
解壓完畢后先進(jìn)入 redis-4.0.10 目錄進(jìn)行redis安裝
進(jìn)入解壓redis目錄.jpg
在安裝前提下必須有安裝以下依賴
[root@anonymous redis-4.0.10]# yum install make gcc cpp binutils glibc-kernheaders glibc-common glibc-devel -y
[root@anonymous redis-4.0.10]# rpm -qa make gcc cpp binutils glibc-kernheaders glibc-common glibc-devel
glibc-common-2.17-292.el7.x86_64
make-3.82-24.el7.x86_64
glibc-devel-2.17-292.el7.x86_64
gcc-4.8.5-39.el7.x86_64
binutils-2.27-41.base.el7_7.3.x86_64
cpp-4.8.5-39.el7.x86_64
[root@anonymous redis-4.0.10]#
② 進(jìn)入解壓后的目錄中輸入 make 進(jìn)行編譯
[root@anonymous redis-4.0.10]# make
- 編譯完成后就安裝 (/usr/study/ext/redis 為指定的安裝目錄)
[root@anonymous redis-4.0.10]# make PREFIX=/usr/study/ext/redis install
-
安裝完畢會在 /usr/study/ext/redis 目錄下生成bin目錄里面有 redis-server 就是我們要運(yùn)行的redis
redis安裝完畢.jpg - 這時回到解壓出來的redis源碼目錄中把 redis.conf 復(fù)制到 /usr/study/ext/redis/bin 目錄下
[root@anonymous redis-4.0.10]# ll
total 308
-rw-rw-r-- 1 root root 162174 Jun 13 2018 00-RELEASENOTES
-rw-rw-r-- 1 root root 53 Jun 13 2018 BUGS
-rw-rw-r-- 1 root root 1815 Jun 13 2018 CONTRIBUTING
-rw-rw-r-- 1 root root 1487 Jun 13 2018 COPYING
drwxrwxr-x 6 root root 4096 Mar 25 16:44 deps
-rw-rw-r-- 1 root root 11 Jun 13 2018 INSTALL
-rw-rw-r-- 1 root root 151 Jun 13 2018 Makefile
-rw-rw-r-- 1 root root 4223 Jun 13 2018 MANIFESTO
-rw-rw-r-- 1 root root 20543 Jun 13 2018 README.md
-rw-rw-r-- 1 root root 58766 Jun 13 2018 redis.conf
-rwxrwxr-x 1 root root 271 Jun 13 2018 runtest
-rwxrwxr-x 1 root root 280 Jun 13 2018 runtest-cluster
-rwxrwxr-x 1 root root 281 Jun 13 2018 runtest-sentinel
-rw-rw-r-- 1 root root 7606 Jun 13 2018 sentinel.conf
drwxrwxr-x 3 root root 4096 Mar 25 16:46 src
drwxrwxr-x 10 root root 4096 Jun 13 2018 tests
drwxrwxr-x 8 root root 4096 Jun 13 2018 utils
[root@anonymous redis-4.0.10]# cp redis.conf /usr/study/ext/redis/bin/
[root@anonymous redis-4.0.10]# cd /usr/study/ext/redis/bin/
[root@anonymous bin]# ll
total 21924
-rwxr-xr-x 1 root root 2452096 Mar 25 16:46 redis-benchmark
-rwxr-xr-x 1 root root 5769528 Mar 25 16:46 redis-check-aof
-rwxr-xr-x 1 root root 5769528 Mar 25 16:46 redis-check-rdb
-rwxr-xr-x 1 root root 2618128 Mar 25 16:46 redis-cli
-rw-r--r-- 1 root root 58766 Mar 25 16:50 redis.conf
lrwxrwxrwx 1 root root 12 Mar 25 16:46 redis-sentinel -> redis-server
-rwxr-xr-x 1 root root 5769528 Mar 25 16:46 redis-server
[root@anonymous bin]#
③ 編輯 vim redis.conf 文件
- 找到配置文件中的 daemonize yes no 改為 yes 即是用后端模式啟動
- 啟動 redis-server 如下圖所示表示redis已經(jīng)成功啟動了
[root@anonymous bin]# ./redis-server
18517:C 25 Mar 17:00:28.642 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
18517:C 25 Mar 17:00:28.642 # Redis version=4.0.10, bits=64, commit=00000000, modified=0, pid=18517, just started
18517:C 25 Mar 17:00:28.642 # Warning: no config file specified, using the default config. In order to specify a config file use ./redis-server /path/to/redis.conf
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 4.0.10 (00000000/0) 64 bit
.-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in standalone mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 6379
| `-._ `._ / _.-' | PID: 18517
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | http://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'
18517:M 25 Mar 17:00:28.644 # Server initialized
18517:M 25 Mar 17:00:28.644 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. 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.
18517:M 25 Mar 17:00:28.644 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
18517:M 25 Mar 17:00:28.644 * DB loaded from disk: 0.000 seconds
18517:M 25 Mar 17:00:28.644 * Ready to accept connections
④ 設(shè)置redis為開啟啟動
- 在etc目錄下創(chuàng)建redis目錄
- 將/usr/study/ext/redis/bin/redis.conf 復(fù)制到 /etc/redis目錄下 并命名為6379.conf
[root@anonymous bin]# mkdir /etc/redis
[root@anonymous bin]# pwd
/usr/study/ext/redis/bin
[root@anonymous bin]# cp redis.conf /etc/redis/6379.conf
- 回到redis解壓出來的源碼包目錄, 并進(jìn)入 utils 目錄中將 redis_init_script 復(fù)制到 /etc/init.d 目錄下
[root@anonymous utils]# pwd
/package/redis-4.0.10/utils
[root@anonymous utils]# ll
total 76
-rw-rw-r-- 1 root root 593 Jun 13 2018 build-static-symbols.tcl
-rw-rw-r-- 1 root root 1303 Jun 13 2018 cluster_fail_time.tcl
-rw-rw-r-- 1 root root 1070 Jun 13 2018 corrupt_rdb.c
drwxrwxr-x 2 root root 4096 Jun 13 2018 create-cluster
-rwxrwxr-x 1 root root 2137 Jun 13 2018 generate-command-help.rb
drwxrwxr-x 3 root root 4096 Jun 13 2018 graphs
drwxrwxr-x 2 root root 4096 Jun 13 2018 hashtable
drwxrwxr-x 2 root root 4096 Jun 13 2018 hyperloglog
-rwxrwxr-x 1 root root 9567 Jun 13 2018 install_server.sh
drwxrwxr-x 2 root root 4096 Jun 13 2018 lru
-rw-rw-r-- 1 root root 1277 Jun 13 2018 redis-copy.rb
-rwxrwxr-x 1 root root 1352 Jun 13 2018 redis_init_script
-rwxrwxr-x 1 root root 1047 Jun 13 2018 redis_init_script.tpl
-rw-rw-r-- 1 root root 1762 Jun 13 2018 redis-sha1.rb
drwxrwxr-x 2 root root 4096 Jun 13 2018 releasetools
-rwxrwxr-x 1 root root 3787 Jun 13 2018 speed-regression.tcl
-rwxrwxr-x 1 root root 693 Jun 13 2018 whatisdoing.sh
[root@anonymous utils]# cp redis_init_script /etc/init.d/redis
[root@anonymous utils]# ll /etc/init.d/redis
-rwxr-xr-x 1 root root 1352 Mar 26 15:56 /etc/init.d/redis
[root@anonymous utils]#
- 然后編輯 /etc/init.d/redis 加入如下這兩行
# chkconfig: 2345 90 10
# description: Redis is a persistent key-value tablebase
修改下面信息 ?? 具體看圖
EXEC=你的redis絕對路徑 例如: EXEC=/usr/study/ext/redis/bin/redis-server
CONF=你的配置文件路徑 例如: /etc/redis/6379.conf
redisconf.jpg
- 加入啟動項
[root@anonymous utils]# chkconfig redis on

⑤ 配置 redis-cli 環(huán)境變量
[root@anonymous bin]# pwd
/usr/study/ext/redis/bin
[root@anonymous bin]# ll
total 21928
-rw-r--r-- 1 root root 93 Mar 26 16:07 dump.rdb
-rwxr-xr-x 1 root root 2452096 Mar 25 16:46 redis-benchmark
-rwxr-xr-x 1 root root 5769528 Mar 25 16:46 redis-check-aof
-rwxr-xr-x 1 root root 5769528 Mar 25 16:46 redis-check-rdb
-rwxr-xr-x 1 root root 2618128 Mar 25 16:46 redis-cli
-rw-r--r-- 1 root root 58763 Mar 25 17:49 redis.conf
lrwxrwxrwx 1 root root 12 Mar 25 16:46 redis-sentinel -> redis-server
-rwxr-xr-x 1 root root 5769528 Mar 25 16:46 redis-server
[root@anonymous bin]# vim ~/.bash_profile

記得執(zhí)行如下 ??
[root@anonymous bin]# source ~/.bash_profile
[root@anonymous ~]# redis-cli
127.0.0.1:6379>
⑥ 在PHP擴(kuò)展redis
- 解壓 phpredis-develop.zip 并進(jìn)入 phpredis-develop 目錄后用phpize生成configure文件進(jìn)行安裝
[root@anonymous package]# ll | grep phpredis-develop
-rw-r--r-- 1 root root 285861 Mar 11 13:54 phpredis-develop.zip
[root@anonymous package]# unzip phpredis-develop.zip
[root@anonymous package]# cd phpredis-develop
[root@anonymous phpredis-develop]# ls
arrays.markdown crc16.h mkdeb-apache2.sh redis.c rpm
Changelog.md CREDITS mkdeb.sh redis_cluster.c sentinel_library.c
cluster_library.c debian package.xml redis_cluster.h sentinel_library.h
cluster_library.h debian.control php_redis.h redis_commands.c sentinel.markdown
cluster.markdown INSTALL.markdown README.markdown redis_commands.h serialize.list
common.h ISSUE_TEMPLATE.md redis_array.c redis_sentinel.c tests
config.m4 liblzf redis_array.h redis_sentinel.h
config.w32 library.c redis_array_impl.c redis_session.c
COPYING library.h redis_array_impl.h redis_session.h
[root@anonymous phpredis-develop]# phpize
Configuring for:
PHP Api Version: 20190902
Zend Module Api No: 20190902
Zend Extension Api No: 320190902
[root@anonymous phpredis-develop]# ls | grep configure
configure
configure.ac
[root@anonymous phpredis-develop]# ./configure --with-php-config=/usr/study/ext/php/bin/php-config
[root@anonymous phpredis-develop]# make && make install
可以看到 redis.so 已經(jīng)生成了
[root@anonymous no-debug-non-zts-20190902]# pwd
/usr/study/ext/php/lib/php/extensions/no-debug-non-zts-20190902
[root@anonymous no-debug-non-zts-20190902]# ll
total 21648
-rwxr-xr-x 1 root root 4676566 Mar 25 13:09 opcache.a
-rwxr-xr-x 1 root root 2490192 Mar 25 13:09 opcache.so
-rwxr-xr-x 1 root root 2621528 Mar 26 16:51 redis.so
-rwxr-xr-x 1 root root 12374008 Mar 25 15:52 swoole.so
[root@anonymous no-debug-non-zts-20190902]#
-
最后在php.ini配置文件加上 extension=redis 保存退出即可
extenRedis.jpg - 通過 php --ri redis 可以查看 redis 已經(jīng) enabled 說明已經(jīng)成功為PHP擴(kuò)展 redis 了
[root@anonymous ~]# php --ri redis
redis
Redis Support => enabled
Redis Version => 5.2.0
Redis Sentinel Version => 0.1
Available serializers => php, json
Directive => Local Value => Master Value
redis.arrays.algorithm => no value => no value
redis.arrays.auth => no value => no value
redis.arrays.autorehash => 0 => 0
redis.arrays.connecttimeout => 0 => 0
redis.arrays.distributor => no value => no value
redis.arrays.functions => no value => no value
redis.arrays.hosts => no value => no value
redis.arrays.index => 0 => 0
redis.arrays.lazyconnect => 0 => 0
redis.arrays.names => no value => no value
redis.arrays.pconnect => 0 => 0
redis.arrays.previous => no value => no value
redis.arrays.readtimeout => 0 => 0
redis.arrays.retryinterval => 0 => 0
redis.arrays.consistent => 0 => 0
redis.clusters.cache_slots => 0 => 0
redis.clusters.auth => no value => no value
redis.clusters.persistent => 0 => 0
redis.clusters.read_timeout => 0 => 0
redis.clusters.seeds => no value => no value
redis.clusters.timeout => 0 => 0
redis.pconnect.pooling_enabled => 1 => 1
redis.pconnect.connection_limit => 0 => 0
redis.session.locking_enabled => 0 => 0
redis.session.lock_expire => 0 => 0
redis.session.lock_retries => 10 => 10
redis.session.lock_wait_time => 2000 => 2000
[root@anonymous ~]#
- 我們可以寫幾行代碼測試下redis是否能用
<?php
try {
$redis = new Redis();
$redis->connect("127.0.0.1", 6379);
$redis->auth("Mo92563.");
$redis->set("name", "Jerry");
echo $redis->ping().PHP_EOL;
} catch (\Exception $e) {
echo $e->getMessage().PHP_EOL;
}
[root@anonymous www]# php test.php
1
[root@anonymous www]# redis-cli
127.0.0.1:6379> get name
"Jerry"
127.0.0.1:6379>



