docker

关注公众号 jb51net

关闭
首页 > 网站技巧 > 服务器 > 云和虚拟化 > docker > docker compose 部署lnmp

利用Docker Compose部署多容器LNMP环境的实战步骤

作者:酒城译痴无心剑

本次实战通过Docker Compose成功构建了完整的LNMP(Nginx + MySQL + PHP-FPM)开发环境,通过自定义PHP-FPM镜像安装MySQL扩展,配置 Nginx 反向代理 PHP 请求,本文介绍的非常详细,感兴趣的朋友一起看看吧

1. 实战概述

2. 实战步骤

2.1 创建项目目录

执行命令:mkdir lnmp

执行命令:cd lnmp

执行命令:mkdir wwwroot php-fpm

2.2 准备nginx配置文件

2.2.1 创建nginx容器

执行命令:docker run -dit --name hwnginx nginx

2.2.2 拷贝nginx配置文件到当前目录

执行命令:docker cp hwnginx:/etc/nginx ./

2.2.3 停止并删除nginx容器

执行命令:docker stop hwnginx

执行命令:docker rm hwnginx

2.2.4 修改nginx配置文件

执行命令:notepad nginx/conf.d/default.conf

server {
    listen       80;
    listen  [::]:80;
    server_name  localhost;
    #access_log  /var/log/nginx/host.access.log  main;
    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }
    #error_page  404              /404.html;
    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}
    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    location ~ \.php$ {
        root                /var/www;
        fastcgi_pass   fpm:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }
    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}

2.3 准备测试网页文件

执行命令:notepad wwwroot\index.html

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">    
    <title>首页</title>
</head>
<body style='text-align: center; background-color: antiquewhite'>
    <h1 style='color: red'>欢迎访问Nginx页面~</h1>
</body>
</html>

执行命令:notepad wwwroot\index.php

<?php
// 设置时区
date_default_timezone_set('Asia/Shanghai');
// 获取当前日期时间并格式化
$now = date('Y-m-d H:i:s');
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">    
    <title>当前时间</title>
</head>
<body  style='text-align: center; background-color: antiquewhite'>
    <h1 style='color: red'>系统当前日期时间</h1>
    <p  style='color: blue'><?php echo htmlspecialchars($now); ?></p>
</body>
</html>

执行命令:notepad wwwroot\db.php

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>数据库连接测试</title>
    <style>
        body {
            text-align: center;
            padding-top: 50px;
            font-family: Arial, sans-serif;
            background-color: antiquewhite
        }
    </style>
</head>
<body>
    <?php
        $link = mysqli_connect('mysql', 'root', '903213');
        if (!$link) {
            echo '<h2 style="color:red;">❌ 数据库连接失败!</h2>';
            exit;
        }
        echo '<h2 style="color:green;">✅ 数据库连接成功!</h2>';
        mysqli_close($link);
    ?>
</body>
</html>

2.4 准备php的Dockerfile文件

2.5 编写Docker编排配置文件

执行命令:notepad docker-compose.yml

services:
  fpm:
    build: ./php-fpm
    container_name: fpm
    volumes:
      - ./wwwroot:/var/www
    ports:
      - "9000"
  nginx:
    image: nginx
    container_name: nginx
    ports:
      - "8100:80"
    volumes:
      - ./wwwroot:/usr/share/nginx/html
      - ./nginx/nginx.conf:/etc/nginx/nginx.conf
      - ./nginx/conf.d:/etc/nginx/conf.d
    depends_on:
      - fpm
  mysql:
    image: mysql:5.7
    container_name: mysql
    ports:
      - "3309:3306"
    environment:
      MYSQL_ROOT_PASSWORD: 903213
      MYSQL_DATABASE: lnmp
    volumes:
      - mysql_data:/var/lib/mysql
volumes:
  mysql_data:

2.6 构建LNMP项目

执行命令:docker-compose up -d

2.7 测试LNMP项目

2.7.1 创建数据库连接

创建LNMPMySQL数据库连接

2.7.2 运行数据库脚本

运行数据库脚本lnmp.sql

单击【开始】按钮

2.7.3 查看数据表

查看商品表

2.7.4 准备图片文件

D:\docker_work\lnmp\wwwroot\images里准备15张商品图片

2.7.5 创建显示商品页面

D:\docker_work\lnmp\wwwroot里创建showProducts.php文件

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>商品列表</title>
    <style>
        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            background-color: #f9f9f9;
            margin: 20px;
            color: #333;
        }
        h1 {
            text-align: center;
            color: #0066cc;
            margin-bottom: 30px;
        }
        table {
            width: 100%;
            border-collapse: collapse;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
            background: white;
            overflow: hidden;
            border-radius: 8px;
        }
        th, td {
            padding: 12px 15px;
            text-align: left;
            border-bottom: 1px solid #ddd;
        }
        th {
            background-color: #0066cc;
            color: white;
            font-weight: bold;
        }
        tr:hover {
            background-color: #f1f1f1;
        }
        img {
            width: 80px;
            height: 80px;
            object-fit: cover;
            border-radius: 4px;
            vertical-align: middle;
        }
        .no-image {
            width: 80px;
            height: 80px;
            background-color: #eee;
            display: flex;
            align-items: center;
            justify-content: center;
            color: #999;
            font-size: 12px;
        }
        .price {
            color: #e63946;
            font-weight: bold;
        }
    </style>
</head>
<body>
<h1>商品列表</h1>
<?php
// 数据库连接配置
$host = 'mysql';
$user = 'root';
$pass = '903213';
$dbname = 'lnmp';
try {
    $pdo = new PDO("mysql:host=$host;dbname=$dbname;charset=utf8", $user, $pass);
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    // 查询所有商品
    $stmt = $pdo->query("SELECT id, name, price, image, add_time FROM t_product");
    $products = $stmt->fetchAll(PDO::FETCH_ASSOC);
    if (empty($products)) {
        echo "<p style='text-align:center; color:#999;'>暂无商品数据。</p>";
    } else {
        echo "<table>";
        echo "<thead>";
        echo "<tr>";
        echo "<th>ID</th>";
        echo "<th>商品名称</th>";
        echo "<th>价格</th>";
        echo "<th>图片</th>";
        echo "<th>添加时间</th>";
        echo "</tr>";
        echo "</thead>";
        echo "<tbody>";
        foreach ($products as $product) {
            echo "<tr>";
            echo "<td>{$product['id']}</td>";
            echo "<td>{$product['name']}</td>";
            echo "<td class='price'>¥{$product['price']}</td>";
            echo "<td>";
            if (!empty($product['image'])) {
                echo "<img src='{$product['image']}' alt='{$product['name']}'>";
            } else {
                echo "<div class='no-image'>无图</div>";
            }
            echo "</td>";
            echo "<td>{$product['add_time']}</td>";
            echo "</tr>";
        }
        echo "</tbody>";
        echo "</table>";
    }
} catch (PDOException $e) {
    echo "<p style='color:red;'>数据库错误:" . htmlspecialchars($e->getMessage()) . "</p>";
}
?>
</body>
</html>

2.7.6 重新构建LNMP项目

执行命令:docker-compose up --build -d

2.7.7 访问页面

访问http://localhost:8100

访问http://localhost:8100/index.php

访问http://localhost:8100/db.php

访问http://localhost:8100/showProducts.php

2.8 停止并删除LNMP项目

执行命令:docker-compose down

3. 实战总结

到此这篇关于利用Docker Compose部署多容器LNMP环境的实战步骤的文章就介绍到这了,更多相关docker compose 部署lnmp内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文