X-Git-Url: https://iankelling.org/git/?p=basic-https-conf;a=blobdiff_plain;f=apache-site;h=8ff08b8d63594bdffd2dbb19dea991d31cf41a3f;hp=8a67722ada42f9487a7fa6408a0a9d29d7612784;hb=79707a58470ef41c8b1f07d1b9fb5b4cbfe46c6c;hpb=74fad776536c29ecc45395259ea19fd3b1cf5cac diff --git a/apache-site b/apache-site index 8a67722..8ff08b8 100755 --- a/apache-site +++ b/apache-site @@ -13,29 +13,30 @@ # See the License for the specific language governing permissions and # limitations under the License. -# run as root. [[ $EUID == 0 ]] || exec sudo -E "$BASH_SOURCE" "$@" set -eE -o pipefail trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?" >&2' ERR +shopt -s nullglob # used in apache config file expansion + usage() { cat < +vhost_file=/etc/apache2/sites-enabled/$h.conf +redir_file=/etc/apache2/sites-enabled/httpsredir.conf + +# note, we exepct ServerRoot of /etc/apache2 +# 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 +cd /etc/apache2 +conf_files=(apache2.conf) + + +if [[ $port == 80 ]]; then + ssl=false + # remove any thats hanging around + rm -f $redir_file +fi + +for (( 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,Ip" "$f"); do + case $p in + 80) listen_80=true ;; + $port) listen_port=true ;; + esac + done +done + +if $ssl; then + https_arg=" https" +fi + + +echo "$0: creating $vhost_file" +cat >$vhost_file < ServerName $h ServerAlias www.$h DocumentRoot $root EOF if [[ $extra_settings ]]; then - cat -- $extra_settings | tee -a /etc/apache2/sites-enabled/$h.conf + cat -- $extra_settings >>$vhost_file 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 <>$vhost_file <>$vhost_file <>$vhost_file <$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 ServerAdmin webmaster@localhost DocumentRoot /var/www/html @@ -137,12 +202,18 @@ RewriteEngine on RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,QSA,R=permanent] EOF + if ! $listen_80; then + cat >>$redir_file <<'EOF' +Listen 80 +EOF + fi + fi mkdir -p /etc/letsencrypt - base_file=/etc/letsencrypt/options-ssl-apache.conf # this is from cerbot, see below. - dd of=$base_file <<'EOF' + echo "$0: creating $certbot_ssl_conf" + cat >$certbot_ssl_conf <<'EOF' # Baseline setting to Include for SSL sites SSLEngine on @@ -168,7 +239,7 @@ LogFormat "%v %h %l %u %t \"%r\" %>s %b" vhost_common 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 + if ! diff -c <(wget -q -O - $upstream) $certbot_ssl_conf; then cat <>$vhost_file < +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 + cat >>$vhost_file <