9.7 PHP+Redis

  • 安装
yum install php php-fpm php-pecl-redis nginx redis -y
  • 修改nginx配置文件
server {
    listen 8000; 
    server_name localhost;
    root        /var/www/html/;
    index       index.php;
      location ~ \.php$ {
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }

}
/var/www/html/redis.php
<?php $redis = new redis(); $redis->connect('127.0.0.1',6379); $redis->set('name','xxoo'); echo $redis->get('name'); ?>
/var/www/html/mysql.php
<?php
$servername = "localhost";
$username = "root";
$password = "123456";
$dbname = "mydb";
 
// 创建连接
$conn = new mysqli($servername, $username, $password, $dbname);
// 检测连接
if ($conn->connect_error) {
    die("连接失败: " . $conn->connect_error);
} 
 
// 使用 sql 创建数据表
$sql = "CREATE TABLE MyGuests (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY, 
firstname VARCHAR(30) NOT NULL,
lastname VARCHAR(30) NOT NULL,
email VARCHAR(50),
reg_date TIMESTAMP
)";
 
if ($conn->query($sql) === TRUE) {
    echo "Table MyGuests created successfully";
} else {
    echo " " . $conn->error;
}



$sql = "INSERT INTO MyGuests (firstname, lastname, email)
VALUES ('John', 'Doe', 'john@example.com')";
 
if ($conn->query($sql) === TRUE) {
    echo "insert Successfully";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}
 
$sql = "SELECT id, firstname, lastname FROM MyGuests";
$result = $conn->query($sql);

 if ($result->num_rows > 0) {
    // 输出数据
    while($row = $result->fetch_assoc()) {
        echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>";
    }
} else {
    echo "0 结果";
}
 

$conn->close();
?>

 

  • 测试
service nginx start
service php-fpm start
service redis start
service mysqldstart
elinks -dump http://localhost:8000/redis.php
elinks -dump http://localhost:8000/mysql.php

 

posted @ 2021-03-29 17:06  huakai201  阅读(74)  评论(0)    收藏  举报