90a98372dbf2c3a48052f239613b579b98161e68
[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 if $ssl; then
107 tee -a /etc/apache2/sites-enabled/$h.conf <<EOF
108 SSLCertificateFile $cert_dir/$h-chained.pem
109 SSLCertificateKeyFile $cert_dir/$h-domain.key
110 Include /etc/letsencrypt/options-ssl-apache.conf
111 EOF
112
113 dd of=/etc/apache2/sites-enabled/httpsredir.conf <<'EOF'
114 <VirtualHost *:80>
115 ServerAdmin webmaster@localhost
116 DocumentRoot /var/www/html
117
118 ErrorLog ${APACHE_LOG_DIR}/error.log
119 CustomLog ${APACHE_LOG_DIR}/httpsredir-access.log combined
120
121 RewriteEngine on
122 # ian: removed so it's for all sites
123 #RewriteCond %{SERVER_NAME} =certbot.iank.bid
124 RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,QSA,R=permanent]
125 </VirtualHost>
126 EOF
127
128 mkdir -p /etc/letsencrypt
129
130 base_file=/etc/letsencrypt/options-ssl-apache.conf
131 # this is from cerbot, see below.
132 dd of=$base_file <<'EOF'
133 # Baseline setting to Include for SSL sites
134
135 SSLEngine on
136
137 # Intermediate configuration, tweak to your needs
138 SSLProtocol all -SSLv2 -SSLv3
139 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
140 SSLHonorCipherOrder on
141 SSLCompression off
142
143 SSLOptions +StrictRequire
144
145 # Add vhost name to log entries:
146 LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-agent}i\"" vhost_combined
147 LogFormat "%v %h %l %u %t \"%r\" %>s %b" vhost_common
148
149 #CustomLog /var/log/apache2/access.log vhost_combined
150 #LogLevel warn
151 #ErrorLog /var/log/apache2/error.log
152
153 # Always ensure Cookies have "Secure" set (JAH 2012/1)
154 #Header edit Set-Cookie (?i)^(.*)(;\s*secure)??((\s*;)?(.*)) "$1; Secure$3$4"
155 EOF
156
157 upstream=https://github.com/certbot/certbot/raw/master/certbot-apache/certbot_apache/options-ssl-apache.conf
158 if ! diff -c <(wget -q -O - $upstream) $base_file; then
159 cat <<EOF
160 WARNING!!!!!!!!!
161 WARNING!!!!!!!!!
162 WARNING!!!!!!!!!
163 WARNING!!!!!!!!!
164 WARNING!!!!!!!!!
165 upstream ssl settings differ from the snapshot we have taken!!!
166 We diffed with this command:
167 diff -c <(wget -q -O - $upstream) $base_file
168 Update this script to take care this warning!!!!!
169 EOF
170 sleep 1
171 fi
172 fi
173 tee -a /etc/apache2/sites-enabled/$h.conf <<EOF
174 ErrorLog \${APACHE_LOG_DIR}/error.log
175 CustomLog \${APACHE_LOG_DIR}/access.log vhost_combined
176 </VirtualHost>
177
178 # vim: syntax=apache ts=4 sw=4 sts=4 sr noet
179 EOF
180
181 a2enmod ssl rewrite # rewrite needed for httpredir
182 service apache2 restart
183
184 # I rarely look at how much traffic I get, so let's keep that info
185 # around for longer than the default of 2 weeks.
186 sed -ri --follow-symlinks 's/^(\s*rotate\s).*/\1 365/' /etc/logrotate.d/apache2