LNMP + Redis

案例 : 部署LNMP + Redis环境

  • 步骤
    • 部署LNMP环境
      • 运行自动部署脚本
      • 配置Nginx动静分离
    • 配置支持Redis
      • 部署redis服务 192.168.4.50
      • 在网站服务器安装提供模块的软件包
      • 配置php支持redis服务并测试
      • 编写php脚本支持redis服务
      • 测试配置

准备环境

  • 配置虚拟机的yum源
  • 准备相应软件包 提取码: j18k
  • 自动部署脚本如下 :
# 自动部署相关环境
#!/bin/bash
yumIns(){
        rpm -q $1 &> /dev/null
        [ $? -ne 0  ] && ( yum -y install $1 &> /dev/null )
}
yumIns gcc;yumIns pcre-devel;yumIns openssl-devel;yumIns mariadb;yumIns mariadb-server;yumIns mariadb-devel;yumIns php;yumIns php-mysql;yumIns php-devel;yumIns php-fpm;yumIns zlib-devel;yumIns automake;yumIns autoconf
read -p "请指定nginx源码包路径:" path
tar -xf $path -C /root/ &> /dev/null
cd ${path%.tar.gz}
id nginx &> /dev/null
if [ $? -ne 0 ];then
        useradd -s /sbin/nologin nginx &> /dev/null
fi
./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module --with-stream --with-http_stub_status_module &> /dev/null
( make &> /dev/null ) && ( make install &> /dev/null )
netstat -ntulp | grep -q nginx
[ $? -eq 0 ] && nginx -s stop
cp -f /usr/local/nginx/sbin/nginx /usr/bin/nginx
nginx
systemctl restart mariadb &> /dev/null
systemctl restart php-fpm &> /dev/null


  • 修改nginx配置文件, 设置动静分离
vim /usr/local/nginx/conf/nginx.conf
	location ~ \.php$ {
        root           html;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        include        fastcgi.conf;
    }

nginx -s reload
nginx -t 		# 测试修改
  • 测试配置
vim /usr/local/nginx/html/test.php
	<?php
		echo "Hello World!";
	?>
curl 192.168.4.57/test.php

配置PHP支持redis

  • 安装php扩展
# 解压扩展包
tar -zxf php-redis-2.2.4.tar.gz
cd phpredis-2.2.4
# 生成配置文件 php-config 及 configure命令
phpize
	Configuring for:
	PHP Api Version:         20100412
	Zend Module Api No:      20100525
	Zend Extension Api No:   220100525
# 配置
./configure --with-php-config=/usr/bin/php-config
# 编译
make
# 安装
make install
	Installing shared extensions:     /usr/lib64/php/modules/ 		# 列出模块文件目录
  • 修改php.ini配置文件
vim /etc/php.ini
	# 修改第728行 指定模块文件目录
	extension_dir = "/usr/lib64/php/modules/"
	# 修改730行 指定模块文件名
	extension = "redis.so"
# 重启 php-fpm服务
systemctl restart php-fpm
# 查看已加载的模块
php -m | grep -i redis
	redis

测试环境

  • 在网站编写脚本测试环境
vim /usr/local/nginx/html/test.php
	<?php
	$redis=new redis();
	$redis->connect("192.168.4.50","6350");
	$redis->auth("123456");
	$redis->set("linux","redhat");
	echo $redis->get("linux");
	?>
curl 192.168.4.57/test.php
	redhat
  • 在redis服务器, 查看数据
redis-cli -h 192.168.4.50 -p 6350 -a 123456
192.168.4.50:6350> keys *
1) "linux"
192.168.4.50:6350> get linux
"redhat"
发布了25 篇原创文章 · 获赞 24 · 访问量 7925

猜你喜欢

转载自blog.csdn.net/qq_40023447/article/details/103603302