make default certdir matchup with acme-tiny-wrapper
[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 In priority: this arg, $ACME_TINY_WRAPPER_CERT_DIR,
33 $HOME/webservercerts, if the other options aren't set.
34 -h|--help Print help and exit
35 -r DocumentRoot
36 -- Subsequent arguments are never treated as options
37
38 Note: options and non-options can be in any order.
39 EOF
40 exit $1
41 }
42
43 ##### begin command line parsing ########
44
45 cert_dir="$ACME_TINY_WRAPPER_CERT_DIR"
46 if [[ ! $cert_dir ]]; then
47 cert_dir=$HOME/webservercerts
48 fi
49 ssl=true
50 extra_settings=
51 args=()
52 port="*:443"
53 while [[ $1 ]]; do
54 case $1 in
55 -i) ssl=false; shift ;; # i for insecure
56 -c) cert_dir="$2"; shift 2 ;;
57 -p) port="$2"; shift 2 ;;
58 -r) root="$2"; shift 2 ;;
59 --) shift; break ;;
60 -?*|-h|--help) usage ;;
61 *) args+=("$1"); shift ;;
62 esac
63 done
64 args+=("$@")
65
66 if (( ${#args[@]} == 2 )); then
67 read extra_settings h <<<"${args[@]}"
68 else
69 read h <<<"${args[@]}"
70 fi
71
72 if [[ ! $h ]]; then
73 echo "$0: error: expected domain arg"
74 usage 1
75 fi
76
77 if [[ ! $root ]]; then
78 root=/var/www/$h/html
79 fi
80
81
82 ##### end command line parsing ########
83
84 # taken from the let's encrypt generated site, using
85 # ./certbot-auto --apache (should use the test mode to check if there are updates)
86 # on 5/29/2016
87
88 # I could have also used the mozilla generator this, but it had some open issues
89 # with no response
90 # so I figured I would check out let's encrypt.
91 # It's a little more liberal, but still get's an A in ssl labs,
92 # so, meh, I'll use it.
93 # https://mozilla.github.io/server-side-tls/ssl-config-generator/
94
95
96 rm -f /etc/apache2/sites-enabled/000-default.conf
97
98 mkdir -p $root
99 dd of=/etc/apache2/sites-enabled/$h.conf <<EOF
100 <VirtualHost $port>
101 ServerName $h
102 ServerAlias www.$h
103 DocumentRoot $root
104 EOF
105
106 if [[ $extra_settings ]]; then
107 cat -- $extra_settings | tee -a /etc/apache2/sites-enabled/$h.conf
108 fi
109
110 # go faster!
111 if [[ -e /etc/apache2/mods-available/http2.load ]]; then
112 # https://httpd.apache.org/docs/2.4/mod/mod_http2.html
113 a2enmod http2
114 tee -a /etc/apache2/sites-enabled/$h.conf <<EOF
115 Protocols h2 http/1.1
116 EOF
117 fi
118
119 if $ssl; then
120 tee -a /etc/apache2/sites-enabled/$h.conf <<EOF
121 SSLCertificateFile $cert_dir/$h-chained.pem
122 SSLCertificateKeyFile $cert_dir/$h-domain.key
123 Include /etc/letsencrypt/options-ssl-apache.conf
124 EOF
125
126 dd of=/etc/apache2/sites-enabled/httpsredir.conf <<'EOF'
127 <VirtualHost *:80>
128 ServerAdmin webmaster@localhost
129 DocumentRoot /var/www/html
130
131 ErrorLog ${APACHE_LOG_DIR}/error.log
132 CustomLog ${APACHE_LOG_DIR}/httpsredir-access.log combined
133
134 RewriteEngine on
135 # ian: removed so it's for all sites
136 #RewriteCond %{SERVER_NAME} =certbot.iank.bid
137 RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,QSA,R=permanent]
138 </VirtualHost>
139 EOF
140
141 mkdir -p /etc/letsencrypt
142
143 base_file=/etc/letsencrypt/options-ssl-apache.conf
144 # this is from cerbot, see below.
145 dd of=$base_file <<'EOF'
146 # Baseline setting to Include for SSL sites
147
148 SSLEngine on
149
150 # Intermediate configuration, tweak to your needs
151 SSLProtocol all -SSLv2 -SSLv3
152 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
153 SSLHonorCipherOrder on
154 SSLCompression off
155
156 SSLOptions +StrictRequire
157
158 # Add vhost name to log entries:
159 LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-agent}i\"" vhost_combined
160 LogFormat "%v %h %l %u %t \"%r\" %>s %b" vhost_common
161
162 #CustomLog /var/log/apache2/access.log vhost_combined
163 #LogLevel warn
164 #ErrorLog /var/log/apache2/error.log
165
166 # Always ensure Cookies have "Secure" set (JAH 2012/1)
167 #Header edit Set-Cookie (?i)^(.*)(;\s*secure)??((\s*;)?(.*)) "$1; Secure$3$4"
168 EOF
169
170 upstream=https://github.com/certbot/certbot/raw/master/certbot-apache/certbot_apache/options-ssl-apache.conf
171 if ! diff -c <(wget -q -O - $upstream) $base_file; then
172 cat <<EOF
173 WARNING!!!!!!!!!
174 WARNING!!!!!!!!!
175 WARNING!!!!!!!!!
176 WARNING!!!!!!!!!
177 WARNING!!!!!!!!!
178 upstream ssl settings differ from the snapshot we have taken!!!
179 We diffed with this command:
180 diff -c <(wget -q -O - $upstream) $base_file
181 Update this script to take care this warning!!!!!
182 EOF
183 sleep 1
184 fi
185 fi
186 tee -a /etc/apache2/sites-enabled/$h.conf <<EOF
187 ErrorLog \${APACHE_LOG_DIR}/error.log
188 CustomLog \${APACHE_LOG_DIR}/access.log vhost_combined
189 </VirtualHost>
190
191 # vim: syntax=apache ts=4 sw=4 sts=4 sr noet
192 EOF
193
194 a2enmod ssl rewrite # rewrite needed for httpredir
195 service apache2 restart
196
197 # I rarely look at how much traffic I get, so let's keep that info
198 # around for longer than the default of 2 weeks.
199 sed -ri --follow-symlinks 's/^(\s*rotate\s).*/\1 365/' /etc/logrotate.d/apache2