1. 程式人生 > 資料庫 >LNMP+Redis(Nginx連線Redis和MySQL)

LNMP+Redis(Nginx連線Redis和MySQL)

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;

4.返回去測試index.php和tsql.php

在這裡插入圖片描述