2 # Copyright (C) 2016 Ian Kelling
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
8 # http://www.apache.org/licenses/LICENSE-2.0
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.
17 [[ $EUID == 0 ]] ||
exec sudo
-E "$BASH_SOURCE" "$@"
20 trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?" >&2' ERR
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.
29 EXTRA_SETTINGS_FILE can be - for stdin
32 -c CERT_DIR Default is /p/c/machine_specific/\$HOSTNAME/webservercerts
33 -h|--help Print help and exit
35 -- Subsequent arguments are never treated as options
37 Note: options and non-options can be in any order.
42 ##### begin command line parsing ########
44 cert_dir
=/p
/c
/machine_specific
/$HOSTNAME/webservercerts
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 ;;
56 -?
*|
-h|
--help) usage
;;
57 *) args
+=("$1"); shift ;;
62 if (( ${#args[@]} == 2 )); then
63 read extra_settings h
<<<"${args[@]}"
65 read h
<<<"${args[@]}"
69 echo "$0: error: expected domain arg"
73 if [[ ! $root ]]; then
78 ##### end command line parsing ########
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)
84 # I could have also used the mozilla generator this, but it had some open issues
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/
92 rm -f /etc
/apache
2/sites-enabled
/000-default.conf
95 dd of
=/etc
/apache
2/sites-enabled
/$h.conf
<<EOF
102 if [[ $extra_settings ]]; then
103 cat -- $extra_settings |
tee -a /etc
/apache
2/sites-enabled
/$h.conf
107 tee -a /etc
/apache
2/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
113 dd of
=/etc
/apache
2/sites-enabled
/httpsredir.conf
<<'EOF'
115 ServerAdmin webmaster@localhost
116 DocumentRoot /var/www/html
118 ErrorLog ${APACHE_LOG_DIR}/error.log
119 CustomLog ${APACHE_LOG_DIR}/httpsredir-access.log combined
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]
128 mkdir
-p /etc
/letsencrypt
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
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
143 SSLOptions +StrictRequire
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
149 #CustomLog /var/log/apache2/access.log vhost_combined
151 #ErrorLog /var/log/apache2/error.log
153 # Always ensure Cookies have "Secure" set (JAH 2012/1)
154 #Header edit Set-Cookie (?i)^(.*)(;\s*secure)??((\s*;)?(.*)) "$1; Secure$3$4"
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
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!!!!!
173 tee -a /etc
/apache
2/sites-enabled
/$h.conf
<<EOF
174 ErrorLog \${APACHE_LOG_DIR}/error.log
175 CustomLog \${APACHE_LOG_DIR}/access.log vhost_combined
178 # vim: syntax=apache ts=4 sw=4 sts=4 sr noet
181 a2enmod ssl rewrite
# rewrite needed for httpredir
182 service apache2 restart
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