33_部署Redis服务 、 部署LNMP+Redis

版本:4.0.8

1. 搭建Redis服务器
在主机 192.168.4.50 上安装并启用 redis 服务
设置变量test,值为123
查看变量test的值
 
1.1 搭建redis服务器
1.1.1 安装redis服务器
]# yum -y install gcc gcc-c++ make
]# tar -xvf redis-4.0.8.tar.gz
]# cd redis-4.0.8/
redis-4.0.8]# ls
00-RELEASENOTES  COPYING  Makefile   redis.conf       runtest-sentinel  tests
BUGS             deps     MANIFESTO  runtest          sentinel.conf     utils
CONTRIBUTING     INSTALL  README.md  runtest-cluster  src
redis-4.0.8]# make && make install  (如果有报错 执行 #make MALLOC=libc)
redis-4.0.8]# cd utils/
utils]# ./install_server.sh (一路回车)
 
查看状态
utils]# /etc/init.d/redis_6379 status
Redis is running (20516)
 
查看监听的端口
utils]# netstat -antupl |grep :6379
tcp  0  0 127.0.0.1:6379   0.0.0.0:*  LISTEN 20516/redis-server
utils]# ps  -C redis-server
  PID TTY     TIME CMD
20516 ?    00:00:00 redis-server
 
停止服务
utils]# /etc/init.d/redis_6379 stop
//再次查看,显示 没有那个文件或目录
utils]# /etc/init.d/redis_6379 status
cat: /var/run/redis_6379.pid: 没有那个文件或目录
Redis is running ()
 
连接redis
utils]# /etc/init.d/redis_6379 start
Starting Redis server...
]# redis-cli
127.0.0.1:6379> ping
PONG     //PONG说明服务正常
 
1.1.2 基本操作
设置变量test,值为123,查看变量test的值
常用指令操作:
set keyname keyvalue 存储
get keyname 获取
127.0.0.1:6379> set test 123
OK
127.0.0.1:6379> get test
"123"

del keyname 删除变量
127.0.0.1:6379> set k1 v1
OK
127.0.0.1:6379> get k1
"v1"
127.0.0.1:6379> del k1
(integer) 1

keys * 打印所有变量
127.0.0.1:6379> keys *
1) "test"
EXISTS keyname 测试是否存在
127.0.0.1:6379> exists k1
(integer) 0

type keyname 查看类型
127.0.0.1:6379> set k2 v1
OK
127.0.0.1:6379> type k2
string

move keyname dbname 移动变量
127.0.0.1:6379> move k2 1   //移动k2到1库
(integer) 1
select 数据库编号0-15 切换库
127.0.0.1:6379> select 1    //切换到1库
OK
127.0.0.1:6379[1]> keys *   //查看有k2
1) "k2"

expire keyname 10 设置有效时间
127.0.0.1:6379[1]> EXPIRE k2 10
(integer) 1

ttl keyname 查看生存时间
127.0.0.1:6379[1]> ttl k2

flushall 删除所有变量
127.0.0.1:6379[1]> FLUSHALL
OK
save 保存所有变量
127.0.0.1:6379[1]> save
OK

shutdown 关闭redis服务
127.0.0.1:6379[1]> SHUTDOWN
 
2.修改Redis服务运行参数
具体要求如下:
端口号 6350
IP地址 192.168.4.50
连接密码 123456
客户端连接Redis服务
 
2.1 修改redis运行参数
//可以先备份一份,防止修改错误没法还原
]# cp /etc/redis/6379.conf /root/6379.conf
]# /etc/init.d/redis_6379 stop
]# vim /etc/redis/6379.conf
...
bind  192.168.4.50               //设置服务使用的ip
port 6350                        //更改端口号
requirepass 123456               //设置密码
]# /etc/init.d/redis_6379 start
Starting Redis server...
]# ss -antul | grep 6350   //查看有端口6350
tcp  LISTEN  0   128  192.168.4.50:6350    *:*
 
由于修改了配置文件所以在连接的时候需要加上ip和端口
]# redis-cli -h 192.168.4.50 -p 6350
192.168.4.50:6350> ping
(error) NOAUTH Authentication required.
192.168.4.50:6350> auth 123456  //输入密码才能操作(因为之前设置过密码)
OK
192.168.4.50:6350> ping
PONG
还可以直接在命令行输入密码连接
]# redis-cli -h 192.168.4.50 -p 6350 -a 123456
192.168.4.50:6350> ping
PONG
 
停止服务
由于修改Redis服务运行参数,所以在停止服务的时候也不能用默认的方法停止
]# /etc/init.d/redis_6379 stop   //停止失败
]# redis-cli -h 192.168.4.50 -p 6350 -a 123456 shutdown
]# ss -antul | grep 6350   //查看没有端口
 
3.部署LNMP+Redis
3.1 部署nginx
]# systemctl stop httpd
]# tar -xf nginx-1.12.2.tar.gz
]# cd nginx-1.12.2/
]# yum -y install gcc pcre-devel openssl-devel zlib-devel
]# useradd nginx
]# ./configure --prefix=/usr/local/nginx
]# make && make install
]# ln -s /usr/local/nginx/sbin/nginx /sbin/
]# ls /usr/local/nginx/
conf  html  logs  sbin
 
修改配置文件并启动服务
]# vim /usr/local/nginx/conf/nginx.conf

location / {
 44         root   html;
 45         index index.php  index.html index.htm;
 46         }
...
 65         location ~ .php$ {
 66             root           html;
 67             fastcgi_pass   127.0.0.1:9000;
 68             fastcgi_index  index.php;
 69         #fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
 70             include        fastcgi.conf;
]# nginx
]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
]# netstat -utnlp  | grep  :80 查看端口
 
3.2 部署PHP
编写PHP文件
]#vim /usr/local/nginx/html/test.php
<?php
phpinfo();
?>
 
安装redis服务软件包并运行服务
]# /etc/init.d/redis_6379 start
]# netstat  -utnlp  | grep  :6350
 
配置php支持Redis 服务
安装连接redis服务 模块软件包
]# yum -y install php
]# yum -y install autoconf automake
]# yum -y install php-fpm php-devel

]# tar -zxvf php-redis-2.2.4.tar.gz
]# cd phpredis-2.2.4/
 
检测php环境
phpredis-2.2.4]# phpize
Configuring for:
PHP Api Version:         20100412
Zend Module Api No:      20100525
Zend Extension Api No:   220100525

2.2.4]# ./configure --with-php-config=/usr/bin/php-config
2.2.4]# make
2.2.4]# make install
Installing shared extensions:  /usr/lib64/php/modules/  提示模块安装目录
2.2.4]# ls /usr/lib64/php/modules/redis.so  查看模块文件
 
配置php加载模块
]# vim /etc/php.ini
 728 extension_dir = "/usr/lib64/php/modules/"
 730 extension = "redis.so"  (不是 extension_dir)
:wq
 
]# systemctl restart php-fpm
]# php -m | grep -i redis  验证模块是否加载成功
redis
 
验证配置
]# vim /usr/local/nginx/html/linkredis.php
<?php
$redis = new redis();
$redis->connect('192.168.4.50',6350);
$redis->auth("123456");
$redis->set('tel','13166667777');
echo  $redis->get('tel');
?>
:wq
 
真机检测:
] firefox http://192.168.4.50/test.php

] firefox http://192.168.4.50/linkredis.php
13166667777

数据库查看:
]# redis-cli -h 192.168.4.50 -p 6350
192.168.4.50:6350> AUTH 123456
OK
192.168.4.50:6350> keys *
1) "tel"
192.168.4.50:6350> get tel
"13166667777"
192.168.4.50:6350>

原文地址:https://www.cnblogs.com/luwei0915/p/12259215.html