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.
16 [[ $EUID == 0 ]] ||
exec sudo
-E "$BASH_SOURCE" "$@"
19 trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?" >&2' ERR
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.
28 EXTRA_SETTINGS_FILE can be - for stdin
31 -c CERT_DIR In priority: this arg, $ACME_TINY_WRAPPER_CERT_DIR,
32 $HOME/webservercerts, if the other options aren't set.
34 -h|--help Print help and exit
36 Note: Uses GNU getopt options parsing style
41 ##### begin command line parsing ########
43 cert_dir
="$ACME_TINY_WRAPPER_CERT_DIR"
44 if [[ ! $cert_dir ]]; then
45 cert_dir
=$HOME/webservercerts
50 temp
=$
(getopt
-l help ic
:p
:r
:h
"$@") || usage
1
54 -i) ssl
=false
; shift ;;
55 -c) cert_dir
="$2"; shift 2 ;;
56 -p) port
="$2"; shift 2 ;;
57 -r) root
="$2"; shift 2 ;;
60 *) echo "$0: Internal error!" ; exit 1 ;;
64 if (( ${#@} == 2 )); then
65 read extra_settings h
<<<"${@}"
71 echo "$0: error: expected domain arg"
75 if [[ ! $root ]]; then
80 ##### end command line parsing ########
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)
86 # I could have also used the mozilla generator this, but it had some open issues
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/
94 rm -f /etc
/apache
2/sites-enabled
/000-default.conf
97 vhost_file
=/etc
/apache
2/sites-enabled
/$h.conf
98 cat >$vhost_file <<EOF
105 if [[ $extra_settings ]]; then
106 cat -- $extra_settings |
tee -a $vhost_file
110 if [[ -e /etc
/apache
2/mods-available
/http2.load
]]; then
111 # https://httpd.apache.org/docs/2.4/mod/mod_http2.html
113 tee -a $vhost_file <<EOF
114 Protocols h2 http/1.1
119 certbot_ssl_conf
=/etc
/letsencrypt
/options-ssl-apache.conf
120 tee -a $vhost_file <<EOF
121 SSLCertificateFile $cert_dir/$h-chained.pem
122 SSLCertificateKeyFile $cert_dir/$h-domain.key
123 Include $certbot_ssl_conf
126 dd of
=/etc
/apache
2/sites-enabled
/httpsredir.conf
<<'EOF'
127 # vhost_combined with %D (request time in microseconds)
128 # this file is just a convenient place to drop it.
129 LogFormat "%v:%p %h %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\" %{ms}T" vhost_time_combined
131 ServerAdmin webmaster@localhost
132 DocumentRoot /var/www/html
134 ErrorLog ${APACHE_LOG_DIR}/error.log
135 CustomLog ${APACHE_LOG_DIR}/httpsredir-access.log combined
138 # ian: removed so it's for all sites
139 #RewriteCond %{SERVER_NAME} =certbot.iank.bid
140 RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,QSA,R=permanent]
144 mkdir
-p /etc
/letsencrypt
146 # this is from cerbot, see below.
147 cat >$certbot_ssl_conf <<'EOF'
148 # Baseline setting to Include for SSL sites
152 # Intermediate configuration, tweak to your needs
153 SSLProtocol all -SSLv2 -SSLv3
154 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
155 SSLHonorCipherOrder on
158 SSLOptions +StrictRequire
160 # Add vhost name to log entries:
161 LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-agent}i\"" vhost_combined
162 LogFormat "%v %h %l %u %t \"%r\" %>s %b" vhost_common
164 #CustomLog /var/log/apache2/access.log vhost_combined
166 #ErrorLog /var/log/apache2/error.log
168 # Always ensure Cookies have "Secure" set (JAH 2012/1)
169 #Header edit Set-Cookie (?i)^(.*)(;\s*secure)??((\s*;)?(.*)) "$1; Secure$3$4"
172 upstream
=https
://github.com
/certbot
/certbot
/raw
/master
/certbot-apache
/certbot_apache
/options-ssl-apache.conf
173 if ! diff -c <(wget
-q -O - $upstream) $certbot_ssl_conf; then
180 upstream ssl settings differ from the snapshot we have taken!!!
181 We diffed with this command:
182 diff -c <(wget -q -O - $upstream) $certbot_ssl_conf
183 Update this script to take care this warning!!!!!
188 tee -a $vhost_file <<EOF
189 ErrorLog \${APACHE_LOG_DIR}/error.log
190 CustomLog \${APACHE_LOG_DIR}/access.log vhost_time_combined
193 # vim: syntax=apache ts=4 sw=4 sts=4 sr noet
196 a2enmod ssl rewrite
# rewrite needed for httpredir
197 service apache2 restart
199 # I rarely look at how much traffic I get, so let's keep that info
200 # around for longer than the default of 2 weeks.
201 sed -ri --follow-symlinks 's/^(\s*rotate\s).*/\1 365/' /etc
/logrotate.d
/apache2