环境:
一台LNMP结构的云主机
一台搭载Redis的云主机
部署LNMP
把nginx网站的数据存储在50主机的内存里 4.1 配置网站服务器51 环境LNMP 56 yum -y install gcc zlib-devel openssl-devel 558 tar -zxvf nginx-1.12.2.tar.gz 560 cd nginx-1.12.2/ 562 ./configure 563 make 564 make install 566 ls /usr/local/nginx/
装包启服务
[root@host51 ~]# yum -y install php php-devel php-mysql php-fpm [root@host51 ~]# rpm -qa | grep -i mysql 安装的是 mysql-5.7.17 [root@host51 ~]# systemctl start php-fpm [root@host51 ~]# systemctl enable php-fpm Created symlink from /etc/systemd/system/multi-user.target.wants/php-fpm.service to /usr/lib/systemd/system/php-fpm.service. [root@host51 ~]# netstat -utnlp | grep 9000 tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 15745/php-fpm: mast
修改nginx配置
[root@host51 ~]# vim +65 /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; 71 }
检测nginx文件是否有误
[root@host51 ~]# /usr/local/nginx/sbin/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
检查是否有httpd服务占用80端口,如果有其他服务占用80端口。则停掉该服务
[root@host51 ~]# systemctl stop httpd 如果有httpd 的话
[root@host51 ~]# systemctl disable httpd
[root@host51 ~]# netstat -utnlp | grep 80
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 15834/nginx: master
不温和的方式,开机自启
#设置服务开机运行 [root@host51 ~]# echo "/usr/local/nginx/sbin/nginx" >> /etc/rc.local [root@host51 ~]# chmod +x /etc/rc.local
编写普通的php脚本测试,是否之支持php
[root@host51 ~]# vim /usr/local/nginx/html/test.php <?php echo "hello world"; ?>
检测
打开浏览器输入网址 http://192.168.4.51/test.php hello world
配置网站192.168.4.51可以把存储在50(redis)服务器的内存里
1:安装redis模块
]# tar -zxvf php-redis-2.2.4.tar.gz ]# cd phpredis-2.2.4 [root@host51 phpredis-2.2.4]# phpize #安装软件才有命令 yum -y install php-devel Configuring for: PHP Api Version: 20100412 Zend Module Api No: 20100525 Zend Extension Api No: 220100525 [root@host51 phpredis-2.2.4]#./configure --with-php-config=/usr/bin/php-config [root@host51 phpredis-2.2.4]# make && make install Installing shared extensions: /usr/lib64/php/modules/ [root@host51 phpredis-2.2.4]# ls /usr/lib64/php/modules/redis.s
/usr/lib64/php/modules/redis.so
2:调用redis模块
]# vim /etc/php.ini 728 extension_dir = "/usr/lib64/php/modules/" 730 extension = "redis.so" :wq ]# systemctl restart php-fpm 4.3.3 查看支持的模块 ]# php -m | grep -i "redis" redis 4.4 测试配置 1 编写php脚本 #连接服务 #存储数据 #查看数据
编写连接脚本,存储数据。查看数据
[root@host51 ~]# vim /usr/local/nginx/html/linkdb.php <?php $redis = new redis(); $redis->connect("192.168.4.50","6379"); //连接ip,端口 $redis->set("school","tarena"); //存数据
$redis->set("class","nsd2012"); echo $redis->get("school"); //取数据 echo $redis->get("class"); ?> [root@host51 ~]#
#在真机的命令行访问也可以
[root@teacher redis]# curl http://192.168.4.51/linkdb.php tarenansd2012
#在50本机命令行访问redis 可以查看到脚本里存储的变量名
[root@host50 ~]# redis-cli -h 192.168.4.50 192.168.4.50:6379> keys * 1) "class" 2) "school" 192.168.4.50:6379> mget class school 1) "nsd2012" 2) "tarena" 192.168.4.50:6379>
部署redis服务器
1 安装软件
[root@host50 ~]# netstat -utnlp | grep redis-server tcp 0 0 127.0.0.1:6379 0.0.0.0:* LISTEN 17529/redis-server
修改redis端口ip
[root@host50 ~]# vim +70 /etc/redis/6379.conf bind 192.168.4.50
重启服务
[root@host50 ~]# /etc/init.d/redis_6379 start
Starting Redis server...
[root@host50 ~]# netstat -utnlp | grep redis-server
tcp 0 0 192.168.4.50:6379 0.0.0.0:* LISTEN 17610/redis-server
查看被web网站置入的信息
[root@host50 ~]# redis-cli -h 192.168.4.50 -p 6350 192.168.4.50:6350> keys * (error) NOAUTH Authentication required. 192.168.4.50:6350> 192.168.4.50:6350> auth 123456 OK 192.168.4.50:6350> keys * 1) "school" 2) "class" 192.168.4.50:6350> exit
[root@host50 ~]# redis-cli -h 192.168.4.50 -p 6350 -a 123456
192.168.4.50:6350> keys *
1) "school"
2) "class"
192.168.4.50:6350>
小结:
在使用redis存储数据的时候
第一先停服务
第二安装支持redis插件的包
第三编写连接redis的脚本
第四测试数据是否能存到redis服务器