LNMP环境下安装Redis以及php的redis扩展
发布日期: 2022/06/14 阅读总量: 74

1.下载

sudo wget http://download.redis.io/releases/redis-6.2.4.tar.gz

2.解压

sudo tar zvxf redis-6.2.4.tar.gz

3.重命名

sudo mv redis-6.2.4/ redis

4.编译

cd redis 
sudo make 
sudo make test 
sudo make install

有可能会出现

You need tcl 8.5 or newer

那就去安装

sudo wget https://downloads.sourceforge.net/tcl/tcl8.6.8-src.tar.gz 
sudo tar xzvf tcl8.6.8-src.tar.gz -C /usr/local/   
cd  /usr/local/tcl8.6.1/unix/   
sudo ./configure   
sudo make   
sudo make install

成功了

16250:C 14 Jun 2022 09:03:59.540 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
16250:C 14 Jun 2022 09:03:59.540 # Redis version=6.2.4, bits=64, commit=00000000, modified=0, pid=16250, just started
16250:C 14 Jun 2022 09:03:59.540 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
16250:M 14 Jun 2022 09:03:59.540 * monotonic clock: POSIX clock_gettime
                _._                                                  
           _.-``__ ''-._                                             
      _.-``    `.  `_.  ''-._           Redis 6.2.4 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._                                  
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 16250
  `-._    `-._  `-./  _.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |           https://redis.io       
  `-._    `-._`-.__.-'_.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |                                  
  `-._    `-._`-.__.-'_.-'    _.-'                                   
      `-._    `-.__.-'    _.-'                                       
          `-._        _.-'                                           
              `-.__.-'                                               
16250:M 14 Jun 2022 09:03:59.541 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
16250:M 14 Jun 2022 09:03:59.541 # Server initialized
16250:M 14 Jun 2022 09:03:59.541 # 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.
16250:M 14 Jun 2022 09:03:59.541 * Ready to accept connections

安装PHP-redis扩展

1.下载

sudo wget https://pecl.php.net/get/redis-5.2.0.tgz

2.解压

sudo tar -zxvf redis-5.2.0.tgz

3.找到phpize文件,生成配置

$whereis phpize phpize: /usr/bin/phpize

在redis-4.0.2文件夹中执行

sudo /usr/bin/phpize 
drwxr-xr-x  2 root root     4096 5月  23 01:57 ./ drwxr-xr-x 10 root root     4096 5月  23 01:57 ../ -rwxr-xr-x  1 root root      837 5月  23 01:57 pear* -rwxr-xr-x  1 root root      858 5月  23 01:57 peardev* -rwxr-xr-x  1 root root      774 5月  23 01:57 pecl* lrwxrwxrwx  1 root root        9 5月  23 01:57 phar -> phar.phar* -rwxr-xr-x  1 root root    14833 5月  23 01:57 phar.phar* -rwxr-xr-x  1 root root 48618304 5月  23 01:57 php* -rwxr-xr-x  1 root root 48473272 5月  23 01:57 php-cgi* -rwxr-xr-x  1 root root     3325 5月  23 01:57 php-config* -rwxr-xr-x  1 root root     4534 5月  23 01:57 phpize*

4.配置

sudo ./configure  --with-php-config=/usr/local/php/bin/php-config 
sudo make
sudo make test

这个时候会报一些错误,打开php.ini

sudo vi /usr/local/php/etc/php.ini 
disable_functions = passthru,exec,system,chroot,chgrp,chown,shell_exec,proc_open,proc_get_status,popen,ini_alter,ini_restore,dl,openlog,syslog,readlink,symlink,popepassthru,stream_socket_server

去除shell_exec,proc_open

sudo make install

5.添加扩展

extension=redis.so

查看

php -m
jiqing@jiqing-pad:/$ php -m |grep redis redis

已经添加成功了!

进一步测试,

<?php     
    $redis = new Redis();     
    $redis->connect('127.0.0.1',6379);     
    $redis->set('test','hello world!');     
    echo $redis->get('test'); 
?>
hello world! Process finished with exit code 0

成功!