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

Posted

技术标签:

【中文标题】Puppet Docker PHP Apache 设置 - 无效命令“RewriteRule”,可能拼写错误或已定义【英文标题】:Puppet Docker PHP Apache setup - Invalid command 'RewriteRule', perhaps misspelled or defined 【发布时间】:2021-04-29 04:38:40 【问题描述】:

我正在使用 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,index.html) found, 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://***.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

【问题讨论】:

如果您需要在Dockerfile 中添加RUN a2enmod rewrite,那么这可能在多阶段构建中最容易实现,您可以通过 Puppet 中的新 Dockerfile 管理生成的图像.如果没有大量的额外编码,在 Puppet(或其他)中修改官方存储库中图像的 Dockerfile 可能是不可能的。 【参考方案1】:

我用下面的配置重建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' ],
  

【讨论】:

以上是关于Puppet Docker PHP Apache 设置 - 无效命令“RewriteRule”,可能拼写错误或已定义的主要内容,如果未能解决你的问题,请参考以下文章

使用 Vagrant + Docker 在本地开发 Puppet

Docker php apache权限被拒绝

docker搭建apache+php+mysql

如何使用 Docker 连接 php-apache 和 MySQL?

在Docker下搭建Apache+PHP+mysql环境的过程记录

Docker PHP-APACHE 容器全局设置 ServerName 指令