add/improve proxy/port args
[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 [[ $EUID == 0 ]] || exec sudo -E "$BASH_SOURCE" "$@"
17
18 set -eE -o pipefail
19 trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?" >&2' ERR
20
21 usage() {
22 cat <<EOF
23 Usage: ${0##*/} [OPTIONS] [EXTRA_SETTINGS_FILE] DOMAIN
24 Setup apache virtualhost config with https using
25 ssl config provided by let's encrypt and my standard
26 location for storing certs.
27
28 EXTRA_SETTINGS_FILE can be - for stdin
29 -c CERT_DIR In priority: this arg, $ACME_TINY_WRAPPER_CERT_DIR,
30 $HOME/webservercerts, if the other options aren't set.
31 -f [ADDR:]PORT Enable proxy to [ADDR:]PORT. ADDR default is 127.0.0.1
32 -i Insecure, no ssl
33 -p PORT Main port to listen on, default 443
34 -r DocumentRoot
35 -h|--help Print help and exit
36
37 Note: Uses GNU getopt options parsing style
38 EOF
39 exit $1
40 }
41
42 ##### begin command line parsing ########
43
44 cert_dir="$ACME_TINY_WRAPPER_CERT_DIR"
45 if [[ ! $cert_dir ]]; then
46 cert_dir=$HOME/webservercerts
47 fi
48 ssl=true
49 extra_settings=
50 port=443
51 temp=$(getopt -l help ic:f:p:r:h "$@") || usage 1
52 eval set -- "$temp"
53 while true; do
54 case $1 in
55 -c) cert_dir="$2"; shift 2 ;;
56 -f) proxy="$2"; shift 2 ;;
57 -i) ssl=false; shift ;;
58 -p) port="$2"; shift 2 ;;
59 -r) root="$2"; shift 2 ;;
60 --) shift; break ;;
61 -h|--help) usage ;;
62 *) echo "$0: Internal error!" ; exit 1 ;;
63 esac
64 done
65
66 if (( ${#@} == 2 )); then
67 read -r extra_settings h <<<"${@}"
68 else
69 read -r h <<<"${@}"
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 if [[ $proxy ]]; then
82 [[ $proxy == *:* ]] || proxy=127.0.0.1:$proxy
83 fi
84
85
86 ##### end command line parsing ########
87
88 # taken from the let's encrypt generated site, using
89 # ./certbot-auto --apache (should use the test mode to check if there are updates)
90 # on 5/29/2016
91
92 # I could have also used the mozilla generator this, but it had some open issues
93 # with no response
94 # so I figured I would check out let's encrypt.
95 # It's a little more liberal, but still get's an A in ssl labs,
96 # so, meh, I'll use it.
97 # https://mozilla.github.io/server-side-tls/ssl-config-generator/
98
99
100 rm -f /etc/apache2/sites-enabled/000-default.conf
101
102 mkdir -p $root
103 vhost_file=/etc/apache2/sites-enabled/$h.conf
104 redir_file=/etc/apache2/sites-enabled/httpsredir.conf
105
106 # note, we exepct ServerRoot of /etc/apache2
107 # apache requires exactly 1 listen directive per port (when no ip is also given),
108 # so we have to parse the config to do it programatically.
109 listen_80=false
110 listen_port=false
111 cd /etc/apache2
112 conf_files=(apache2.conf)
113 for (( i=0; i < ${#conf_files[@]}; i++ )); do
114 f="${conf_files[i]}"
115 # note: globs are expanded here:
116 conf_files+=( $(sed -rn "s,^\s*Include(Optional)?\s+(\S+).*,\2,p" "$f") )
117 case $(readlink -f "$f") in
118 $vhost_file|$redir_file) continue ;;
119 esac
120 for p in $(sed -rn "s,^\s*listen\s+(\S+).*,\1,Ip" "$f"); do
121 case $p in
122 80) listen_80=true ;;
123 $port) listen_port=true ;;
124 esac
125 done
126 done
127
128 if $ssl; then
129 https_arg=" https"
130 fi
131
132
133 echo "$0: creating $vhost_file"
134 cat >$vhost_file <<EOF
135 <VirtualHost *:$port>
136 ServerName $h
137 ServerAlias www.$h
138 DocumentRoot $root
139 EOF
140
141 if [[ $extra_settings ]]; then
142 cat -- $extra_settings >>$vhost_file
143 fi
144
145 # go faster!
146 if [[ -e /etc/apache2/mods-available/http2.load ]]; then
147 # https://httpd.apache.org/docs/2.4/mod/mod_http2.html
148 a2enmod http2
149 cat >>$vhost_file <<EOF
150 Protocols h2 http/1.1
151 EOF
152 fi
153
154 if [[ $proxy ]]; then
155 a2enmod proxy proxy_http
156 # fyi: trailing slash is important
157 # reference: https://httpd.apache.org/docs/2.4/howto/reverse_proxy.html
158 cat >>$vhost_file <<EOF
159 ProxyPass "/" "http://$proxy/"
160 ProxyPassReverse "/" "http://$proxy/"
161 EOF
162 fi
163
164 if $ssl; then
165 certbot_ssl_conf=/etc/letsencrypt/options-ssl-apache.conf
166 cat >>$vhost_file <<EOF
167 SSLCertificateFile $cert_dir/$h-chained.pem
168 SSLCertificateKeyFile $cert_dir/$h-domain.key
169 Include $certbot_ssl_conf
170 EOF
171
172 # if we are using a non-standard port, setup don't setup
173 # irrelevant 443 redirect.
174 if [[ $port == "443" ]]; then
175 echo "$0: creating $redir_file"
176 cat >$redir_file <<'EOF'
177 # vhost_combined with %D (request time in microseconds)
178 # this file is just a convenient place to drop it.
179 LogFormat "%v:%p %h %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\" %D" vhost_time_combined
180 <VirtualHost *:80>
181 ServerAdmin webmaster@localhost
182 DocumentRoot /var/www/html
183
184 ErrorLog ${APACHE_LOG_DIR}/error.log
185 CustomLog ${APACHE_LOG_DIR}/httpsredir-access.log combined
186
187 RewriteEngine on
188 # ian: removed so it's for all sites
189 #RewriteCond %{SERVER_NAME} =certbot.iank.bid
190 RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,QSA,R=permanent]
191 </VirtualHost>
192 EOF
193 if ! $listen_80; then
194 cat >>$redir_file <<'EOF'
195 Listen 80
196 EOF
197 fi
198 fi
199
200 mkdir -p /etc/letsencrypt
201
202 # this is from cerbot, see below.
203 echo "$0: creating $certbot_ssl_conf"
204 cat >$certbot_ssl_conf <<'EOF'
205 # Baseline setting to Include for SSL sites
206
207 SSLEngine on
208
209 # Intermediate configuration, tweak to your needs
210 SSLProtocol all -SSLv2 -SSLv3
211 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
212 SSLHonorCipherOrder on
213 SSLCompression off
214
215 SSLOptions +StrictRequire
216
217 # Add vhost name to log entries:
218 LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-agent}i\"" vhost_combined
219 LogFormat "%v %h %l %u %t \"%r\" %>s %b" vhost_common
220
221 #CustomLog /var/log/apache2/access.log vhost_combined
222 #LogLevel warn
223 #ErrorLog /var/log/apache2/error.log
224
225 # Always ensure Cookies have "Secure" set (JAH 2012/1)
226 #Header edit Set-Cookie (?i)^(.*)(;\s*secure)??((\s*;)?(.*)) "$1; Secure$3$4"
227 EOF
228
229 upstream=https://github.com/certbot/certbot/raw/master/certbot-apache/certbot_apache/options-ssl-apache.conf
230 if ! diff -c <(wget -q -O - $upstream) $certbot_ssl_conf; then
231 cat <<EOF
232 WARNING!!!!!!!!!
233 WARNING!!!!!!!!!
234 WARNING!!!!!!!!!
235 WARNING!!!!!!!!!
236 WARNING!!!!!!!!!
237 upstream ssl settings differ from the snapshot we have taken!!!
238 We diffed with this command:
239 diff -c <(wget -q -O - $upstream) $certbot_ssl_conf
240 Update this script to take care this warning!!!!!
241 EOF
242 sleep 1
243 fi
244 fi
245 cat >>$vhost_file <<EOF
246 ErrorLog \${APACHE_LOG_DIR}/error.log
247 CustomLog \${APACHE_LOG_DIR}/access.log vhost_time_combined
248 </VirtualHost>
249 EOF
250
251 if ! $listen_port; then
252 # reference: https://httpd.apache.org/docs/2.4/mod/mpm_common.html#listen
253 cat >>$vhost_file <<EOF
254 listen ${port}${https_arg}
255 EOF
256 fi
257
258
259 a2enmod ssl rewrite # rewrite needed for httpredir
260 service apache2 restart
261
262 # I rarely look at how much traffic I get, so let's keep that info
263 # around for longer than the default of 2 weeks.
264 sed -ri --follow-symlinks 's/^(\s*rotate\s).*/\1 365/' /etc/logrotate.d/apache2