菜鸟笔记
提升您的技术认知

LNMP环境下安装Redis,以及php的redis扩展

1.下载

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

2.解压

sudo tar zvxf redis-4.0.9.tar.gz 

3.重命名

sudo mv redis-4.0.9/ 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   

成功了

redis-server
27904:C 23 May 02:23:52.443 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
27904:C 23 May 02:23:52.443 # Redis version=4.0.9, bits=64, commit=00000000, modified=0, pid=27904, just started
27904:C 23 May 02:23:52.443 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
27904:M 23 May 02:23:52.445 # You requested maxclients of 10000 requiring at least 10032 max file descriptors.
27904:M 23 May 02:23:52.445 # Server can't set maximum open files to 10032 because of OS error: Operation not permitted.
27904:M 23 May 02:23:52.445 # Current maximum open files is 4096. maxclients has been reduced to 4064 to compensate for low ulimit. If you need higher maxclients increase 'ulimit -n'.
                _._                                                  
           _.-``__ ''-._                                             
      _.-``    `.  `_.  ''-._           Redis 4.0.9 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._                                   
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 27904
  `-._    `-._  `-./  _.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |           http://redis.io        
  `-._    `-._`-.__.-'_.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |                                  
  `-._    `-._`-.__.-'_.-'    _.-'                                   
      `-._    `-.__.-'    _.-'                                       
          `-._        _.-'                                           
              `-.__.-'                          
jiqing@jiqing-pad:~$ redis-cli
127.0.0.1:6379> set name jack
OK
127.0.0.1:6379> get name
"jack"
127.0.0.1:6379> 

如果出现

Could not connect to Redis at 127.0.0.1:6379: Connection refused

在安装好redis扩展 尝试连接redis时,客户端打不开,原因是需要先开启服务端,这需要先配置——

A.找到redis.conf 并修改 daemonize no 为 daemonize yes ,这样就可以默认启动就后台运行

 vi /etc/redis.conf

B.开启客户端要确保服务端启动

redis-server /etc/redis.conf

 

安装PHP-redis扩展

1.下载

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

2.解压

sudo tar -zxvf redis-4.0.2.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

成功!

进一步查看服务开启情况!

jiqing@jiqing-pad:/home/wwwroot/default$ sudo netstat -anp|grep :6379
tcp        0      0 0.0.0.0:6379            0.0.0.0:*               LISTEN      27904/redis-server  
tcp6       0      0 :::6379                 :::*                    LISTEN      27904/redis-server  
jiqing@jiqing-pad:/home/wwwroot/default$ sudo ps -ef |grep redis
jiqing    1576 32272  0 03:05 pts/1    00:00:00 grep --color=auto redis
jiqing   27904  1772  0 02:23 ?        00:00:02 redis-server *:6379

,

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

2.解压

sudo tar zvxf redis-4.0.9.tar.gz 

3.重命名

sudo mv redis-4.0.9/ 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   

成功了

redis-server
27904:C 23 May 02:23:52.443 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
27904:C 23 May 02:23:52.443 # Redis version=4.0.9, bits=64, commit=00000000, modified=0, pid=27904, just started
27904:C 23 May 02:23:52.443 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
27904:M 23 May 02:23:52.445 # You requested maxclients of 10000 requiring at least 10032 max file descriptors.
27904:M 23 May 02:23:52.445 # Server can't set maximum open files to 10032 because of OS error: Operation not permitted.
27904:M 23 May 02:23:52.445 # Current maximum open files is 4096. maxclients has been reduced to 4064 to compensate for low ulimit. If you need higher maxclients increase 'ulimit -n'.
                _._                                                  
           _.-``__ ''-._                                             
      _.-``    `.  `_.  ''-._           Redis 4.0.9 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._                                   
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 27904
  `-._    `-._  `-./  _.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |           http://redis.io        
  `-._    `-._`-.__.-'_.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |                                  
  `-._    `-._`-.__.-'_.-'    _.-'                                   
      `-._    `-.__.-'    _.-'                                       
          `-._        _.-'                                           
              `-.__.-'                          
jiqing@jiqing-pad:~$ redis-cli
127.0.0.1:6379> set name jack
OK
127.0.0.1:6379> get name
"jack"
127.0.0.1:6379> 

如果出现

Could not connect to Redis at 127.0.0.1:6379: Connection refused

在安装好redis扩展 尝试连接redis时,客户端打不开,原因是需要先开启服务端,这需要先配置——

A.找到redis.conf 并修改 daemonize no 为 daemonize yes ,这样就可以默认启动就后台运行

 vi /etc/redis.conf

B.开启客户端要确保服务端启动

redis-server /etc/redis.conf

 

安装PHP-redis扩展

1.下载

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

2.解压

sudo tar -zxvf redis-4.0.2.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

成功!

进一步查看服务开启情况!

jiqing@jiqing-pad:/home/wwwroot/default$ sudo netstat -anp|grep :6379
tcp        0      0 0.0.0.0:6379            0.0.0.0:*               LISTEN      27904/redis-server  
tcp6       0      0 :::6379                 :::*                    LISTEN      27904/redis-server  
jiqing@jiqing-pad:/home/wwwroot/default$ sudo ps -ef |grep redis
jiqing    1576 32272  0 03:05 pts/1    00:00:00 grep --color=auto redis
jiqing   27904  1772  0 02:23 ?        00:00:02 redis-server *:6379