LNMP+REDIS
实验准备
1:Nginx+php 192.168.108.10
2:Redis服务 192.168.108.20
3:数据库 192.168.108.30
实验流程
一、第一台服务器{nginx+php}
1.源码安装nginx
tar -xf nginx-1.18.0.tar.gz
cd nginx-1.18.0
yum -y install gcc-c++ pcre-devel zlib-devel
./configure --prefix=/usr/local/nginx
make && make install
2.安装php-fpm
yum -y install php-common
yum -y install php-fpm
systemctl start php-fpm
netstat -ntulap |grep :9000
vim /usr/local/nginx/conf/nginx.conf
3.修改nginx配置文件,开启php解析功能
vim /usr/local/nginx/conf/nginx.conf
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
include fastcgi.conf;
}
/usr/local/nginx/sbin/nginx -t
检查nginx配置文件是否正确
vim /usr/local/nginx/html/test.php
<?php
echo "hello world"
?>
/usr/local/nginx/sbin/nginx 当页面能出现helloworld则证明成功
4.PHP和Redis连接
yum -y install php
yum -y install php-devel
cd
yum -y install wget
wget https://github.com/phpredis/phpredis/archive/2.2.8.tar.gz
tar -xf 2.2.8.tar.gz
cd phpredis-2.2.8/
phpize
./configure --with-php-config=/usr/bin/php-config
make
make install
ls /usr/lib64/php/modules/
#查看是否有so文件生成
vim /etc/php.ini
#修改php主配置文件
#728行
extension_dir = "/usr/lib64/php/modules"
extension = "redis.so"
systemctl restart php-fpm
php -m |grep redis
#是否过滤出redis
5.测试Redis是否连接成功,后面会说Redis如何安装
vim redis.php
<?php
$redis=new redis();
$redis->connect("192.168.108.20",6379);
$redis->auth("123456");
$redis->set("school","tarena");
echo $redis->get("school");
?>
第二台服务器测试
[root@localhost redis]# /usr/local/redis/bin/redis-cli
127.0.0.1:6379>
127.0.0.1:6379>
127.0.0.1:6379> get name
(nil)
127.0.0.1:6379>
127.0.0.1:6379> keys *
1) "school"
127.0.0.1:6379> get school
"tarena"
127.0.0.1:6379> exit
6.PHP和Mariadb数据库连接
yum -y install php-mysql php-gd
vim /etc/php.ini
extension=msql.dll
extension=msql.so
extension=/path/to/extension/msql.so
7.测试是否连接成功
vim tsql.php
<?php
//tsqli.php
$servername = "192.168.108.30";
$username = "redis"; //用户名
$password = "123"; //密码
// 创建连接
$conn = mysql_connect($servername, $username, $password);
// 检测连接
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
$conn->close();
?>
注意测试前重启服务
pkill nginx
/usr/local/nginx/sbin/nginx
systemctl restart php-fpm
8.编写index.php页面
vim index.php
<?php
$redis = new Redis();
$redis->connect('192.168.108.20',6379) or die ("could net connect redis server");
$query = "select * from test";
for ($key = 1; $key < 10; $key++)
{
if (!$redis->get($key))
{
$connect = mysql_connect("192.168.108.30","redis","123");
mysql_select_db(test);
$result = mysql_query($query);
while ($row = mysql_fetch_assoc($result))
{
$redis->set($row['id'],$row['name']);
}
$myserver = 'mar';
break;
}
else
{
$myserver = "redis";
$data[$key] = $redis->get($key);
}
}
echo $myserver;
echo "<br>";
for ($key = 1; $key < 10; $key++)
{
echo "number is <b><font color=#FF0000>$key</font></b>";
echo "<br>";
echo "name is <b><font color=#FF0000>$data[$key]</font></b>";
echo "<br>";
}
?>
二、第二台服务器配置{Redis}
1.Redis安装
yum -y install gcc
tar -xf redis-3.2.12.tar.gz
make
make PREFIX=/usr/local/redis install
cp -a redis.conf /usr/local/redis/
2.修改redis配置文件
vim /usr/local/redis/redis.conf
bind 127.0.0.1 192.168.108.20
#监听地址
daemonize yes
#放入后台执行
3.启动Redis
/usr/local/redis/bin/redis-server /usr/local/redis/redis.conf
[root@localhost redis]# netstat -antp
tcp 0 0 127.0.0.1:6379 0.0.0.0:* LISTEN 10284/redis-server
/usr/local/redis/bin/redis-cli
4.返回去测试redis.php,查看是否有数据
[root@localhost redis]# /usr/local/redis/bin/redis-cli
127.0.0.1:6379>
127.0.0.1:6379>
127.0.0.1:6379> get name
(nil)
127.0.0.1:6379>
127.0.0.1:6379> keys *
1) "school"
127.0.0.1:6379> get school
"tarena"
127.0.0.1:6379> exit
三、第三台服务器配置{Mariadb}
这里为了方便直接yum安装了,也可以换成源码包
1.安装mariadb
yum -y install mariadb-server
systemctl start mariadb
#启动服务
mysql_secure_installation
#初始化
除了第一个回车,接下来一路y就行了
mysql -uroot -p123
#登录数据库
grant all on test.* to 'redis'@'%' identified by '123';
#授权用户权限
flush privileges;
#刷新权限
exit
2.创建表结构并插入收据
vim test.sql
use test;
CREATE TABLE `test` (`id` int(7) NOT NULL AUTO_INCREMENT, `name` char(8) DEFAULT NULL, PRIMARY KEY (`id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `test` VALUES (1,'test1'),(2,'test2'),(3,'test3'),(4,'test4'),(5,'test5'),(6,'test6'),(7,'test7'),(8,'test8'),(9,'test9');
#你也可以在数据库里面自己创建
3.查看是否还原成功
还原数据库
mysql -uroot -p123 < test.sql
登录数据库后查看
mysql -uroot -p123
select * from test.test;