]> git.openstreetmap.org Git - chef.git/blob - cookbooks/apache/recipes/default.rb
apache: restart apache if ports or ssl config changed
[chef.git] / cookbooks / apache / recipes / default.rb
1 #
2 # Cookbook:: apache
3 # Recipe:: default
4 #
5 # Copyright:: 2011, OpenStreetMap Foundation
6 #
7 # Licensed under the Apache License, Version 2.0 (the "License");
8 # you may not use this file except in compliance with the License.
9 # You may obtain a copy of the License at
10 #
11 #     https://www.apache.org/licenses/LICENSE-2.0
12 #
13 # Unless required by applicable law or agreed to in writing, software
14 # distributed under the License is distributed on an "AS IS" BASIS,
15 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 # See the License for the specific language governing permissions and
17 # limitations under the License.
18 #
19
20 include_recipe "fail2ban"
21 include_recipe "prometheus"
22 include_recipe "ssl"
23
24 package %w[
25   apache2
26   libwww-perl
27   logrotate
28 ]
29
30 %w[event itk prefork worker].each do |mpm|
31   next if mpm == node[:apache][:mpm]
32
33   apache_module "mpm_#{mpm}" do
34     action [:disable]
35   end
36 end
37
38 apache_module "mpm_#{node[:apache][:mpm]}" do
39   action [:enable]
40 end
41
42 apache_module "http2"
43
44 admins = data_bag_item("apache", "admins")
45
46 apache_conf "httpd" do
47   template "httpd.conf.erb"
48   notifies :reload, "service[apache2]"
49 end
50
51 template "/etc/apache2/ports.conf" do
52   source "ports.conf.erb"
53   owner "root"
54   group "root"
55   mode "644"
56   notifies :restart, "service[apache2]"
57 end
58
59 systemd_service "apache2" do
60   dropin "chef"
61   memory_high "50%"
62   memory_max "75%"
63   notifies :restart, "service[apache2]"
64 end
65
66 apache_module "info" do
67   conf "info.conf.erb"
68   variables :hosts => admins["hosts"]
69 end
70
71 apache_module "status" do
72   conf "status.conf.erb"
73   variables :hosts => admins["hosts"]
74 end
75
76 apache_conf "tokens" do
77   template "tokens.conf.erb"
78 end
79
80 if node[:apache][:evasive][:enable]
81   apache_module "evasive" do
82     conf "evasive.conf.erb"
83   end
84 else
85   apache_module "evasive" do
86     action :disable
87   end
88 end
89
90 apache_module "brotli" do
91   conf "brotli.conf.erb"
92 end
93
94 apache_module "deflate" do
95   conf "deflate.conf.erb"
96 end
97
98 apache_module "headers"
99 apache_module "ssl"
100
101 apache_conf "ssl" do
102   template "ssl.erb"
103   reload_apache false
104   restart_apache true # restart required for shared memory config changes
105 end
106
107 # Apache should only be started after modules enabled
108 service "apache2" do
109   action [:enable, :start]
110   retries 2
111   retry_delay 10
112   supports :status => true, :restart => true, :reload => true
113 end
114
115 fail2ban_filter "apache-forbidden" do
116   action :delete
117 end
118
119 fail2ban_jail "apache-forbidden" do
120   action :delete
121 end
122
123 fail2ban_filter "apache-evasive" do
124   failregex ": Blacklisting address <ADDR>: possible DoS attack\\.$"
125 end
126
127 fail2ban_jail "apache-evasive" do
128   filter "apache-evasive"
129   backend "systemd"
130   journalmatch "_SYSTEMD_UNIT=apache2.service SYSLOG_IDENTIFIER=mod_evasive"
131   ports [80, 443]
132   findtime "10m"
133   maxretry 3
134 end
135
136 template "/var/lib/prometheus/node-exporter/apache.prom" do
137   source "apache.prom.erb"
138   owner "root"
139   group "root"
140   mode "644"
141 end
142
143 prometheus_exporter "apache" do
144   port 9117
145   options "--scrape_uri=http://localhost/server-status?auto"
146 end