最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

利用Docker Compose部署多容器LNMP環(huán)境的實戰(zhàn)步驟

 更新時間:2025年12月10日 10:30:13   作者:酒城譯癡無心劍  
本次實戰(zhàn)通過Docker Compose成功構(gòu)建了完整的LNMP(Nginx + MySQL + PHP-FPM)開發(fā)環(huán)境,通過自定義PHP-FPM鏡像安裝MySQL擴展,配置 Nginx 反向代理 PHP 請求,本文介紹的非常詳細,感興趣的朋友一起看看吧

1. 實戰(zhàn)概述

  • 本實戰(zhàn)基于 Docker Compose 搭建 LNMP 開發(fā)環(huán)境,通過自定義 PHP-FPM 鏡像安裝 MySQL 擴展,配置 Nginx 反向代理 PHP 請求,導(dǎo)入商品數(shù)據(jù)并創(chuàng)建 showProducts.php 頁面,成功實現(xiàn)商品信息的表格化展示,完整驗證了 Web 服務(wù)、動態(tài)腳本與數(shù)據(jù)庫的協(xié)同工作。

2. 實戰(zhàn)步驟

2.1 創(chuàng)建項目目錄

執(zhí)行命令:mkdir lnmp

執(zhí)行命令:cd lnmp

執(zhí)行命令:mkdir wwwroot php-fpm

2.2 準(zhǔn)備nginx配置文件

2.2.1 創(chuàng)建nginx容器

執(zhí)行命令:docker run -dit --name hwnginx nginx

2.2.2 拷貝nginx配置文件到當(dāng)前目錄

執(zhí)行命令:docker cp hwnginx:/etc/nginx ./

2.2.3 停止并刪除nginx容器

執(zhí)行命令:docker stop hwnginx

執(zhí)行命令:docker rm hwnginx

2.2.4 修改nginx配置文件

執(zhí)行命令: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 準(zhǔn)備測試網(wǎng)頁文件

執(zhí)行命令: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>

執(zhí)行命令:notepad wwwroot\index.php

<?php
// 設(shè)置時區(qū)
date_default_timezone_set('Asia/Shanghai');
// 獲取當(dāng)前日期時間并格式化
$now = date('Y-m-d H:i:s');
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">    
    <title>當(dāng)前時間</title>
</head>
<body  style='text-align: center; background-color: antiquewhite'>
    <h1 style='color: red'>系統(tǒng)當(dāng)前日期時間</h1>
    <p  style='color: blue'><?php echo htmlspecialchars($now); ?></p>
</body>
</html>

執(zhí)行命令:notepad wwwroot\db.php

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>數(shù)據(jù)庫連接測試</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;">? 數(shù)據(jù)庫連接失??!</h2>';
            exit;
        }
        echo '<h2 style="color:green;">? 數(shù)據(jù)庫連接成功!</h2>';
        mysqli_close($link);
    ?>
</body>
</html>

2.4 準(zhǔn)備php的Dockerfile文件

  • Docker官方php鏡像沒有安裝mysql插件,無法連接mysql數(shù)據(jù)庫,需要利用Dockerfile重新生成新鏡像。從php:7.4-fpm開始構(gòu)建,安裝 mysqlipdo_mysql 擴展。
  • 執(zhí)行命令:notepad php-fpm\Dockerfile

2.5 編寫Docker編排配置文件

執(zhí)行命令: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 構(gòu)建LNMP項目

執(zhí)行命令:docker-compose up -d

  • 結(jié)果說明:執(zhí)行 docker-compose up -d 成功啟動 LNMP 環(huán)境,創(chuàng)建了默認網(wǎng)絡(luò)和 MySQL 數(shù)據(jù)卷,依次啟動 fpm、mysql 和 nginx 容器,服務(wù)運行正常,可訪問 http://localhost:8100 進行測試。

2.7 測試LNMP項目

2.7.1 創(chuàng)建數(shù)據(jù)庫連接

創(chuàng)建LNMPMySQL數(shù)據(jù)庫連接

2.7.2 運行數(shù)據(jù)庫腳本

運行數(shù)據(jù)庫腳本lnmp.sql

單擊【開始】按鈕

2.7.3 查看數(shù)據(jù)表

查看商品表

2.7.4 準(zhǔn)備圖片文件

D:\docker_work\lnmp\wwwroot\images里準(zhǔn)備15張商品圖片

2.7.5 創(chuàng)建顯示商品頁面

D:\docker_work\lnmp\wwwroot里創(chuàng)建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
// 數(shù)據(jù)庫連接配置
$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;'>暫無商品數(shù)據(jù)。</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;'>數(shù)據(jù)庫錯誤:" . htmlspecialchars($e->getMessage()) . "</p>";
}
?>
</body>
</html>

2.7.6 重新構(gòu)建LNMP項目

執(zhí)行命令: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項目

執(zhí)行命令:docker-compose down

  • 結(jié)果說明:執(zhí)行 docker-compose down 成功停止并移除了 mysql、nginx、fpm 三個容器及默認網(wǎng)絡(luò) lnmp_default,徹底清理了 LNMP 環(huán)境,避免資源占用和配置沖突,為下次部署提供干凈環(huán)境。

3. 實戰(zhàn)總結(jié)

  • 本次實戰(zhàn)通過 Docker Compose 成功構(gòu)建了完整的 LNMP(Nginx + MySQL + PHP-FPM)開發(fā)環(huán)境。從創(chuàng)建項目目錄、提取并配置 Nginx 默認配置,到編寫自定義 Dockerfile 安裝 mysqlipdo_mysql 擴展,確保 PHP 能正常連接數(shù)據(jù)庫;通過導(dǎo)入 lnmp.sql 初始化商品數(shù)據(jù),并在 wwwroot 中準(zhǔn)備圖片資源,最終實現(xiàn) showProducts.php 頁面以美觀表格展示全部商品信息。整個流程涵蓋了容器編排、服務(wù)協(xié)同、數(shù)據(jù)庫操作與前端展示,驗證了 LNMP 架構(gòu)的完整性與可用性。環(huán)境可一鍵啟動(docker-compose up -d)和清理(docker-compose down),具備良好的可移植性與復(fù)用性,適用于教學(xué)、開發(fā)與快速部署場景。

到此這篇關(guān)于利用Docker Compose部署多容器LNMP環(huán)境的實戰(zhàn)步驟的文章就介紹到這了,更多相關(guān)docker compose 部署lnmp內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 利用Docker?運行?python?簡單程序

    利用Docker?運行?python?簡單程序

    這篇文章主要介紹了利用Docker?運行?python?簡單程序,文章圍繞主題展開詳細的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下
    2022-04-04
  • 安裝docker-ce報錯yum倉庫錯誤問題及解決

    安裝docker-ce報錯yum倉庫錯誤問題及解決

    安裝Docker-CE時,可能會因依賴軟件版本不足導(dǎo)致報錯,解決方法包括使用yum update更新軟件包,若倉庫無該包則需更新repo源,也可通過yum install -y docker-ce --skip-broken跳過依賴更新直接安裝,若遇yum命令執(zhí)行報錯
    2024-11-11
  • Docker v1.13.0 正式版發(fā)布

    Docker v1.13.0 正式版發(fā)布

    本文給大家分享的是Docker v1.13.0 正式版發(fā)布的信息,最近一直在學(xué)習(xí)docker中,所以關(guān)注的比較多,這里分享給大家
    2017-01-01
  • docker部署mysql8之PXC8.0分布式集群過程

    docker部署mysql8之PXC8.0分布式集群過程

    這篇文章主要介紹了docker部署mysql8之PXC8.0分布式集群過程,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-07-07
  • docker如何開啟2375端口提供外部訪問docker

    docker如何開啟2375端口提供外部訪問docker

    這篇文章主要介紹了docker如何開啟2375端口提供外部訪問docker問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-08-08
  • 詳解基于Docker的服務(wù)部署流程

    詳解基于Docker的服務(wù)部署流程

    這篇文章主要介紹了詳解基于Docker的服務(wù)部署流程,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-06-06
  • Docker基于現(xiàn)有鏡像構(gòu)建新鏡像的實現(xiàn)方法

    Docker基于現(xiàn)有鏡像構(gòu)建新鏡像的實現(xiàn)方法

    這篇文章主要介紹了Docker基于現(xiàn)有鏡像構(gòu)建新鏡像的實現(xiàn)方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-12-12
  • 在docker容器中elasticsearch的導(dǎo)入導(dǎo)出方式

    在docker容器中elasticsearch的導(dǎo)入導(dǎo)出方式

    文章詳細介紹了如何使用Docker拉取Elasticsearch鏡像,并導(dǎo)出和導(dǎo)入索引數(shù)據(jù),還討論了在Elasticsearch?Head中解決請求頭顯示不正確的問題,包括從容器中復(fù)制文件、編輯文件和將文件回傳到容器
    2025-11-11
  • 使用Portainer部署Docker容器的項目實踐

    使用Portainer部署Docker容器的項目實踐

    這篇文章主要介紹了使用Portainer部署Docker容器的項目實踐,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-04-04
  • Docker部署搭建WebDav服務(wù)的詳細過程

    Docker部署搭建WebDav服務(wù)的詳細過程

    這篇文章主要介紹了Docker部署WebDav服務(wù),小編綜合了各種共享方式后最終選擇了使用 WebDav 來共享文件,下面小編把搭建部署過程分享給大家,需要的朋友可以參考下
    2022-08-08

最新評論

新建县| 汶上县| 视频| 平舆县| 交口县| 靖宇县| 哈巴河县| 喀什市| 西和县| 溧水县| 武邑县| 汤原县| 民权县| 衡阳市| 盖州市| 耿马| 邵武市| 银川市| 东安县| 环江| 德保县| 沙雅县| 英山县| 武乡县| 石林| 清苑县| 鄢陵县| 云梦县| 察隅县| 灵寿县| 永平县| 峨眉山市| 石林| 全椒县| 淳化县| 大田县| 莫力| 铜鼓县| 多伦县| 安义县| 福州市|