allow alternate ports for apache
authorIan Kelling <ian@iankelling.org>
Sun, 2 Apr 2017 01:06:01 +0000 (18:06 -0700)
committerIan Kelling <ian@iankelling.org>
Mon, 3 Apr 2017 00:22:41 +0000 (17:22 -0700)
apache-site
nginx-site

index 9a22d2aad73a308d4bffc0b4fd6c6e2a85e2dcf7..5e3f8c463d07d6c267e9d72a7b6e7754721a98be 100755 (executable)
@@ -20,7 +20,7 @@ trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?" >&2' ERR
 
 usage() {
     cat <<EOF
 
 usage() {
     cat <<EOF
-Usage: ${0##*/} [EXTRA_SETTINGS_FILE] DOMAIN
+Usage: ${0##*/} [OPTIONS] [EXTRA_SETTINGS_FILE] DOMAIN
 Setup apache virtualhost config with https using
 ssl config provided by let's encrypt and my standard
 location for storing certs.
 Setup apache virtualhost config with https using
 ssl config provided by let's encrypt and my standard
 location for storing certs.
@@ -29,7 +29,7 @@ EXTRA_SETTINGS_FILE can be - for stdin
 -c CERT_DIR       In priority: this arg, $ACME_TINY_WRAPPER_CERT_DIR,
                   $HOME/webservercerts, if the other options aren't set.
 -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.
 -i                Insecure, no ssl
--p PORT           Main port to listen on, default 443
+-p ADDR_PORT      Main address and port to listen on, default *:443
 -r                DocumentRoot
 -h|--help         Print help and exit
 
 -r                DocumentRoot
 -h|--help         Print help and exit
 
@@ -46,14 +46,14 @@ if [[ ! $cert_dir ]]; then
 fi
 ssl=true
 extra_settings=
 fi
 ssl=true
 extra_settings=
-port="*:443"
+addr_port="*:443"
 temp=$(getopt -l help ic:p:r:h "$@") || usage 1
 eval set -- "$temp"
 while true; do
     case $1 in
         -i) ssl=false; shift ;;
         -c) cert_dir="$2"; shift 2 ;;
 temp=$(getopt -l help ic:p:r:h "$@") || usage 1
 eval set -- "$temp"
 while true; do
     case $1 in
         -i) ssl=false; shift ;;
         -c) cert_dir="$2"; shift 2 ;;
-        -p) port="$2"; shift 2 ;;
+        -p) addr_port="$2"; shift 2 ;;
         -r) root="$2"; shift 2 ;;
         --) shift; break ;;
         -h|--help) usage ;;
         -r) root="$2"; shift 2 ;;
         --) shift; break ;;
         -h|--help) usage ;;
@@ -76,6 +76,8 @@ if [[ ! $root ]]; then
     root=/var/www/$h/html
 fi
 
     root=/var/www/$h/html
 fi
 
+port=${addr_port##*:}
+
 
 ##### end command line parsing ########
 
 
 ##### end command line parsing ########
 
@@ -95,8 +97,38 @@ rm -f /etc/apache2/sites-enabled/000-default.conf
 
 mkdir -p $root
 vhost_file=/etc/apache2/sites-enabled/$h.conf
 
 mkdir -p $root
 vhost_file=/etc/apache2/sites-enabled/$h.conf
-cat >$vhost_file <<EOF
-<VirtualHost $port>
+redir_file=/etc/apache2/sites-enabled/httpsredir.conf
+
+# note, we exepct ServerRoot of /etc/apache2
+cd /etc/apache2
+conf_files=(apache2.conf)
+
+# apache requires exactly 1 listen directive per port (when no ip is also given),
+# so we have to parse the config to do it programatically.
+listen_80=false
+listen_port=false
+while (( i=0; i < ${#conf_files[@]}; i++ )); do
+    f="${conf_files[i]}"
+    # note: globs are expanded here:
+    conf_files+=( $(sed -rn "s,^\s*Include(Optional)?\s+(\S+).*,\2,p" "$f") )
+    case $(readlink -f "$f") in
+        $vhost_file|$redir_file) continue ;;
+    esac
+    for p in $(sed -rn "s,^\s*Listen\s+(\S+).*,\1,p" "$f"); do
+        case $p in
+            80) listen_80=true ;;
+            $port) listen_port=true ;;
+        esac
+    done
+done
+
+if $ssl; then
+    https_arg=" https"
+fi
+
+
+tee $vhost_file <<EOF
+<VirtualHost $addr_port>
         ServerName $h
         ServerAlias www.$h
         DocumentRoot $root
         ServerName $h
         ServerAlias www.$h
         DocumentRoot $root
@@ -110,7 +142,7 @@ fi
 if [[ -e /etc/apache2/mods-available/http2.load ]]; then
     # https://httpd.apache.org/docs/2.4/mod/mod_http2.html
     a2enmod http2
 if [[ -e /etc/apache2/mods-available/http2.load ]]; then
     # https://httpd.apache.org/docs/2.4/mod/mod_http2.html
     a2enmod http2
-tee -a $vhost_file <<EOF
+    tee -a $vhost_file <<EOF
         Protocols h2 http/1.1
 EOF
 fi
         Protocols h2 http/1.1
 EOF
 fi
@@ -123,7 +155,10 @@ if $ssl; then
         Include $certbot_ssl_conf
 EOF
 
         Include $certbot_ssl_conf
 EOF
 
-    dd of=/etc/apache2/sites-enabled/httpsredir.conf <<'EOF'
+    # if we are using a non-standard port, setup don't setup
+    # irrelevant 443 redirect.
+    if [[ $port == "443" ]]; then
+        tee $redir_file <<'EOF'
 # vhost_combined with %D (request time in microseconds)
 # this file is just a convenient place to drop it.
 LogFormat "%v:%p %h %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\" %D" vhost_time_combined
 # vhost_combined with %D (request time in microseconds)
 # this file is just a convenient place to drop it.
 LogFormat "%v:%p %h %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\" %D" vhost_time_combined
@@ -140,11 +175,17 @@ RewriteEngine on
 RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,QSA,R=permanent]
 </VirtualHost>
 EOF
 RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,QSA,R=permanent]
 </VirtualHost>
 EOF
+        if ! $listen_80; then
+            tee -a $redir_file <<'EOF'
+Listen 80
+EOF
+        fi
+    fi
 
     mkdir -p /etc/letsencrypt
 
     # this is from cerbot, see below.
 
     mkdir -p /etc/letsencrypt
 
     # this is from cerbot, see below.
-     cat >$certbot_ssl_conf <<'EOF'
+    cat >$certbot_ssl_conf <<'EOF'
 # Baseline setting to Include for SSL sites
 
 SSLEngine on
 # Baseline setting to Include for SSL sites
 
 SSLEngine on
@@ -189,9 +230,15 @@ tee -a $vhost_file <<EOF
         ErrorLog \${APACHE_LOG_DIR}/error.log
         CustomLog \${APACHE_LOG_DIR}/access.log vhost_time_combined
 </VirtualHost>
         ErrorLog \${APACHE_LOG_DIR}/error.log
         CustomLog \${APACHE_LOG_DIR}/access.log vhost_time_combined
 </VirtualHost>
+EOF
 
 
-# vim: syntax=apache ts=4 sw=4 sts=4 sr noet
+if ! $listen_port; then
+    # reference: https://httpd.apache.org/docs/2.4/mod/mpm_common.html#listen
+    tee -a $vhost_file <<EOF
+listen ${port}${https_arg}
 EOF
 EOF
+fi
+
 
 a2enmod ssl rewrite # rewrite needed for httpredir
 service apache2 restart
 
 a2enmod ssl rewrite # rewrite needed for httpredir
 service apache2 restart
index 5e555c74f90a4ef121cb84e26823260e66e31837..f66b40c86b40db48d0601a287abe2f7dcecbaaa8 100755 (executable)
@@ -21,7 +21,7 @@ trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?" >&2' ERR
 
 usage() {
     cat <<EOF
 
 usage() {
     cat <<EOF
-Usage: ${0##*/} [EXTRA_SETTINGS_FILE] DOMAIN
+Usage: ${0##*/} [OPTIONS] [EXTRA_SETTINGS_FILE] DOMAIN
 Note: this is less tested and mature than the apache site script.
 
 Setup nginx config with https using
 Note: this is less tested and mature than the apache site script.
 
 Setup nginx config with https using