使用Traefik 2在Docker中安装Wordpress

时间:2020-07-08 12:05:48

标签: wordpress docker traefik

我遵循了这些Examples在服务器上设置traefik。我尝试按照其他一些指南在docker中设置我的Wordpress-Site,并使其可以通过Traefik进行访问,而无需使用nginx。

目前,我有一个whoami容器在whoami.mydomain.com上运行,效果很好。 但是访问blog.mydomain.com只会导致502 Bad Gateway,您能否确定我的错误是否在traefik标签内,或者是否与wordpress相关?

我的文件如下:

traefik-compose.yml:

version: "3.7"

services:
  traefik:
    image: "traefik:v2.1"
    container_name: "traefik"
    hostname: "traefik"
    env_file:
      - .env
    ports:
      - "80:80"
      - "443:443"
      - "8080:8080"
    volumes:
      - "/var/run/docker.sock:/var/run/docker.sock:ro"
      - "./traefik.yml:/traefik.yml:ro"
      - "./acme.json:/acme.json"
    labels:
      - "traefik.enable=true"
      ## DNS CHALLENGE
      - "traefik.http.routers.traefik.tls.certresolver=lets-encr"
      - "traefik.http.routers.traefik.tls.domains[0].main=*.$MY_DOMAIN"
      - "traefik.http.routers.traefik.tls.domains[0].sans=$MY_DOMAIN"
      ## HTTP REDIRECT
      - "traefik.http.middlewares.redirect-to-https.redirectscheme.scheme=https"
      - "traefik.http.routers.redirect-https.rule=hostregexp(`{host:.+}`)"
      - "traefik.http.routers.redirect-https.entrypoints=web"
      - "traefik.http.routers.redirect-https.middlewares=redirect-to-https"

networks:
  default:
    external:
      name: $DEFAULT_NETWORK

traefik.yml:

## STATIC CONFIGURATION
log:
  level: INFO

api:
  insecure: true
  dashboard: true

entryPoints:
  web:
    address: ":80"
  websecure:
    address: ":443"

providers:
  docker:
    endpoint: "unix:///var/run/docker.sock"
    exposedByDefault: false
 
certificatesResolvers:
  lets-encr:
    acme:
      caServer: https://acme-staging-v02.api.letsencrypt.org/directory
      storage: acme.json
      email: mymail@example.com
      dnsChallenge:
        provider: netcup

whoami-docker-compose.yml

version: "3.7"

services:
  whoami:
    image: "containous/whoami"
    container_name: "whoami"
    hostname: "whoami"
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.whoami.entrypoints=websecure"
      - "traefik.http.routers.whoami.rule=Host(`whoami.$MY_DOMAIN`)"
      - "traefik.http.routers.whoami.tls.certresolver=lets-encr"

networks:
  default:
    external:
      name: $DEFAULT_NETWORK

wordpress-docker-compose.yml

version: "3.7"

services:
  db:
    image: mysql:8.0
    container_name: db-blog
    restart: unless-stopped
    environment:
      - MYSQL_DATABASE=wordpress-blog
      - MYSQL_ROOT_PASSWORD=MYROOTPASSWORD
      - MYSQL_USER=blog-user
      - MYSQL_PASSWORD=MYPASSWORD
    volumes:
      - dbdata:/var/lib/mysql
    command: "--default-authentication-plugin=mysql_native_password"
    networks:
      - app-network

  wordpress:
    depends_on:
      - db
    image: wordpress:5.4.1-fpm-alpine
    container_name: wordpress-blog
    hostname: wordpress-blog
    restart: unless-stopped
    environment:
      - WORDPRESS_DB_HOST=db:3306
      - WORDPRESS_DB_USER=blog-user
      - WORDPRESS_DB_PASSWORD=MYPASSWORD
      - WORDPRESS_DB_NAME=wordpress-blog
    volumes:
      - wordpress:/var/www/html
      - ./uploads.ini:/usr/local/etc/php/conf.d/uploads.ini
    networks:
      - app-network
      - default
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.blog.entrypoints=websecure"
      - "traefik.http.routers.blog.rule=Host(`blog.$MY_DOMAIN`)"
      - "traefik.http.routers.blog.tls.certresolver=lets-encr"

volumes:
  wordpress:
  dbdata:
  
networks:
  default:
    external:
      name: $DEFAULT_NETWORK
  app-network:
    driver: bridge 

2 个答案:

答案 0 :(得分:0)

似乎wordpress正在8080上监听,因此您需要在wordpress容器中添加标签:

     - "traefik.http.services.blog.loadbalancer.server.port=8080"

此外,您不需要在traefik容器中公开该端口,因为您正在使用websecure入口点,因此建议删除此行

     - "8080:8080"

来自traefik容器。

答案 1 :(得分:0)

所以我自己弄清楚了。主要问题是完全不同的东西。我的用于wordpress的docker-compose位于名为mydomain.com的目录中。当目录包含.

时,traefik似乎有问题

将目录更改为仅mydomain后,它就像一个超级按钮。我还清理了wordpress-docker-compose.yml,以供任何有疑问的人使用。

version: '3'
 
services:
   wordpress:
     container_name: wordpress-blog
     depends_on:
       - db
     image: wordpress:latest
     volumes:
       - wp_data:/var/www/html
       - ./uploads.ini:/usr/local/etc/php/conf.d/uploads.ini
     restart: on-failure
     environment:
       WORDPRESS_DB_HOST: db:3306
       WORDPRESS_DB_USER: wordpress
       WORDPRESS_DB_PASSWORD: MYSUPERSECUREPASSWORD
     labels:
       - "traefik.enable=true"
       - "traefik.http.routers.blog.entrypoints=websecure"
       - "traefik.http.routers.blog.rule=Host(`$MY_DOMAIN`)"
       - "traefik.http.routers.blog.tls.certresolver=lets-encr"

   db:
     container_name: db-blog
     image: mariadb
     volumes:
       - db_data:/var/lib/mysql
     restart: on-failure
     environment:
       MYSQL_ROOT_PASSWORD: MYOTHERSUPERSECUREPASSWORD
       MYSQL_DATABASE: wordpress
       MYSQL_USER: wordpress
       MYSQL_PASSWORD: MYSUPERSECUREPASSWORD
volumes:
    wp_data:
    db_data:

networks:
  default:
    external:
      name: $DEFAULT_NETWORK