【问题标题】:Error setting Wordpress site in Docker container environment在 Docker 容器环境中设置 Wordpress 站点时出错
【发布时间】:2021-04-17 17:35:42
【问题描述】:

我想设置一个包含三个容器的 docker 环境:一个用于 mysql 服务器绑定在端口 3306 上,一个用于 apache/letsencrypt 绑定在端口 80 和 443 上,一个用于托管一些 wordpress 站点,绑定在端口 8073 上。

我已经配置了这个 docker-compose 文件,并且对于每个容器都有一个 Dockerfile 来安装相应的图像:

version: "3.3"
services:
    mysql-server:
        build: 'mysql-server'
        container_name: mysql-server
        volumes:
            - db_volumes:/var/lib/mysql
        environment:
            MYSQL_ROOT_PASSWORD: test
        ports:
            - "3306:3306"
    apache-letsencrypt:
        build: 'apache-letsencrypt'
        container_name: apache-letsencrypt
        environment:
            WEBMASTER_MAIL: "example@example.it"
        volumes:
            - /etc/letsencrypt:/etc/letsencrypt
            - vhosts_volumes:/etc/apache2/sites-available
            - log_volumes:/var/log/apache2
        ports:
            - "80:80"
            - "443:443"
        network_mode: host
    wordpress_php7.2:
        build: 'php7.2'
        container_name: wordpress_php7.2
        environment:
            DB_SERVER: mysql-server
        depends_on:
            - mysql-server
        volumes:
            - /var/www/wordpress_php7.2:/var/www
            - vhosts_volumes:/etc/apache2/sites-available
            - log_volumes:/var/log/apache2
        ports:
            - "8073:80"
volumes:
    db_volumes:
        driver: local
        driver_opts:
            type: 'none'
            o: 'bind'
            device: '/var/www/databases'
    vhosts_volumes:
        driver: local
        driver_opts:
            type: 'none'
            o: 'bind'
            device: '/etc/apache2/sites-available'
    log_volumes:
        driver: local
        driver_opts:
            type: 'none'
            o: 'bind'
            device: '/var/log/apache2'

创建容器后,我将 wordpress 站点放在 /var/www/wordpress_php7.2/www.example.com 并在 mysql 容器中设置数据库。

此外,我在 apache-letsencrypt 容器中成功创建了letsencrypt证书。 所以我必须设置虚拟主机并为一个站点创建三个虚拟主机: 我放在 wordpress 容器中的一个绑定到容器内的端口 80:

<VirtualHost *:80>
 ServerName www.example.com
 ServerAlias example.com
 ServerAdmin webmaster@localhost
 DocumentRoot /var/www/example.com

 <Directory /var/www/example.com>
 Options Indexes FollowSymLinks
 AllowOverride All
 </Directory>


 ErrorLog /var/log/apache2/example.com.error.log
 CustomLog /var/log/apache2/example.com.access.log combined

</VirtualHost>

另外两个在 apache-letsencrypt 容器中的虚拟主机绑定在端口 80 和 443:

<VirtualHost *:80>
  ServerName www.example.com

  ## Redirect rules
  Redirect permanent / https://www.example.com/

  ## Server aliases
  ServerAlias example.com
</VirtualHost>

<VirtualHost *:443>
        ServerName www.example.com
        ServerAlias example.com

        SSLEngine On
        SSLCertificateFile      "/etc/letsencrypt/live/www.example.com/cert.pem"
        SSLCertificateKeyFile   "/etc/letsencrypt/live/www.example.com/privkey.pem"
        SSLCertificateChainFile "/etc/letsencrypt/live/www.example.com/fullchain.pem"
        SSLCACertificatePath    "/etc/ssl/certs"

        SSLProxyEngine on
        ProxyPass / http://www.example.com:8073/
        ProxyPassReverse / http://www.example.com:8073/
        ProxyRequests Off
</VirtualHost>

最后,我在 docker 网络中传递请求并在 /etc/hosts 路径中的 apache 容器中进行设置:

172.17.0.1 www.example.com

在 wp_option 表中的数据库站点内部更改站点的 https 路径 (https://www.example.com) 后,我收到错误 too_many_redirects。

我进行了调试,如果我在加载站点之前回显某些内容并在 index.php 中退出,则一切正常,所以我认为代码中的某些进程无法正常工作。

怎么了?

【问题讨论】:

    标签: wordpress docker apache


    【解决方案1】:

    我为 wordpress 网站找到了这个解决方案:

    在绑定到端口 443 的虚拟主机中插入此指令:

    RequestHeader set X-Forwarded-Proto https
    

    识别客户端连接代理使用的协议(文档 -> https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-Forwarded-Proto)。

    之后我在主题的functions.php文件的第一行插入了这段代码:

    remove_action('template_redirect', 'redirect_canonical');
    

    避免模板重定向。

    现在一切似乎都正常

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-10-22
      • 2017-05-11
      • 2022-10-05
      • 1970-01-01
      • 1970-01-01
      • 2015-04-08
      • 1970-01-01
      相关资源
      最近更新 更多