使用 Docker 和 Traefik 搭建 WordPress

本文使用「署名 4.0 國際 (CC BY 4.0)」許可協議,歡迎轉載、或從新修改使用,但須要註明來源。 署名 4.0 國際 (CC BY 4.0)php

本文做者: 蘇洋html

建立時間: 2019年04月07日 統計字數: 4925字 閱讀時間: 10分鐘閱讀 本文連接: soulteary.com/2019/04/07/…mysql


使用 Docker 和 Traefik 搭建 WordPress

其實不止一次想從新提筆聊聊 WordPress ,然而以前由於定製代碼量比較多,許多文章不得不擱置在草稿箱中。恰逢假期,整理草稿箱,從搭建開始聊起吧。web

本文將使用 Docker、Compose、Traefik 對 WordPress 進行搭建,完整操做時間應該在十分鐘內。sql

爲何選擇 WordPress

每當聊起 CMS 類軟件,聊起社區資源豐富,不禁地會想到一個「萬金油」:WordPress ,官方數據稱:docker

Over 60 million people have chosen WordPress to power the place on the web they call 「home」數據庫

Hundreds of thousands of developers, content creators, and site owners gather at monthly meetups in 436 cities worldwide.apache

WordPress 爲 33% 的互聯網提供支持。編程

許多人對它的印象還停留在執行速度慢、安全性差、代碼臃腫的博客系統上。可是事實上,通過十幾年的迭代,它的大版原本到了 5.0 (PHP 主流運行時也來到了 7.0 時代),性能早已不是問題、安全問題只要作適當的防禦能杜絕絕大多數。瀏覽器

emmm, 代碼確實仍是挺臃腫的。

基於官方鏡像

官方提供了容器鏡像,鏡像下載能夠直接使用下面的命令:

docker pull wordpress
複製代碼

可是爲了更好的配置使用,咱們使用 compose 的方式進行編排,將下面的內容保存爲 docker-compose.yml

version: '3'

services:

  wp:
    image: ${WP_IMAGE}
    restart: always
    networks:
      - traefik
    environment:
        WORDPRESS_DB_HOST: ${DB_HOST}
        WORDPRESS_TABLE_PREFIX: ${WP_DB_PREFIX}
        WORDPRESS_DB_NAME: ${DB_NAME}
        WORDPRESS_DB_USER: ${DB_USER}
        WORDPRESS_DB_PASSWORD: ${DB_PASS}
    volumes:
    # 若是你有定製上傳文件尺寸的需求
    # - ./config/php.conf.uploads.ini:/usr/local/etc/php/conf.d/uploads.ini
      - ./wp-app:/var/www/html
    labels:
      - "traefik.enable=true"
      - "traefik.frontend.rule=Host:${WP_DOMAINS}"
      - "traefik.frontend.entryPoints=https,http"

  mariadb:
    image: ${DB_IMAGE}
    restart: always
    container_name: ${DB_HOST}
    networks:
      - traefik
    environment:
      MYSQL_DATABASE: ${DB_NAME}
      MYSQL_USER: ${DB_USER}
      MYSQL_PASSWORD: ${DB_PASS}
      MYSQL_ROOT_PASSWORD: ${DB_ROOT_PASS}
    volumes:
      - ./data:/var/lib/mysql

  pma:
    image: ${PMA_IMAGE}
    restart: always
    networks:
      - traefik
    environment:
      MYSQL_USER: ${DB_USER}
      MYSQL_PASSWORD: ${DB_PASS}
      MYSQL_ROOT_PASSWORD: ${DB_ROOT_PASS}
      PMA_HOST: ${DB_HOST}
    labels:
      - "traefik.enable=true"
      - "traefik.frontend.rule=Host:${PMA_DOMAIN}"

networks:
  traefik:
    external: true
複製代碼

若是你還不會使用 Traefik ,能夠翻看我以前的文章,這裏不作過多贅述。

爲了可維護性,咱們將容器鏡像版本信息,應用域名,數據庫配置等抽象爲單獨的環境配置文件 .env,內容示例:

WP_IMAGE=wordpress:5.1.1-php7.3-apache
WP_DOMAINS=wp.lab.com,wp.lab.io
WP_DB_PREFIX=wp

DB_IMAGE=mariadb:10.3.8
DB_HOST=wp-db
DB_NAME=wordpress
DB_USER=wordpress
DB_PASS=wordpress
DB_ROOT_PASS=soulteary

PMA_IMAGE=phpmyadmin/phpmyadmin:4.8.2
PMA_DOMAIN=pma.wp.lab.com,pma.wp.lab.io
複製代碼

當兩個文件都保存完畢以後,咱們執行 docker-compose up 命令,你將會看到許多日誌信息,當看到相似下面的信息時,WordPress 環境便準備就緒啦。

wp-db      |
wp-db      | MySQL init process done. Ready for start up.
wp-db      |
wp-db      | 2019-04-06 16:26:48 0 [Note] mysqld (mysqld 10.3.8-MariaDB-1:10.3.8+maria~jessie) starting as process 1 ...
wp-db      | 2019-04-06 16:26:48 0 [Note] InnoDB: Using Linux native AIO
wp-db      | 2019-04-06 16:26:48 0 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
wp-db      | 2019-04-06 16:26:48 0 [Note] InnoDB: Uses event mutexes
wp-db      | 2019-04-06 16:26:48 0 [Note] InnoDB: Compressed tables use zlib 1.2.8
wp-db      | 2019-04-06 16:26:48 0 [Note] InnoDB: Number of pools: 1
wp-db      | 2019-04-06 16:26:48 0 [Note] InnoDB: Using SSE2 crc32 instructions
wp-db      | 2019-04-06 16:26:48 0 [Note] InnoDB: Initializing buffer pool, total size = 256M, instances = 1, chunk size = 128M
wp-db      | 2019-04-06 16:26:48 0 [Note] InnoDB: Completed initialization of buffer pool
wp-db      | 2019-04-06 16:26:48 0 [Note] InnoDB: If the mysqld execution user is authorized, page cleaner thread priority can be changed. See the man page of setpriority().
wp-db      | 2019-04-06 16:26:48 0 [Note] InnoDB: 128 out of 128 rollback segments are active.
wp-db      | 2019-04-06 16:26:48 0 [Note] InnoDB: Creating shared tablespace for temporary tables
wp-db      | 2019-04-06 16:26:48 0 [Note] InnoDB: Setting file './ibtmp1' size to 12 MB. Physically writing the file full; Please wait ...
wp-db      | 2019-04-06 16:26:48 0 [Note] InnoDB: File './ibtmp1' size is now 12 MB.
wp-db      | 2019-04-06 16:26:48 0 [Note] InnoDB: 10.3.8 started; log sequence number 1630833; transaction id 21
wp-db      | 2019-04-06 16:26:48 0 [Note] Plugin 'FEEDBACK' is disabled.
wp-db      | 2019-04-06 16:26:48 0 [Note] InnoDB: Loading buffer pool(s) from /var/lib/mysql/ib_buffer_pool
wp-db      | 2019-04-06 16:26:48 0 [Note] Server socket created on IP: '::'.
wp-db      | 2019-04-06 16:26:48 0 [Note] InnoDB: Buffer pool(s) load completed at 190406 16:26:48
wp-db      | 2019-04-06 16:26:48 0 [Warning] 'proxies_priv' entry '@% root@e97787886b74' ignored in --skip-name-resolve mode.
wp-db      | 2019-04-06 16:26:48 0 [Note] Reading of all Master_info entries succeded
wp-db      | 2019-04-06 16:26:48 0 [Note] Added new Master_info '' to hash table
wp-db      | 2019-04-06 16:26:48 0 [Note] mysqld: ready for connections.
wp-db      | Version: '10.3.8-MariaDB-1:10.3.8+maria~jessie'  socket: '/var/run/mysqld/mysqld.sock'  port: 3306  mariadb.org binary distribution
wp_1       | AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 172.23.0.8. Set the 'ServerName' directive globally to suppress this message 複製代碼

此時啓動瀏覽器,打開咱們配置文件中配置好的域名(WP_DOMAIN),即可以開始著名的「三分鐘」安裝了。

環境就緒,能夠開始安裝了

填寫適當信息,一路 Next ,WordPress 就安裝成功了。

WordPress 安裝就緒

後續即是具體的應用配置,以及性能、安全方面的優化啦。

WordPress 歡迎界面

其餘

若是你有操做數據庫的需求,又不想下載數據庫工具或者使用命令行進行操做,可使用 **PHPMyAdmin ** ,一樣的,在瀏覽器中打開以前配置文件中的 PMA 域名地址(PMA_DOMAIN),就能夠進行操做了。

不過須要注意的是,須要使用 rootroot password 進行登陸,由於默認狀況下,Mariadb 未對其餘用戶帳號進行遠程訪問受權。

數據庫管理界面

最後

做爲從新提筆 WordPress 第一篇,內容看起來會簡單一些,還請見諒,下一篇將以本篇爲基礎進行擴展,聊聊容器化部署的細節。


我如今有一個小小的折騰羣,裏面彙集了一些喜歡折騰的小夥伴。

在不發廣告的狀況下,咱們在裏面會一塊兒聊聊軟件、HomeLab、編程上的一些問題,也會在羣裏不按期的分享一些技術沙龍的資料。

喜歡折騰的小夥伴歡迎掃碼添加好友。(請註明來源和目的,不然不會經過審覈)

關於折騰羣入羣的那些事

相關文章
相關標籤/搜索