docker-compose快速搭建wordpress个人网站

2021-12-05 13:33:07 浏览数 (1)

先后用过wordpress、GHost搭建过个人博客,Wordpress让人诟病的是相比与GHost的轻量而庞大复杂框架,安装环境依赖复杂,配置繁琐,过程痛苦,升级版本更是糟心,所以长期依赖一直用GHost撑着心血来潮的博客。但是,wordpress对得起复杂架构的是她的普适性,博客是小试身手,各种论坛、企业门户、甚至电商平台也是轻松胜任;其android、ios的APP端入口更是方便随手拍个照片写个感想。docker技术发展,wordpress曾经麻烦的搭建过程就变的很简单。

1. docker-compose及dockerfile

1.1 总体介绍及目录文件

整体长期运行3个docker container。nginx作为接收路由转发,php作为wordpress运行,mysql用于存放数据,另外还有一个php包含wp-cli的运行调试container,在需要命令行调试时候起来看看,大部分用不上。

目录结构.png目录结构.png

mysql是自己建立的空文件夹,用于mysql的container挂载本地目录,后期备份blog可以只复制这个目录,如果像我一样通过github来版本控制,忽略这个目录。

wordpress存放从wordpress官方下载zip或者gz文件解压的wordpress源码文件,php的container用挂载这个目录,运行wordpress

nginx存放唯一的两个自己修改的地方,一个是default.conf网站配置文件,需要在对应地方改成你从腾讯云购买的域名;一个是certs存放ssl证书文件,要与default.conf中的路径匹配

1.2 docker-compose.yml

代码语言:txt复制
version: '3'

networks:
  wordpress:

services:
  site:
    build:
      context: .
      dockerfile: nginx.dockerfile
    container_name: nginx
    ports:
      - 80:80
      - 443:443
    volumes:
      - ./wordpress:/var/www/html:delegated
    depends_on:
      - php
      - mysql
    networks:
      - wordpress

  mysql:
    image: mysql:5.7.29
    container_name: mysql
    restart: always
    ports:
      - 3306:3306
    environment:
      MYSQL_DATABASE: wp
      MYSQL_USER: wp
      MYSQL_PASSWORD: secret
      MYSQL_ROOT_PASSWORD: secret
      SERVICE_TAGS: dev
      SERVICE_NAME: mysql
    volumes:
      - ./mysql:/var/lib/mysql
    networks:
      - wordpress

  php:
    build:
      context: .
      dockerfile: php.dockerfile
    container_name: php
    volumes:
      - ./wordpress:/var/www/html:delegated
    networks:
      - wordpress

  wp:
    build:
      context: .
      dockerfile: php.dockerfile
    container_name: wp
    entrypoint: ['wp', '--allow-root']
    volumes:
      - ./wordpress:/var/www/html:delegated
    networks:
      - wordpress

1.3 nginx.dockerfile

代码语言:txt复制
FROM nginx:stable-alpine

ADD ./nginx/default.conf /etc/nginx/conf.d/default.conf
ADD ./nginx/certs /etc/nginx/certs/self-signed

RUN mkdir -p /var/www/html

1.4 php.dockerfile

代码语言:txt复制
FROM php:7.4-fpm-alpine
RUN touch /var/log/error_log
ADD ./php/www.conf /usr/local/etc/php-fpm.d/www.conf
RUN addgroup -g 1000 wp && adduser -G wp -g wp -s /bin/sh -D wp
RUN mkdir -p /var/www/html
RUN chown wp:wp /var/www/html
WORKDIR /var/www/html
RUN docker-php-ext-install mysqli pdo pdo_mysql && docker-php-ext-enable pdo_mysql
RUN curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
RUN chmod  x wp-cli.phar && mv wp-cli.phar /usr/local/bin/wp

1.5 php www.conf

代码语言:txt复制
[www]
user = wp
group = wp
listen = 127.0.0.1:9000
pm = dynamic
pm.max_children = 5
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 3
php_flag[display_errors] = on
php_admin_value[error_log] = /var/log/error_log
php_admin_value[error_reporting] = E_ALL & ~E_NOTICE & ~E_WARNING & ~E_STRICT & ~E_DEPRECATED
php_admin_flag[log_errors] = on

2. 修改适配的地方

2.1 nginx default.conf

代码语言:txt复制
upstream php {
    server unix:/tmp/php-cgi.socket;
    server php:9000;
}

server {
    listen 80;
    server_name www.<你的域名>.net;

    root /var/www/html;

    index index.php;

    location / {
        try_files $uri $uri/ /index.php?$args;
    }

    location ~ .php$ {
        include fastcgi.conf;
        fastcgi_intercept_errors on;
        fastcgi_pass php;
    }

    location ~* .(js|css|png|jpg|jpeg|gif|ico)$ {
        expires max;
        log_not_found off;
    }
}

server {
    listen 443 ssl;
    server_name www.flavorfan.net;

    ssl_certificate /etc/nginx/certs/self-signed/www.<你的域名>.net_bundle.crt;
    ssl_certificate_key /etc/nginx/certs/self-signed/www.<你的域名>.net.key;
    ssl_session_timeout 5m;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2; 
    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE; 
    ssl_prefer_server_ciphers on;

    root /var/www/html;

    index index.php;

    location / {
        try_files $uri $uri/ /index.php?$args;
    }

    location ~ .php$ {
        include fastcgi.conf;
        fastcgi_intercept_errors on;
        fastcgi_pass php;
    }

    location ~* .(js|css|png|jpg|jpeg|gif|ico)$ {
        expires max;
        log_not_found off;
    }

}

3 建议步骤及运行维护

工程文件夹目录下

代码语言:txt复制
# 编译img启动
docker-compose up -d --build
# 关闭
docker-compose down
# 查看log定位
docker-compose logs -f <container-name>
docker.pngdocker.png

0 人点赞