fix arg parsing
[basic-https-conf] / apache-site
index 32c9f82cca78dbb3866218000d456bf145217e16..a4f295ac68f0bd9de1e0981085e6e667c890eaff 100755 (executable)
@@ -1,4 +1,4 @@
-#!/bin/bash -l
+#!/bin/bash
 # Copyright (C) 2016 Ian Kelling
 
 # Licensed under the Apache License, Version 2.0 (the "License");
@@ -13,6 +13,8 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
+[[ $EUID == 0 ]] || exec sudo -E "$BASH_SOURCE" "$@"
+
 set -eE -o pipefail
 trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?" >&2' ERR
 
@@ -25,38 +27,44 @@ location for storing certs.
 
 EXTRA_SETTINGS_FILE can be - for stdin
 -p PORT
--i         Insecure, no ssl
--h|--help  Print help and exit
--r         DocumentRoot
---         Subsequent arguments are never treated as options
+-i                Insecure, no ssl
+-c CERT_DIR       In priority: this arg, $ACME_TINY_WRAPPER_CERT_DIR,
+                  $HOME/webservercerts, if the other options aren't set.
+-r                DocumentRoot
+-h|--help         Print help and exit
 
-Note: options and non-options can be in any order.
+Note: Uses GNU getopt options parsing style
 EOF
     exit $1
 }
 
 ##### begin command line parsing ########
 
+cert_dir="$ACME_TINY_WRAPPER_CERT_DIR"
+if [[ ! $cert_dir ]]; then
+    cert_dir=$HOME/webservercerts
+fi
 ssl=true
 extra_settings=
-args=()
 port="*:443"
-while [[ $1 ]]; do
+temp=$(getopt -l help ic:p:r:h "$@") || usage 1
+eval set -- "$temp"
+while true; do
     case $1 in
-        -i) ssl=false; shift ;; # i for insecure
+        -i) ssl=false; shift ;;
+        -c) cert_dir="$2"; shift 2 ;;
         -p) port="$2"; shift 2 ;;
         -r) root="$2"; shift 2 ;;
         --) shift; break ;;
-        -?*|-h|--help) usage ;;
-        *) args+=("$1"); shift ;;
+        -h|--help) usage ;;
+        *) echo "$0: Internal error!" ; exit 1 ;;
     esac
 done
-args+=("$@")
 
-if (( ${#args[@]} == 2 )); then
-    read extra_settings h <<<"${args[@]}"
+if (( ${#@} == 2 )); then
+    read extra_settings h <<<"${@}"
 else
-    read h <<<"${args[@]}"
+    read h <<<"${@}"
 fi
 
 if [[ ! $h ]]; then
@@ -68,8 +76,8 @@ if [[ ! $root ]]; then
     root=/var/www/$h/html
 fi
 
+
 ##### end command line parsing ########
-cdir=/p/c/machine_specific/$HOSTNAME/webservercerts
 
 # taken from the let's encrypt generated site, using
 # ./certbot-auto --apache (should use the test mode to check if there are updates)
@@ -83,9 +91,10 @@ cdir=/p/c/machine_specific/$HOSTNAME/webservercerts
 # https://mozilla.github.io/server-side-tls/ssl-config-generator/
 
 
-sudo rm -f /etc/apache2/sites-enabled/000-default.conf
+rm -f /etc/apache2/sites-enabled/000-default.conf
 
-sudo dd of=/etc/apache2/sites-enabled/$h.conf <<EOF
+mkdir -p $root
+dd of=/etc/apache2/sites-enabled/$h.conf <<EOF
 <VirtualHost $port>
         ServerName $h
         ServerAlias www.$h
@@ -93,24 +102,32 @@ sudo dd of=/etc/apache2/sites-enabled/$h.conf <<EOF
 EOF
 
 if [[ $extra_settings ]]; then
-    cat $extra_settings | sudo tee -a /etc/apache2/sites-enabled/$h.conf
+    cat -- $extra_settings | tee -a /etc/apache2/sites-enabled/$h.conf
+fi
+
+# go faster!
+if [[ -e /etc/apache2/mods-available/http2.load ]]; then
+    # https://httpd.apache.org/docs/2.4/mod/mod_http2.html
+    a2enmod http2
+tee -a /etc/apache2/sites-enabled/$h.conf <<EOF
+        Protocols h2 http/1.1
+EOF
 fi
 
 if $ssl; then
-    sudo tee -a /etc/apache2/sites-enabled/$h.conf <<EOF
-        SSLCertificateFile $cdir/$h-chained.pem
-        SSLCertificateKeyFile $cdir/$h-domain.key
+    tee -a /etc/apache2/sites-enabled/$h.conf <<EOF
+        SSLCertificateFile $cert_dir/$h-chained.pem
+        SSLCertificateKeyFile $cert_dir/$h-domain.key
         Include /etc/letsencrypt/options-ssl-apache.conf
 EOF
 
-    sudo dd of=/etc/apache2/sites-enabled/httpsredir.conf <<'EOF'
+    dd of=/etc/apache2/sites-enabled/httpsredir.conf <<'EOF'
 <VirtualHost *:80>
         ServerAdmin webmaster@localhost
         DocumentRoot /var/www/html
 
-
         ErrorLog ${APACHE_LOG_DIR}/error.log
-        CustomLog ${APACHE_LOG_DIR}/access.log combined
+        CustomLog ${APACHE_LOG_DIR}/httpsredir-access.log combined
 
 RewriteEngine on
 # ian: removed so it's for all sites
@@ -119,8 +136,11 @@ RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,QSA,R=permanent]
 </VirtualHost>
 EOF
 
-    sudo mkdir -p /etc/letsencrypt
-    sudo dd of=/etc/letsencrypt/options-ssl-apache.conf <<'EOF'
+    mkdir -p /etc/letsencrypt
+
+    base_file=/etc/letsencrypt/options-ssl-apache.conf
+    # this is from cerbot, see below.
+    dd of=$base_file <<'EOF'
 # Baseline setting to Include for SSL sites
 
 SSLEngine on
@@ -145,14 +165,33 @@ LogFormat "%v %h %l %u %t \"%r\" %>s %b" vhost_common
 #Header edit Set-Cookie (?i)^(.*)(;\s*secure)??((\s*;)?(.*)) "$1; Secure$3$4"
 EOF
 
+    upstream=https://github.com/certbot/certbot/raw/master/certbot-apache/certbot_apache/options-ssl-apache.conf
+    if ! diff -c <(wget -q -O - $upstream) $base_file; then
+        cat <<EOF
+WARNING!!!!!!!!!
+WARNING!!!!!!!!!
+WARNING!!!!!!!!!
+WARNING!!!!!!!!!
+WARNING!!!!!!!!!
+upstream ssl settings differ from the snapshot we have taken!!!
+We diffed with this command:
+diff -c <(wget -q -O - $upstream) $base_file
+Update this script to take care this warning!!!!!
+EOF
+        sleep 1
+    fi
 fi
-sudo tee -a /etc/apache2/sites-enabled/$h.conf <<EOF
+tee -a /etc/apache2/sites-enabled/$h.conf <<EOF
         ErrorLog \${APACHE_LOG_DIR}/error.log
-        CustomLog \${APACHE_LOG_DIR}/access.log combined
+        CustomLog \${APACHE_LOG_DIR}/access.log vhost_combined
 </VirtualHost>
 
 # vim: syntax=apache ts=4 sw=4 sts=4 sr noet
 EOF
 
-s a2enmod ssl rewrite # rewrite needed for httpredir
-ser restart apache2
+a2enmod ssl rewrite # rewrite needed for httpredir
+service apache2 restart
+
+# I rarely look at how much traffic I get, so let's keep that info
+# around for longer than the default of 2 weeks.
+sed -ri --follow-symlinks 's/^(\s*rotate\s).*/\1 365/' /etc/logrotate.d/apache2