Puppet Docker PHP Apache 设置 - 命令“RewriteRule”无效,可能拼写错误或定义错误

问题描述

我正在使用 puppet docker (https://forge.puppet.com/modules/puppetlabs/docker) 并使用 PHP docker 镜像 (https://hub.docker.com/layers/php/library/php/7.2.34-apache/images/sha256-77e5a326252f951aa557f48829973f67e8efde9c52f81ee4e4a5473a59a217d9?context=explore)

设置 PHP-Apache 网站

PHP 脚本工作正常,但是当我在文件夹中添加 .htaccess 文件时,它抛出错误

[Mon Jan 25 09:52:12.078604 2021] [mpm_prefork:notice] [pid 1] AH00163: Apache/2.4.38 (Debian) PHP/7.2.34 configured -- resuming normal operations
[Mon Jan 25 09:52:12.078766 2021] [core:notice] [pid 1] AH00094: Command line: 'apache2 -D FOREGROUND'
[Mon Jan 25 09:52:32.734378 2021] [autoindex:error] [pid 16] [client 172.17.0.1:50756] AH01276: Cannot serve directory /var/www/html/: No matching DirectoryIndex (index.PHP,index.html) found,and server-generated directory index forbidden by Options directive
172.17.0.1 - - [25/Jan/2021:09:52:32 +0000] "GET / HTTP/1.1" 403 493 "-" "Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Firefox/68.0"
172.17.0.1 - - [25/Jan/2021:09:52:32 +0000] "GET /favicon.ico HTTP/1.1" 404 489 "-" "Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Firefox/68.0"
[Mon Jan 25 09:56:33.265445 2021] [autoindex:error] [pid 18] [client 172.17.0.1:50830] AH01276: Cannot serve directory /var/www/html/: No matching DirectoryIndex (index.PHP,and server-generated directory index forbidden by Options directive
172.17.0.1 - - [25/Jan/2021:09:56:33 +0000] "GET / HTTP/1.1" 403 493 "-" "Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Firefox/68.0"
[Mon Jan 25 09:56:52.186711 2021] [core:alert] [pid 17] [client 172.17.0.1:50854] /var/www/html/packageContent/.htaccess: Invalid command 'RewriteRule',perhaps misspelled or defined by a module not included in the server configuration

我相信我需要 RUN a2enmod rewrite 就像这里 (https://stackoverflow.com/a/38064289/376702)

我不知道如何使用 puppet docker(https://forge.puppet.com/modules/puppetlabs/docker) 进行设置。

有人可以指导我吗?这是我的傀儡代码

 docker::image { 'docker.io/PHP':
    image_tag => '7.2.34-apache'
  }

  docker::run { 'test123.com':
    image            => 'docker.io/PHP:7.2.34-apache',command          => 'apache2-foreground',expose           => ['80'],ports            => ['8101:80'],volumes          => ['/var/www/test123.com:/var/www/html'],hostname         => 'test123.com',restart_service  => true,before_stop      => 'echo "So Long,and Thanks for All the Fish"',before_start     => 'echo "Run this on the host before starting the Docker container"',after_stop       => 'echo "container has stopped"',after_start      => 'echo "container has started"',extra_parameters => [ '--restart=always' ],}

我在 Linux RedHat 中运行 docker

参考: .htaccess: Invalid command 'RewriteEngine',perhaps misspelled or defined by a module not included in the server configuration

解决方法

我用以下配置重建PHP apache官方镜像

# Dockerfile

FROM docker.io/php:7.2.34-apache
RUN a2enmod rewrite

pp puppet类中的puppet代码变化

      docker::image { 'php-apache7234':
        docker_file => '/tmp/Dockerfile',subscribe   => File['/tmp/Dockerfile'],}
    
      file { '/tmp/Dockerfile':
        ensure => file,source => 'puppet:///modules/mydocker/php_apache/Dockerfile',}

  docker::run { 'test123.com':
    image            => 'php-apache7234',command          => 'apache2-foreground',expose           => ['80'],ports            => ['8101:80'],volumes          => ['/var/www/test123.com:/var/www/html'],hostname         => 'test123.com',restart_service  => true,before_stop      => 'echo "So Long,and Thanks for All the Fish"',before_start     => 'echo "Run this on the host before starting the Docker container"',after_stop       => 'echo "container has stopped"',after_start      => 'echo "container has started"',extra_parameters => [ '--restart=always' ],}