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 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
36 -- Subsequent arguments are never treated as options
38 Note: options and non-options can be in any order.
43 ##### begin command line parsing ########
45 cert_dir
="$ACME_TINY_WRAPPER_CERT_DIR"
46 if [[ ! $cert_dir ]]; then
47 cert_dir
=$HOME/webservercerts
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 ;;
60 -?
*|
-h|
--help) usage
;;
61 *) args
+=("$1"); shift ;;
66 if (( ${#args[@]} == 2 )); then
67 read extra_settings h
<<<"${args[@]}"
69 read h
<<<"${args[@]}"
73 echo "$0: error: expected domain arg"
77 if [[ ! $root ]]; then
82 ##### end command line parsing ########
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)
88 # I could have also used the mozilla generator this, but it had some open issues
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/
96 rm -f /etc
/apache
2/sites-enabled
/000-default.conf
99 dd of
=/etc
/apache
2/sites-enabled
/$h.conf
<<EOF
106 if [[ $extra_settings ]]; then
107 cat -- $extra_settings |
tee -a /etc
/apache
2/sites-enabled
/$h.conf
111 if [[ -e /etc
/apache
2/mods-available
/http2.load
]]; then
112 # https://httpd.apache.org/docs/2.4/mod/mod_http2.html
114 tee -a /etc
/apache
2/sites-enabled
/$h.conf
<<EOF
115 Protocols h2 http/1.1
120 tee -a /etc
/apache
2/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
126 dd of
=/etc
/apache
2/sites-enabled
/httpsredir.conf
<<'EOF'
128 ServerAdmin webmaster@localhost
129 DocumentRoot /var/www/html
131 ErrorLog ${APACHE_LOG_DIR}/error.log
132 CustomLog ${APACHE_LOG_DIR}/httpsredir-access.log combined
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]
141 mkdir
-p /etc
/letsencrypt
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
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
156 SSLOptions +StrictRequire
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
162 #CustomLog /var/log/apache2/access.log vhost_combined
164 #ErrorLog /var/log/apache2/error.log
166 # Always ensure Cookies have "Secure" set (JAH 2012/1)
167 #Header edit Set-Cookie (?i)^(.*)(;\s*secure)??((\s*;)?(.*)) "$1; Secure$3$4"
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
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!!!!!
186 tee -a /etc
/apache
2/sites-enabled
/$h.conf
<<EOF
187 ErrorLog \${APACHE_LOG_DIR}/error.log
188 CustomLog \${APACHE_LOG_DIR}/access.log vhost_combined
191 # vim: syntax=apache ts=4 sw=4 sts=4 sr noet
194 a2enmod ssl rewrite
# rewrite needed for httpredir
195 service apache2 restart
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