当前位置: 首页 > news >正文

Docker-compose 编排lnmp(dockerfile) 完成Wordpress

使用Docker Compose编排LNMP(Linux, Nginx, MySQL, PHP)环境并部署WordPress,是一个非常高效的解决方案。本文将详细介绍如何编写Dockerfile和docker-compose.yml文件来完成这一任务。

环境准备

确保已经安装了以下工具:

  • Docker
  • Docker Compose

创建项目目录结构

首先,创建一个项目目录,并在其中创建所需的文件和子目录。

lnmp-wordpress/
├── docker-compose.yml
├── nginx/
│   ├── Dockerfile
│   └── nginx.conf
├── php/
│   └── Dockerfile
└── wordpress/├── Dockerfile└── wp-config.php
​

编写Nginx的Dockerfile和配置文件

nginx/Dockerfile

FROM nginx:latest
COPY nginx.conf /etc/nginx/nginx.conf
​

nginx/nginx.conf

server {listen 80;server_name localhost;root /var/www/html;index index.php index.html index.htm;location / {try_files $uri $uri/ /index.php?$args;}location ~ .php$ {include snippets/fastcgi-php.conf;fastcgi_pass php:9000;fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;include fastcgi_params;}location ~ /.ht {deny all;}
}
​

编写PHP的Dockerfile

php/Dockerfile

FROM php:7.4-fpm
RUN docker-php-ext-install mysqli
​

编写WordPress的Dockerfile和配置文件

wordpress/Dockerfile

FROM wordpress:latest
COPY wp-config.php /var/www/html/wp-config.php
​

wordpress/wp-config.php

<?php
define('DB_NAME', 'wordpress');
define('DB_USER', 'root');
define('DB_PASSWORD', 'root');
define('DB_HOST', 'mysql');
define('DB_CHARSET', 'utf8');
define('DB_COLLATE', '');define('AUTH_KEY',         'put your unique phrase here');
define('SECURE_AUTH_KEY',  'put your unique phrase here');
define('LOGGED_IN_KEY',    'put your unique phrase here');
define('NONCE_KEY',        'put your unique phrase here');
define('AUTH_SALT',        'put your unique phrase here');
define('SECURE_AUTH_SALT', 'put your unique phrase here');
define('LOGGED_IN_SALT',   'put your unique phrase here');
define('NONCE_SALT',       'put your unique phrase here');$table_prefix = 'wp_';define('WP_DEBUG', false);if ( !defined('ABSPATH') )define('ABSPATH', dirname(__FILE__) . '/');require_once(ABSPATH . 'wp-settings.php');
​

编写Docker Compose文件

docker-compose.yml

version: '3.8'services:nginx:build:context: ./nginxports:- "80:80"volumes:- ./wordpress:/var/www/htmldepends_on:- php- mysqlphp:build:context: ./phpvolumes:- ./wordpress:/var/www/htmlmysql:image: mysql:5.7volumes:- mysql_data:/var/lib/mysqlenvironment:MYSQL_ROOT_PASSWORD: rootMYSQL_DATABASE: wordpresswordpress:build:context: ./wordpressvolumes:- ./wordpress:/var/www/htmldepends_on:- mysqlvolumes:mysql_data:
​

启动服务

在项目根目录中运行以下命令启动服务:

docker-compose up -d
​

该命令将构建和启动Nginx、PHP、MySQL和WordPress容器。启动完成后,可以在浏览器中访问 http://localhost来设置和使用WordPress。


总结

通过使用Docker Compose,我们可以轻松编排LNMP环境并部署WordPress。本文详细介绍了各组件的Dockerfile和配置文件编写,并通过docker-compose.yml文件实现了整个环境的自动化部署。这种方法不仅简化了部署过程,还提高了环境的可移植性和一致性。

相关文章:

  • 【AI学习笔记】Coze平台实现生成小红书热门多图笔记
  • 【Pandas】pandas DataFrame drop
  • 九、【前后端联调篇】Vue3 + Axios 异步通信实战
  • 探索C++标准模板库(STL):String接口实践+底层的模拟实现(中篇)
  • GitHub 趋势日报 (2025年05月27日)
  • Linux中基础IO(下)
  • Flink CEP实践总结:使用方法、常见报错、优化与难点应对
  • 【Redis】基本架构
  • java导入excel
  • Android-Room + WorkManager学习总结
  • Python生成ppt(python-pptx)N问N答(如何绘制一个没有背景的矩形框;如何绘制一个没有背景的矩形框)
  • Pytest 是什么
  • Function calling和mcp区别
  • 【pycharm】如何连接远程仓库进行版本管理(应用版本)
  • OpenWrt 插件安装失败的常见问题和解决方法
  • LeetCode 169:多数元素 - 摩尔投票法的精妙解法
  • 8.5 Q1|中山大学CHARLS发文 | 甘油三酯葡萄糖-腰身高比指数与中国中老年人心血管疾病的关系
  • 删除队列中整数
  • nova14 ultra,是如何防住80°C热水和10000KPa水压冲击的?
  • Windows下的Qtxlsx下载和编译打包成库
  • 有关建设旅行网站的建设/深圳网络营销外包公司推荐
  • b2b电子商务平台官网/百度推广账户优化
  • 200M电信宽带做网站/百度联盟
  • 网站建设 论文/大数据营销系统多少钱
  • 做网站知名的学习网站/营销方案怎么写?
  • 产品网站建设/天津建站网