Use Docker and Traefik to build WordPress (Nginx)

This article uses the "Attribution 4.0 International (CC BY 4.0)" license agreement, welcome to reprint, or re-modify the use, but need to indicate the source. Signed 4.0 International (CC BY 4.0)

Author: Su Yang

Creation time: April 07, 2019 Count of words: 6261 words Reading time: 13 minutes Read this article link: https://soulteary.com/2019/04/07/use-docker-and-traefik-to-build-wordpress -with-nginx.html

Using Docker and Traefik to build WordPress (Nginx) The
previous article introduced how to use official images to quickly build WordPress, but the official default is the "fat container" application. Next, we will talk about other options that are also based on containers: Nginx. Demonstrate how to transform an application into a "thin" container application.

This article will take about ten minutes to introduce how to use WordPress and Nginx with Traefik in a Docker container.

Why choose Nginx


NGINX is a free, open-source, high-performance HTTP server and reverse proxy, as well as an IMAP/POP3 proxy server. NGINX is known for its high performance, stability, rich feature set, simple configuration, and low resource consumption.

In addition to the official positioning of Nginx as free, open source, lightweight, and high-performance, Nginx's resources are far more abundant than Apache (the default tool for WordPress container mirroring in the previous article), whether in enterprises or personal learning.

In addition, two well-known derivative applications of Nginx are also widely used in enterprises: Tengine and OpenResty. The content in this article also applies to these two versions of "Nginx".

Container image list
This article will use the following official images as a demonstration. As mentioned above, you can use Nginx's "like" to replace it.


Nginx: 1.15.10-alpine

As a service front end to replace Apache


WordPress: 5.1.1-php7.1-fpm-alpine

Use a container that contains only WordPress code and PHP runtime


mariadb: 10.3.14

For our database, if there is a cloud database, you don’t need to configure
Traefik to use
Traefik. You can refer to previous articles, such as: Use service discovery to improve development experience, more complete Docker + Traefik usage plan, etc., and more, You can look at the tags of historical content, but I won’t repeat them here.

This article only needs to pay attention to the labels and networks field configuration in the layout file.

If you declare that the networks field of different container services contain the same content, you can make the networks of different applications be consistent.


networks:
  - traefik

For example, the above statement will make container services in a network environment called traefik.

Rewriting the container orchestration configuration
The following configuration was mentioned in the previous article. In order to avoid excessive length, I have simplified it appropriately.

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

networks:
  traefik:
    external: true

If we use Nginx as the "Web front end", then appropriate adjustments are needed here.

Rewrite the WordPress container orchestration configuration.
Because Nginx is used instead of WordPress as the traffic entry, the WordPress service can no longer be bound to Traefik, register the request domain name, and the labels field can be completely deleted.

In addition, because WordPress needs to be called remotely by Nginx, it is necessary to add a fixed container_name to the WordPress service for easy calling. As in the above configuration, WordPress calls Mariadb.


services:
  wp:
    image: ${WP_IMAGE}
    restart: always
    container_name: ${WP_HOST}
    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:
      - ./wordpress:/var/www/html

The above is the modified configuration. There is no change in the database. Let's skip it and start configuring Nginx.

Write Nginx container orchestration configuration


services:
  nginx:
    image: ${NGX_IMAGE}
    restart: always
    networks:
      - traefik
    expose:
      - 80
    volumes:
      - ./conf:/etc/nginx/conf.d
      - ./logs:/var/log/nginx
      - ./wordpress:/var/www/html
    depends_on:
      - wp
    labels:
      - "traefik.enable=true"
      - "traefik.frontend.rule=Host:${NGX_DOMAINS}"
      - "traefik.frontend.entryPoints=https,http"

Because Nginx takes over the ingress traffic, the task of registering service discovery on Traefik is none other than it. Here, we use the labels field to add some commands supported by Traefik to perform service registration.

In addition, the files that need to mount WordPress to the file system are also mounted to Nginx, just like this.


- ./wordpress:/var/www/html

Finally, simply declare an Nginx configuration to describe how to call WordPress.

Create a simple Nginx service configuration and
save the following content as wp.conf.

server {
    listen 80;

    root /var/www/html;
    index index.php;

    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;

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

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass wp:9000;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
    }
}

Writing environment files
is the same as the previous article. For maintainability, we have separated environment configuration information and application orchestration files.


WP_IMAGE=wordpress:5.1.1-php7.1-fpm-alpine
WP_DB_PREFIX=wp
WP_HOST=wp

NGX_IMAGE=nginx:1.15.10-alpine
NGX_DOMAINS=wp.lab.com,wp.lab.io

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

After saving the above file as .env, we can start the application.

Start the complete application
Before starting the application, we will summarize the orchestration files that have just been modified.


version: '3'

services:

  wp:
    image: ${WP_IMAGE}
    restart: always
    container_name: ${WP_HOST}
    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:
      - ./wordpress:/var/www/html

  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

  nginx:
    image: ${NGX_IMAGE}
    restart: always
    networks:
      - traefik
    expose:
      - 80
    volumes:
      - ./conf:/etc/nginx/conf.d
      - ./logs:/var/log/nginx
      - ./wordpress:/var/www/html
    depends_on:
      - wp
    labels:
      - "traefik.enable=true"
      - "traefik.frontend.rule=Host:${NGX_DOMAINS}"
      - "traefik.frontend.entryPoints=https,http"

networks:
  traefik:
    external: true

After saving the file as docker-compose.yml, we use docker-compose up to start the application and verify that the application is normal.

When you see the log below, your application can be accessed.


wp-db      | 2019-04-07  3:42:58 0 [Note] Server socket created on IP: '::'.
wp-db      | 2019-04-07  3:42:58 0 [Warning] 'proxies_priv' entry '@% root@2ed5be12f731' ignored in --skip-name-resolve mode.
wp-db      | 2019-04-07  3:42:58 0 [Note] Reading of all Master_info entries succeded
wp-db      | 2019-04-07  3:42:58 0 [Note] Added new Master_info '' to hash table
wp-db      | 2019-04-07  3:42:58 0 [Note] mysqld: ready for connections.
wp-db      | Version: '10.3.14-MariaDB-1:10.3.14+maria~bionic'  socket: '/var/run/mysqld/mysqld.sock'  port: 3306  mariadb.org binary distribution
wp         | [07-Apr-2019 03:42:59] NOTICE: fpm is running, pid 1
wp         | [07-Apr-2019 03:42:59] NOTICE: ready to handle connections

It is still using a browser to access the domain name configured in .env. It is still a familiar operation, fill in the information, and perform the famous "three-minute" installation. After that, a new site is born.

Use Docker and Traefik to build WordPress (Nginx)

Some additional tips. When
we use Compose to start the application, if it is the first debugging, it is recommended to execute:


docker-compose up

Because you can directly see the actual running log of the application in the terminal, if you make a mistake, you can press the CTRL+C key combination to interrupt execution and return to debugging.

When your application is fully ready and we need to run this service stably for a long time, when using Compose again, you can add a -d parameter to let the application execute in daemon mode.


docker-compose up -d

At this time, the application will execute silently in the background, and will not output any valuable information to the terminal. If the application is abnormal, we need to debug. What should we do if we want to see the application log? Just execute the following command.


docker-compose logs -f

If you find an application execution error, use docker-compose down to end the application, adjust the orchestration configuration file, and restart the application with docker-compose up without parameters. After the application is completely ready, add the daemon parameter.

Finally,
thank you all the students who continue to pay attention to and encourage me to write. It is your attention that allows me to avoid repeating a lot of content in the writing process, and the writing becomes more efficient.

Common WordPress is mostly deployed online and needs to rely on MySQL and Mariadb to run. It is not suitable for "carrying" or running on low-configuration machines.

The next article will talk about "How to create a portable WordPress".

Guess you like

Origin blog.51cto.com/15054039/2621788