Quantcast
Channel: 小蓝博客
Viewing all articles
Browse latest Browse all 3145

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

$
0
0

使用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: ./nginx
    ports:
      - "80:80"
    volumes:
      - ./wordpress:/var/www/html
    depends_on:
      - php
      - mysql

  php:
    build:
      context: ./php
    volumes:
      - ./wordpress:/var/www/html

  mysql:
    image: mysql:5.7
    volumes:
      - mysql_data:/var/lib/mysql
    environment:
      MYSQL_ROOT_PASSWORD: root
      MYSQL_DATABASE: wordpress

  wordpress:
    build:
      context: ./wordpress
    volumes:
      - ./wordpress:/var/www/html
    depends_on:
      - mysql

volumes:
  mysql_data:

启动服务

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

docker-compose up -d

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

分析说明表

组件说明关键配置
Nginx反向代理和静态文件服务配置文件 nginx.conf,通过 fastcgi_pass连接PHP容器
PHP处理PHP请求Dockerfile安装 mysqli扩展
MySQL数据库服务使用环境变量配置数据库名称和用户
WordPress内容管理系统配置文件 wp-config.php,指定数据库连接信息
Docker Compose管理和编排多个Docker容器docker-compose.yml定义各服务及其依赖关系

总结

通过使用Docker Compose,我们可以轻松编排LNMP环境并部署WordPress。本文详细介绍了各组件的Dockerfile和配置文件编写,并通过docker-compose.yml文件实现了整个环境的自动化部署。这种方法不仅简化了部署过程,还提高了环境的可移植性和一致性。希望本文能帮助你更好地理解和使用Docker Compose来管理和部署复杂的应用程序。


Viewing all articles
Browse latest Browse all 3145

Trending Articles