Puppet Class: apache

Inherits:
::apache::params
Defined in:
manifests/init.pp

Overview

Class: apache

This class installs Apache

Parameters:

Actions:

- Install Apache
- Manage Apache service

Requires:

Sample Usage:

Parameters:

  • apache_name (Any) (defaults to: $::apache::params::apache_name)
  • apache_port (Any) (defaults to: $::apache::params::apache_port)
  • service_name (Any) (defaults to: $::apache::params::service_name)
  • default_mods (Any) (defaults to: true)
  • default_vhost (Boolean) (defaults to: true)
  • default_charset (Any) (defaults to: undef)
  • default_confd_files (Boolean) (defaults to: true)
  • default_ssl_vhost (Boolean) (defaults to: false)
  • default_ssl_cert (Any) (defaults to: $::apache::params::default_ssl_cert)
  • default_ssl_key (Any) (defaults to: $::apache::params::default_ssl_key)
  • default_ssl_chain (Any) (defaults to: undef)
  • default_ssl_ca (Any) (defaults to: undef)
  • default_ssl_crl_path (Any) (defaults to: undef)
  • default_ssl_crl (Any) (defaults to: undef)
  • default_ssl_crl_check (Any) (defaults to: undef)
  • default_type (Any) (defaults to: 'none')
  • dev_packages (Any) (defaults to: $::apache::params::dev_packages)
  • ip (Any) (defaults to: undef)
  • service_enable (Boolean) (defaults to: true)
  • service_manage (Boolean) (defaults to: true)
  • service_ensure (Any) (defaults to: 'running')
  • service_restart (Any) (defaults to: undef)
  • purge_configs (Any) (defaults to: true)
  • purge_vhost_dir (Any) (defaults to: undef)
  • purge_vdir (Any) (defaults to: false)
  • serveradmin (Any) (defaults to: 'root@localhost')
  • sendfile (Enum['On', 'Off', 'on', 'off']) (defaults to: 'On')
  • error_documents (Any) (defaults to: false)
  • timeout (Any) (defaults to: '120')
  • httpd_dir (Any) (defaults to: $::apache::params::httpd_dir)
  • server_root (Any) (defaults to: $::apache::params::server_root)
  • conf_dir (Any) (defaults to: $::apache::params::conf_dir)
  • confd_dir (Any) (defaults to: $::apache::params::confd_dir)
  • vhost_dir (Any) (defaults to: $::apache::params::vhost_dir)
  • vhost_enable_dir (Any) (defaults to: $::apache::params::vhost_enable_dir)
  • mod_packages (Any) (defaults to: $::apache::params::mod_packages)
  • vhost_include_pattern (Any) (defaults to: $::apache::params::vhost_include_pattern)
  • mod_dir (Any) (defaults to: $::apache::params::mod_dir)
  • mod_enable_dir (Any) (defaults to: $::apache::params::mod_enable_dir)
  • mpm_module (Any) (defaults to: $::apache::params::mpm_module)
  • lib_path (Any) (defaults to: $::apache::params::lib_path)
  • conf_template (Any) (defaults to: $::apache::params::conf_template)
  • servername (Any) (defaults to: $::apache::params::servername)
  • pidfile (Any) (defaults to: $::apache::params::pidfile)
  • rewrite_lock (Optional[Stdlib::Absolutepath]) (defaults to: undef)
  • manage_user (Boolean) (defaults to: true)
  • manage_group (Boolean) (defaults to: true)
  • user (Any) (defaults to: $::apache::params::user)
  • group (Any) (defaults to: $::apache::params::group)
  • http_protocol_options (Any) (defaults to: $::apache::params::http_protocol_options)
  • supplementary_groups (Any) (defaults to: [])
  • keepalive (Any) (defaults to: $::apache::params::keepalive)
  • keepalive_timeout (Any) (defaults to: $::apache::params::keepalive_timeout)
  • max_keepalive_requests (Any) (defaults to: $::apache::params::max_keepalive_requests)
  • limitreqfieldsize (Any) (defaults to: '8190')
  • logroot (Any) (defaults to: $::apache::params::logroot)
  • logroot_mode (Any) (defaults to: $::apache::params::logroot_mode)
  • log_level (Any) (defaults to: $::apache::params::log_level)
  • log_formats (Any) (defaults to: {})
  • ssl_file (Any) (defaults to: undef)
  • ports_file (Any) (defaults to: $::apache::params::ports_file)
  • docroot (Any) (defaults to: $::apache::params::docroot)
  • apache_version (Any) (defaults to: $::apache::version::default)
  • server_tokens (Any) (defaults to: 'OS')
  • server_signature (Any) (defaults to: 'On')
  • trace_enable (Any) (defaults to: 'On')
  • allow_encoded_slashes (Optional[Enum['on', 'off', 'nodecode']]) (defaults to: undef)
  • file_e_tag (Any) (defaults to: undef)
  • package_ensure (Any) (defaults to: 'installed')
  • use_optional_includes (Boolean) (defaults to: $::apache::params::use_optional_includes)
  • use_systemd (Any) (defaults to: $::apache::params::use_systemd)
  • mime_types_additional (Any) (defaults to: $::apache::params::mime_types_additional)
  • file_mode (Any) (defaults to: $::apache::params::file_mode)
  • root_directory_options (Any) (defaults to: $::apache::params::root_directory_options)
  • root_directory_secured (Boolean) (defaults to: false)
  • error_log (Any) (defaults to: $::apache::params::error_log)
  • scriptalias (Any) (defaults to: $::apache::params::scriptalias)
  • access_log_file (Any) (defaults to: $::apache::params::access_log_file)


15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
# File 'manifests/init.pp', line 15

class apache (
  $apache_name                                                   = $::apache::params::apache_name,
  $apache_port							                         = $::apache::params::apache_port,
  $service_name                                                  = $::apache::params::service_name,
  $default_mods                                                  = true,
  Boolean $default_vhost                                         = true,
  $default_charset                                               = undef,
  Boolean $default_confd_files                                   = true,
  Boolean $default_ssl_vhost                                     = false,
  $default_ssl_cert                                              = $::apache::params::default_ssl_cert,
  $default_ssl_key                                               = $::apache::params::default_ssl_key,
  $default_ssl_chain                                             = undef,
  $default_ssl_ca                                                = undef,
  $default_ssl_crl_path                                          = undef,
  $default_ssl_crl                                               = undef,
  $default_ssl_crl_check                                         = undef,
  $default_type                                                  = 'none',
  $dev_packages                                                  = $::apache::params::dev_packages,
  $ip                                                            = undef,
  Boolean $service_enable                                        = true,
  Boolean $service_manage                                        = true,
  $service_ensure                                                = 'running',
  $service_restart                                               = undef,
  $purge_configs                                                 = true,
  $purge_vhost_dir                                               = undef,
  $purge_vdir                                                    = false,
  $serveradmin                                                   = 'root@localhost',
  Enum['On', 'Off', 'on', 'off'] $sendfile                       = 'On',
  $error_documents                                               = false,
  $timeout                                                       = '120',
  $httpd_dir                                                     = $::apache::params::httpd_dir,
  $server_root                                                   = $::apache::params::server_root,
  $conf_dir                                                      = $::apache::params::conf_dir,
  $confd_dir                                                     = $::apache::params::confd_dir,
  $vhost_dir                                                     = $::apache::params::vhost_dir,
  $vhost_enable_dir                                              = $::apache::params::vhost_enable_dir,
  $mod_packages                                                  = $::apache::params::mod_packages,
  $vhost_include_pattern                                         = $::apache::params::vhost_include_pattern,
  $mod_dir                                                       = $::apache::params::mod_dir,
  $mod_enable_dir                                                = $::apache::params::mod_enable_dir,
  $mpm_module                                                    = $::apache::params::mpm_module,
  $lib_path                                                      = $::apache::params::lib_path,
  $conf_template                                                 = $::apache::params::conf_template,
  $servername                                                    = $::apache::params::servername,
  $pidfile                                                       = $::apache::params::pidfile,
  Optional[Stdlib::Absolutepath] $rewrite_lock                   = undef,
  Boolean $manage_user                                           = true,
  Boolean $manage_group                                          = true,
  $user                                                          = $::apache::params::user,
  $group                                                         = $::apache::params::group,
  $http_protocol_options                                         = $::apache::params::http_protocol_options,
  $supplementary_groups                                          = [],
  $keepalive                                                     = $::apache::params::keepalive,
  $keepalive_timeout                                             = $::apache::params::keepalive_timeout,
  $max_keepalive_requests                                        = $::apache::params::max_keepalive_requests,
  $limitreqfieldsize                                             = '8190',
  $logroot                                                       = $::apache::params::logroot,
  $logroot_mode                                                  = $::apache::params::logroot_mode,
  $log_level                                                     = $::apache::params::log_level,
  $log_formats                                                   = {},
  $ssl_file                                                      = undef,
  $ports_file                                                    = $::apache::params::ports_file,
  $docroot                                                       = $::apache::params::docroot,
  $apache_version                                                = $::apache::version::default,
  $server_tokens                                                 = 'OS',
  $server_signature                                              = 'On',
  $trace_enable                                                  = 'On',
  Optional[Enum['on', 'off', 'nodecode']] $allow_encoded_slashes = undef,
  $file_e_tag                                                    = undef,
  $package_ensure                                                = 'installed',
  Boolean $use_optional_includes                                 = $::apache::params::use_optional_includes,
  $use_systemd                                                   = $::apache::params::use_systemd,
  $mime_types_additional                                         = $::apache::params::mime_types_additional,
  $file_mode                                                     = $::apache::params::file_mode,
  $root_directory_options                                        = $::apache::params::root_directory_options,
  Boolean $root_directory_secured                                = false,
  $error_log                                                     = $::apache::params::error_log,
  $scriptalias                                                   = $::apache::params::scriptalias,
  $access_log_file                                               = $::apache::params::access_log_file,
) inherits ::apache::params {

  $valid_mpms_re = $apache_version ? {
    '2.4'   => '(event|itk|peruser|prefork|worker)',
    default => '(event|itk|prefork|worker)'
  }

  if $::osfamily == 'RedHat' and $::apache::version::distrelease == '7' {
    # On redhat 7 the ssl.conf lives in /etc/httpd/conf.d (the confd_dir)
    # when all other module configs live in /etc/httpd/conf.modules.d (the
    # mod_dir). On all other platforms and versions, ssl.conf lives in the
    # mod_dir. This should maintain the expected location of ssl.conf
    $_ssl_file = $ssl_file ? {
      undef   => "${apache::confd_dir}/ssl.conf",
      default =>  $ssl_file
    }
  } else {
    $_ssl_file = $ssl_file ? {
      undef   => "${apache::mod_dir}/ssl.conf",
      default =>  $ssl_file
    }
  }
 
  #ADD THE HTML FILE 
  #CHECK FOR THE EXISTENCE HTML DIRECTORY, IF NOT THERE THEN CREATE THE DIRECTORY
  if $::osfamily == 'CentOS' {
    #CREATE THE HTML FILE FOR HTTPD WEB SERVER
    file { '/var/www/html/index.html':
      ensure  => 'present',
      replace => 'yes', # this is the important property
      content =>  template('apache/index.html'),
      mode    => '0755',
    }
  } 
  # CONFIGURING THE FIREWALL
  #The web server requires an open port so people can access the pages hosted on our web server. 
  #The open problem is that different versions of Red Hat Enterprise Linux uses different methods for controlling the firewall. 
  #For Red Hat Enterprise Linux 6 and below, we use iptables. 
  #For Red Hat Enterprise Linux 7, we use firewalld
  #Use the operatingsystemmajrelease fact to determine whether the operating system is Red Hat Enterprise Linux 6 or 7.
  if $::osfamily == 'CentOS' and $::apache::version::distrelease == '7' {
    notify{"!##%*$%*CENTOS ":}
    #RUNS FIREWALL-CMD TO ADD A PERMANENT FIREWALL RULE
    exec { 'firewall-cmd':
      command => "firewall-cmd --zone=public --add-port=${apache_port}/tcp --permanent",
      path => ['/usr/bin'],
      logoutput => true,
      notify => Service["firewalld"],  #checks our firewall for any changes. If it changed, Puppet restarts the service
    }

    #RESTART THE FIREWALLD SERVICE
    service { 'firewalld':
      ensure => running,
    }
  }elsif $::osfamily == 'CentOS' and $::apache::version::distrelease == '6' {
    #RUNS IPTABLES-CMD TO ADD A PERMANENT FIREWALL RULE 
    exec { 'iptables':
      command => "iptables -I INPUT 1 -p tcp -m multiport --ports ${apache_port} -m comment --comment 'Custom HTTP Web Host' -j ACCEPT && iptables-save > /etc/sysconfig/iptables",
      path => ['/sbin'],
      logoutput => true,
      notify => Service["iptables"],  #checks our firewall for any changes. If it changed, Puppet restarts the service
    }

    #RESTART THE FIREWALLD SERVICE
    service { 'iptables':
      ensure => running,
    }
  }

  if $::osfamily == 'CentOS' {
    # CONFIGURING SELINUX
    #SELinux restricts non-standard access to the HTTP server by default. 
    #If we define a custom port, we need to add configuration that allows SELinux to grant access.
    #Puppet contains resource types to manage some SELinux functions, such as Booleans and modules. 
    #However, we need to execute the semanage command to manage port settings. 
    #This tool is a part of the policycoreutils-python package, which is not installed on Red Hat Enterprise Linux systems by default.

    #ADD THE CUSTOM PORT TO THE LIST OF TCP PORTS, THAT APACHE IS ALLOWED TO LISTEN ON
    exec { 'semanage-port':
      command => "semanage port -a -t http_port_t -p tcp ${apache_port}",
      path => "/usr/sbin",
      require => Package['policycoreutils-python'],  #makes sure the policycoreutils-python is installed prior to executing the command
      logoutput => true,
      returns => [0,1],
    }
  
    # INSTALL SEMANAGE PACKAGE TO ADD THE PORT IN SELINUX
    package { 'policycoreutils-python':
      ensure => installed,
    }
  }

  if $mpm_module and $mpm_module != 'false' { # lint:ignore:quoted_booleans
    assert_type(Pattern[$valid_mpms_re], $mpm_module)
  }

  # NOTE: on FreeBSD it's mpm module's responsibility to install httpd package.
  # NOTE: the same strategy may be introduced for other OSes. For this, you
  # should delete the 'if' block below and modify all MPM modules' manifests
  # such that they include apache::package class (currently event.pp, itk.pp,
  # peruser.pp, prefork.pp, worker.pp).
  if $::osfamily != 'FreeBSD' {
    package { 'httpd':
      ensure => $package_ensure,
      name   => $apache_name,
      notify => Class['Apache::Service'],
    }
  }

  # declare the web server user and group
  # Note: requiring the package means the package ought to create them and not puppet
  if $manage_user {
    user { $user:
      ensure  => present,
      gid     => $group,
      groups  => $supplementary_groups,
      require => Package['httpd'],
    }
  }
  if $manage_group {
    group { $group:
      ensure  => present,
      require => Package['httpd'],
    }
  }

  validate_apache_log_level($log_level)

  class { '::apache::service':
    service_name    => $service_name,
    service_enable  => $service_enable,
    service_manage  => $service_manage,
    service_ensure  => $service_ensure,
    service_restart => $service_restart,
  }

  # Deprecated backwards-compatibility
  if $purge_vdir {
    warning('Class[\'apache\'] parameter purge_vdir is deprecated in favor of purge_configs')
    $purge_confd = $purge_vdir
  } else {
    $purge_confd = $purge_configs
  }

  # Set purge vhostd appropriately
  if $purge_vhost_dir == undef {
    $purge_vhostd = $purge_confd
  } else {
    $purge_vhostd = $purge_vhost_dir
  }

  Exec {
    path => '/bin:/sbin:/usr/bin:/usr/sbin',
  }

  exec { "mkdir ${confd_dir}":
    creates => $confd_dir,
    require => Package['httpd'],
  }
  file { $confd_dir:
    ensure  => directory,
    recurse => true,
    purge   => $purge_confd,
    force   => $purge_confd,
    notify  => Class['Apache::Service'],
    require => Package['httpd'],
  }

  if ! defined(File[$mod_dir]) {
    exec { "mkdir ${mod_dir}":
      creates => $mod_dir,
      require => Package['httpd'],
    }
    # Don't purge available modules if an enable dir is used
    $purge_mod_dir = $purge_configs and !$mod_enable_dir
    file { $mod_dir:
      ensure  => directory,
      recurse => true,
      purge   => $purge_mod_dir,
      notify  => Class['Apache::Service'],
      require => Package['httpd'],
      before  => Anchor['::apache::modules_set_up'],
    }
  }

  if $mod_enable_dir and ! defined(File[$mod_enable_dir]) {
    $mod_load_dir = $mod_enable_dir
    exec { "mkdir ${mod_enable_dir}":
      creates => $mod_enable_dir,
      require => Package['httpd'],
    }
    file { $mod_enable_dir:
      ensure  => directory,
      recurse => true,
      purge   => $purge_configs,
      notify  => Class['Apache::Service'],
      require => Package['httpd'],
    }
  } else {
    $mod_load_dir = $mod_dir
  }

  if ! defined(File[$vhost_dir]) {
    exec { "mkdir ${vhost_dir}":
      creates => $vhost_dir,
      require => Package['httpd'],
    }
    file { $vhost_dir:
      ensure  => directory,
      recurse => true,
      purge   => $purge_vhostd,
      notify  => Class['Apache::Service'],
      require => Package['httpd'],
    }
  }

  if $vhost_enable_dir and ! defined(File[$vhost_enable_dir]) {
    $vhost_load_dir = $vhost_enable_dir
    exec { "mkdir ${vhost_load_dir}":
      creates => $vhost_load_dir,
      require => Package['httpd'],
    }
    file { $vhost_enable_dir:
      ensure  => directory,
      recurse => true,
      purge   => $purge_vhostd,
      notify  => Class['Apache::Service'],
      require => Package['httpd'],
    }
  } else {
    $vhost_load_dir = $vhost_dir
  }

  concat { $ports_file:
    ensure  => present,
    owner   => 'root',
    group   => $::apache::params::root_group,
    mode    => $::apache::file_mode,
    notify  => Class['Apache::Service'],
    require => Package['httpd'],
  }
  concat::fragment { 'Apache ports header':
    target  => $ports_file,
    content => template('apache/ports_header.erb'),
  }

  if $::apache::conf_dir and $::apache::params::conf_file {
    if $::osfamily == 'gentoo' {
      $error_documents_path = '/usr/share/apache2/error'
      if $default_mods =~ Array {
        if versioncmp($apache_version, '2.4') >= 0 {
          if defined('apache::mod::ssl') {
            ::portage::makeconf { 'apache2_modules':
              content => concat($default_mods, [ 'authz_core', 'socache_shmcb' ]),
            }
          } else {
            ::portage::makeconf { 'apache2_modules':
              content => concat($default_mods, 'authz_core'),
            }
          }
        } else {
          ::portage::makeconf { 'apache2_modules':
            content => $default_mods,
          }
        }
      }

      file { [
        '/etc/apache2/modules.d/.keep_www-servers_apache-2',
        '/etc/apache2/vhosts.d/.keep_www-servers_apache-2',
      ]:
        ensure  => absent,
        require => Package['httpd'],
      }
    }

    $apxs_workaround = $::osfamily ? {
      'freebsd' => true,
      default   => false
    }

    # Template uses:
    # - $pidfile
    # - $user
    # - $group
    # - $logroot
    # - $error_log
    # - $sendfile
    # - $mod_dir
    # - $ports_file
    # - $confd_dir
    # - $vhost_dir
    # - $error_documents
    # - $error_documents_path
    # - $apxs_workaround
    # - $http_protocol_options
    # - $keepalive
    # - $keepalive_timeout
    # - $max_keepalive_requests
    # - $server_root
    # - $server_tokens
    # - $server_signature
    # - $trace_enable
    # - $rewrite_lock
    # - $root_directory_secured
    file { "${::apache::conf_dir}/${::apache::params::conf_file}":
      ensure  => file,
      content => template($conf_template),
      notify  => Class['Apache::Service'],
      require => [Package['httpd'], Concat[$ports_file]],
    }

    # preserve back-wards compatibility to the times when default_mods was
    # only a boolean value. Now it can be an array (too)
    if $default_mods =~ Array {
      class { '::apache::default_mods':
        all  => false,
        mods => $default_mods,
      }
    } else {
      class { '::apache::default_mods':
        all => $default_mods,
      }
    }
    class { '::apache::default_confd_files':
      all => $default_confd_files,
    }
    if $mpm_module and $mpm_module != 'false' { # lint:ignore:quoted_booleans
      include "::apache::mod::${mpm_module}"
    }

    $default_vhost_ensure = $default_vhost ? {
      true  => 'present',
      false => 'absent'
    }
    $default_ssl_vhost_ensure = $default_ssl_vhost ? {
      true  => 'present',
      false => 'absent'
    }

    ::apache::vhost { 'default':
      ensure          => $default_vhost_ensure,
      port            => $apache_port,
      docroot         => $docroot,
      scriptalias     => $scriptalias,
      serveradmin     => $serveradmin,
      access_log_file => $access_log_file,
      priority        => '15',
      ip              => $ip,
      logroot_mode    => $logroot_mode,
      manage_docroot  => $default_vhost,
    }
    $ssl_access_log_file = $::osfamily ? {
      'freebsd' => $access_log_file,
      default   => "ssl_${access_log_file}",
    }
    ::apache::vhost { 'default-ssl':
      ensure          => $default_ssl_vhost_ensure,
      port            => '443',
      ssl             => true,
      docroot         => $docroot,
      scriptalias     => $scriptalias,
      serveradmin     => $serveradmin,
      access_log_file => $ssl_access_log_file,
      priority        => '15',
      ip              => $ip,
      logroot_mode    => $logroot_mode,
      manage_docroot  => $default_ssl_vhost,
    }
  }

  # This anchor can be used as a reference point for things that need to happen *after*
  # all modules have been put in place.
  anchor { '::apache::modules_set_up': }
}