Linux服务器安装Redis并启用支持PHP7的扩展库

PHP7的Redis扩展库目前(2019年8月8日)已经更新到了5.0.5版本
2020年3月2日23:09:28已更新到redis-5.2.0.tgz

今天我们来学习如何在服务器部署Redis,并启用php支持。

redis.jpg

安装Redis

Redis的各个版本http://download.redis.io/releases/

#下载最新版
wget http://download.redis.io/releases/redis-stable.tar.gz
#解压
tar zxf redis-stable.tar.gz
#进入解压目录
cd redis-stable
#编译并安装
make && make install
# 复制配置文件
cp redis.conf /etc/redis.conf
# 将二进制文件拷贝至 /usr/local/bin
cd src
cp redis-server redis-cli redis-benchmark /usr/local/bin/

配置Redis

安装完成后需要我们进行一些配置工作。

#显示版本号
redis-server -v
#Redis server v=3.2.8 sha=00000000:0 malloc=jemalloc-4.0.3 bits=64 build=4b121b11919de795

# 建立数据库文件夹
mkdir /var/lib/redis_db/ -p

#编辑配置文件
vim /etc/redis.conf

# 修改以下变量
daemonize yes   # 守护方式运行
logfile "/var/log/redis.log"  # 日志文件
dir /var/lib/redis_db/      # 数据持久化文件夹

现在redis的基本运行已经配置完成

运行redis

redis-server /etc/redis.conf

# 检查运行日志,还记得我们刚才配置的日志文件吗?
tail -100 /var/log/redis.log

# 显示
                _._                                                  
           _.-``__ ''-._                                             
      _.-``    `.  `_.  ''-._           Redis 3.2.8 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._                                   
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 6744
  `-._    `-._  `-./  _.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |           http://redis.io        
  `-._    `-._`-.__.-'_.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |                                  
  `-._    `-._`-.__.-'_.-'    _.-'                                   
      `-._    `-.__.-'    _.-'                                       
          `-._        _.-'                                           
              `-.__.-'                                               

6744:M 18 Jun 16:19:24.210 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
6744:M 18 Jun 16:19:24.210 # Server started, Redis version 3.2.8
6744:M 18 Jun 16:19:24.210 # 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.
6744:M 18 Jun 16:19:24.210 # 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.
6744:M 18 Jun 16:19:24.210 * The server is now ready to accept connections on port 6379
6744:signal-handler (1560870654) Received SIGTERM scheduling shutdown...
6744:M 18 Jun 23:10:54.260 # User requested shutdown...
6744:M 18 Jun 23:10:54.274 * Saving the final RDB snapshot before exiting.
6744:M 18 Jun 23:10:54.296 * DB saved on disk
6744:M 18 Jun 23:10:54.296 * Removing the pid file.
6744:M 18 Jun 23:10:54.297 # Redis is now ready to exit, bye bye...

php7安装redis扩展库

下载php7的redis扩展库
项目地址:https://github.com/phpredis/phpredis.git
官网下载地址:https://pecl.php.net/package/redis
redis扩展库目前(2019年8月8日)最新5.0.2版本,下载地址:https://pecl.php.net/get/redis-5.0.2.tgz

#下载5.0.2版本
wget https://pecl.php.net/get/redis-5.0.2.tgz
#解压
tar -xzvf redis-5.0.2.tgz
#进入解压目录
cd redis-5.0.2
#通过phpize扩展脚本生成configure编译配置文件
[root@localhost /]# /usr/local/php/bin/phpize
Cannot find config.m4. 
Make sure that you run '/usr/local/php/bin/phpize' in the top level source directory of the module

#编译指定php7的php-config配置文件的路径
./configure --with-php-config=/usr/local/php/bin/php-config

#编译安装
make && make install

#编辑php.ini配置文件
vim /usr/local/php/etc/php.ini

对编译安装完后生成的扩展库路径和扩展库so文件增加到php.ini配置文件内,编译路径在安装的时候会自动生成,需要根据实际路径填写,包括上面的配置路径和phpize的实际路径。

开启PHP7的Redis扩展

extension_dir = "/usr/local/php/lib/php/extensions/no-debug-zts-20160303" 
extension=redis.so

重启Apache、Nginx或php-fpm!!!
添加php7的redis扩展库需要先安装好php再执行这里的操作。安装好后可以通过运行php脚本进行查看扩展库的支持是否添加成功。

<?php
   phpinfo();
?>

phpinfo页面
redis.png

给php7安装redis扩展库 https://zixuephp.net/article-269.html
Linux服务器配置Redis并启用PHP支持 https://blog.phpgao.com/redis_php.html
Linux服务器配置memcached并启用PHP支持 https://blog.phpgao.com/php-memcached-extension-installation.html

最后修改:2020 年 03 月 02 日 11 : 10 PM
如果觉得我的文章对你有用,请随意赞赏

发表评论