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