在主机192.168.4.57部署LNMP 环境
配置PHP支持redis
编写网站脚本,把数据存储到redis服务器192.168.4.50
一:在主机192.168.4.57部署LNMP 环境
1)安装源码nginx软件及php-fpm
[root@lnmp~] # yum -y install gcc pcre-devel zlib-devel //安装依赖
[root@lnmp~] # tar -zxvf nginx-1.12.2.tar.gz //解压
[root@lnmp~] # cd nginx-1.12.2 //进源码目录
[root@lnmp~] # ./configure //配置
[root@lnmp~] # make && make install //编译安装
[root@lnmp~] # ls /usr/local //查看安装目录
bin etc games include lib lib64 libexec nginx sbin share src
[root@lnmp~] #ls /usr/local/nginx //查看目录列表
conf html logs sbin
[root@lnmp~] #yum -y install php-fpm //安装php-fpm
2)修改配置nginx.conf
[root@lnmp~] # vim +65 /usr/local/nginx/conf/nginx.conf
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi.conf;
}
[root@lnmp~] # /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
3)启动php-fpm服务和启动nginx服务
[root@lnmp~] # systemctl start php-fpm //启动服务
[root@lnmp~] # netstat -utnlp | grep :9000 //查看端口
[root@lnmp~] # /usr/local/nginx/sbin/nginx
[root@lnmp~] # netstat -utnlp | grep :80
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 23505/nginx: master
4)测试配置
[root@lnmp~] # vim /usr/local/nginx/html/test.php //编写php文件
<?php
echo "hello world!!!";
?>
[root@lnmp~] # curl http://localhost/test.php //访问nginx服务
二:配置PHP支持redis
1)安装php扩展
[root@lnmp~] # yum -y install php php-devel automake autoconf //安装依赖
[root@lnmp~] # tar -zxf php-redis-2.2.4.tar.gz //安装扩展包
[root@lnmp~] # cd phpredis-2.2.4/
[root@lnmp~] # phpize //生成配置文件php-config及 configure命令
Configuring for:
PHP Api Version: 20100412
Zend Module Api No: 20100525
Zend Extension Api No: 220100525
[root@lnmp~] # ./configure --with-php-config=/usr/bin/php-config //配置
[root@lnmp~] # make //编译
[root@lnmp~] # make install //安装
2)修改php.ini文件
[root@lnmp~] #vim /etc/php.ini
728 extension_dir = "/usr/lib64/php/modules/" //模块文件目录
730 extension = "redis.so" //模块文件名
[root@lnmp~] # systemctl restart php-fpm //重启php-fpm服务
[root@lnmp~] # php -m | grep -i redis //查看已加载的模块
redis
三:redis服务器配置
对Redis服务器192.168.4.50做如下配置: 端口号 6350 连接密码 123456
1)修改配置文件
[root@redis utils]# /etc/init.d/redis_6379 stop
[root@redis utils]# vim /etc/redis/6379.conf
...
bind 192.168.4.50 //设置服务使用的ip
port 6350 //更改端口号
requirepass 123456 //设置密码
2)修改启动脚本
[root@redis ~]# vim +43 /etc/init.d/redis_6379
$CLIEXEC -h 192.168.4.50 -p 6350 -a 123456 shutdown
3)启动服务
[root@redis ~]# /etc/init.d/redis_6379 start
Starting Redis server...
[root@redis ~]#
[root@redis ~]# netstat -utnlp | grep redis-server
tcp 0 0 192.168.4.50:6350 0.0.0.0:* LISTEN 11523/redis-server
4)测试配置,访问服务存取数据
[root@redis ~]# redis-cli -h 192.168.4.50 -p 6350 -a 123456 //访问服务
192.168.4.50:6350> ping
PONG
192.168.4.50:6350> keys *
(empty list or set)
192.168.4.50:6350>
192.168.4.50:6350> set x 99
OK
192.168.4.50:6350>
192.168.4.50:6350> exit
四:测试配置:编写网站脚本,把数据存储到redis服务器192.168.4.50
1)查看192.168.4.50主机的redis服务是否运行
[root@redis~] # netstat -utnlp | grep redis-server
tcp 0 0 192.168.4.50:6350 0.0.0.0:* LISTEN 11523/redis-server
[root@redis~] # redis-cli -h 192.168.4.50 -p 6350 -a 123456 //访问服务
192.168.4.50:6350> ping
PONG
192.168.4.50:6350> exit
2)编写网站脚本
[root@lnmp~] # vim /usr/local/nginx/html/linkredis.php
<?php
$redis = new redis();
$redis->connect("192.168.4.50","6350");
$redis->auth("123456");
$redis->set("linux","redhat");
echo $redis->get("linux");
?>
3)访问网站脚本
[root@lnmp~] #curl http://localhost/linkredis.php //访问nginx服务
redhat
4)在192.168.4.50 服务器,查看数据
[root@redis~] # redis-cli -h 192.168.4.50 -p 6350 -a 123456 //连接redis服务
192.168.4.50:6350> keys * //查看变量
1) "linux"
192.168.4.50:6350>
192.168.4.50:6350> get linux //获取值
"redhat"
192.168.4.50:6350>