微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

三十九.NoSQL概述 部署Redis服务 、 部署LNMP+Redis

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 redis-4.0.8]# cd utils/ utils]# ./install_server.sh (一路回车)   查看状态 utils]# /etc/init.d/redis_6379 status   查看监听的端口 utils]# netstat -antupl |grep :6379 tcp  0  0 127.0.0.1:6379   0.0.0.0:*   LISTEN   15203/redis-server utils]# ps  -C redis-server   PID TTY          TIME CMD 15203 ?        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 ]# 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  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  ]# rpm -ivh PHP-devel-5.4.16-42.el7.x86_64.rpm  ]# rpm -ivh PHP-fpm-5.4.16-42.el7.x86_64.rpm    ]# tar -zxvf PHP-redis-2.2.4.tar.gz  ]# cd PHPredis-2.2.4/   ]# PHPize  检测PHP环境  ]# ./configure --with-PHP-config=/usr/bin/PHP-config ]# make ]# make install Installing shared extensions:  /usr/lib64/PHP/modules/  提示模块安装目录 ]# ls /usr/lib64/PHP/modules/redis.so  查看模块文件   配置PHP加载模块 ]# vim /etc/PHP.ini  728 extension_dir = "/usr/lib64/PHP/modules/"  730 extension = "redis.so" :wq   ]# systemctl restart PHP-fpm ]# PHP -m | grep -i redis  验证模块是否加载成功 redis   验证配置 ]# cd nosql(自己打的包) ]# cp linkredis.PHP /usr/local/Nginx/html/ ]# vim /usr/local/Nginx/html/linkredis.PHP <?PHP $redis = new redis(); $redis->connect('192.168.4.50',6350); $redis->auth("123456"); $redis->set('tel,'13152098678); echo  $redis->get('school'); ?> :wq   真机检测: ] firefox http://192.168.4.50/test.php ] firefox http://192.168.4.50/linkredis.php

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐