三十九.NoSQL概述 部署Redis服務 、 部署LNMP+Redis
阿新 • • 發佈:2019-03-08
link eas osql etc restart save logs 加載模塊 操作 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
三十九.NoSQL概述 部署Redis服務 、 部署LNMP+Redis