add http2 for apache
[basic-https-conf] / apache-site
1 #!/bin/bash
2 # Copyright (C) 2016 Ian Kelling
3
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7
8 # http://www.apache.org/licenses/LICENSE-2.0
9
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15
16 # run as root.
17 [[ $EUID == 0 ]] || exec sudo -E "$BASH_SOURCE" "$@"
18
19 set -eE -o pipefail
20 trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?" >&2' ERR
21
22 usage() {
23 cat <<EOF
24 Usage: ${0##*/} [EXTRA_SETTINGS_FILE] DOMAIN
25 Setup apache virtualhost config with https using
26 ssl config provided by let's encrypt and my standard
27 location for storing certs.
28
29 EXTRA_SETTINGS_FILE can be - for stdin
30 -p PORT
31 -i Insecure, no ssl
32 -c CERT_DIR Default is /p/c/machine_specific/\$HOSTNAME/webservercerts
33 -h|--help Print help and exit
34 -r DocumentRoot
35 -- Subsequent arguments are never treated as options
36
37 Note: options and non-options can be in any order.
38 EOF
39 exit $1
40 }
41
42 ##### begin command line parsing ########
43
44 cert_dir=/p/c/machine_specific/$HOSTNAME/webservercerts
45 ssl=true
46 extra_settings=
47 args=()
48 port="*:443"
49 while [[ $1 ]]; do
50 case $1 in
51 -i) ssl=false; shift ;; # i for insecure
52 -c) cert_dir="$2"; shift 2 ;;
53 -p) port="$2"; shift 2 ;;
54 -r) root="$2"; shift 2 ;;
55 --) shift; break ;;
56 -?*|-h|--help) usage ;;
57 *) args+=("$1"); shift ;;
58 esac
59 done
60 args+=("$@")
61
62 if (( ${#args[@]} == 2 )); then
63 read extra_settings h <<<"${args[@]}"
64 else
65 read h <<<"${args[@]}"
66 fi
67
68 if [[ ! $h ]]; then
69 echo "$0: error: expected domain arg"
70 usage 1
71 fi
72
73 if [[ ! $root ]]; then
74 root=/var/www/$h/html
75 fi
76
77
78 ##### end command line parsing ########
79
80 # taken from the let's encrypt generated site, using
81 # ./certbot-auto --apache (should use the test mode to check if there are updates)
82 # on 5/29/2016
83
84 # I could have also used the mozilla generator this, but it had some open issues
85 # with no response
86 # so I figured I would check out let's encrypt.
87 # It's a little more liberal, but still get's an A in ssl labs,
88 # so, meh, I'll use it.
89 # https://mozilla.github.io/server-side-tls/ssl-config-generator/
90
91
92 rm -f /etc/apache2/sites-enabled/000-default.conf
93
94 mkdir -p $root
95 dd of=/etc/apache2/sites-enabled/$h.conf <<EOF
96 <VirtualHost $port>
97 ServerName $h
98 ServerAlias www.$h
99 DocumentRoot $root
100 EOF
101
102 if [[ $extra_settings ]]; then
103 cat -- $extra_settings | tee -a /etc/apache2/sites-enabled/$h.conf
104 fi
105
106 # go faster!
107 if [[ -e /etc/apache2/mods-available/http2.load ]]; then
108 # https://httpd.apache.org/docs/2.4/mod/mod_http2.html
109 a2enmod http2
110 tee -a /etc/apache2/sites-enabled/$h.conf <<EOF
111 Protocols h2 http/1.1
112 EOF
113 fi
114
115 if $ssl; then
116 tee -a /etc/apache2/sites-enabled/$h.conf <<EOF
117 SSLCertificateFile $cert_dir/$h-chained.pem
118 SSLCertificateKeyFile $cert_dir/$h-domain.key
119 Include /etc/letsencrypt/options-ssl-apache.conf
120 EOF
121
122 dd of=/etc/apache2/sites-enabled/httpsredir.conf <<'EOF'
123 <VirtualHost *:80>
124 ServerAdmin webmaster@localhost
125 DocumentRoot /var/www/html
126
127 ErrorLog ${APACHE_LOG_DIR}/error.log
128 CustomLog ${APACHE_LOG_DIR}/httpsredir-access.log combined
129
130 RewriteEngine on
131 # ian: removed so it's for all sites
132 #RewriteCond %{SERVER_NAME} =certbot.iank.bid
133 RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,QSA,R=permanent]
134 </VirtualHost>
135 EOF
136
137 mkdir -p /etc/letsencrypt
138
139 base_file=/etc/letsencrypt/options-ssl-apache.conf
140 # this is from cerbot, see below.
141 dd of=$base_file <<'EOF'
142 # Baseline setting to Include for SSL sites
143
144 SSLEngine on
145
146 # Intermediate configuration, tweak to your needs
147 SSLProtocol all -SSLv2 -SSLv3
148 SSLCipherSuite ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-DSS-AES128-GCM-SHA256:kEDH+AESGCM:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES256-SHA:DHE-RSA-AES128-SHA256:DHE-RSA-AES128-SHA:DHE-DSS-AES128-SHA256:DHE-RSA-AES256-SHA256:DHE-DSS-AES256-SHA:DHE-RSA-AES256-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:AES128-SHA256:AES256-SHA256:AES128-SHA:AES256-SHA:AES:CAMELLIA:DES-CBC3-SHA:!aNULL:!eNULL:!EXPORT:!DES:!RC4:!MD5:!PSK:!aECDH:!EDH-DSS-DES-CBC3-SHA:!EDH-RSA-DES-CBC3-SHA:!KRB5-DES-CBC3-SHA
149 SSLHonorCipherOrder on
150 SSLCompression off
151
152 SSLOptions +StrictRequire
153
154 # Add vhost name to log entries:
155 LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-agent}i\"" vhost_combined
156 LogFormat "%v %h %l %u %t \"%r\" %>s %b" vhost_common
157
158 #CustomLog /var/log/apache2/access.log vhost_combined
159 #LogLevel warn
160 #ErrorLog /var/log/apache2/error.log
161
162 # Always ensure Cookies have "Secure" set (JAH 2012/1)
163 #Header edit Set-Cookie (?i)^(.*)(;\s*secure)??((\s*;)?(.*)) "$1; Secure$3$4"
164 EOF
165
166 upstream=https://github.com/certbot/certbot/raw/master/certbot-apache/certbot_apache/options-ssl-apache.conf
167 if ! diff -c <(wget -q -O - $upstream) $base_file; then
168 cat <<EOF
169 WARNING!!!!!!!!!
170 WARNING!!!!!!!!!
171 WARNING!!!!!!!!!
172 WARNING!!!!!!!!!
173 WARNING!!!!!!!!!
174 upstream ssl settings differ from the snapshot we have taken!!!
175 We diffed with this command:
176 diff -c <(wget -q -O - $upstream) $base_file
177 Update this script to take care this warning!!!!!
178 EOF
179 sleep 1
180 fi
181 fi
182 tee -a /etc/apache2/sites-enabled/$h.conf <<EOF
183 ErrorLog \${APACHE_LOG_DIR}/error.log
184 CustomLog \${APACHE_LOG_DIR}/access.log vhost_combined
185 </VirtualHost>
186
187 # vim: syntax=apache ts=4 sw=4 sts=4 sr noet
188 EOF
189
190 a2enmod ssl rewrite # rewrite needed for httpredir
191 service apache2 restart
192
193 # I rarely look at how much traffic I get, so let's keep that info
194 # around for longer than the default of 2 weeks.
195 sed -ri --follow-symlinks 's/^(\s*rotate\s).*/\1 365/' /etc/logrotate.d/apache2