利用Docker Compose部署多容器LNMP環(huán)境的實戰(zhàn)步驟
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)建,安裝
mysqli和pdo_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 安裝
mysqli和pdo_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)文章希望大家以后多多支持腳本之家!
- Docker Compose一鍵部署LNMP環(huán)境詳細過程
- Docker-compose詳解和LNMP搭建全過程
- 使用 Docker-compose 搭建lnmp的詳細過程
- 詳解docker compose搭建lnmpr環(huán)境實現(xiàn)
- Docker多容器編排Compose實戰(zhàn)案例
- Docker Compose 輕松管理多容器應(yīng)用小結(jié)
- 使用?Docker?Compose?構(gòu)建復(fù)雜的多容器?App的方法
- Docker Compose快速部署多容器服務(wù)實戰(zhàn)的實例詳解
- Docker Compose多容器部署的實現(xiàn)
相關(guā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)入索引數(shù)據(jù),還討論了在Elasticsearch?Head中解決請求頭顯示不正確的問題,包括從容器中復(fù)制文件、編輯文件和將文件回傳到容器2025-11-11

