d99bf9698c48169bd8ccefb58df238a7104719db
[basic-https-conf] / apache-site
1 #!/bin/bash
2 # Copyright (C) 2016 Ian Kelling
3
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
7
8 # http://www.apache.org/licenses/LICENSE-2.0
9
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.
15
16 set -eE -o pipefail
17 trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?" >&2' ERR
18
19 usage() {
20 cat <<EOF
21 Usage: ${0##*/} [EXTRA_SETTINGS_FILE] DOMAIN
22 Setup apache virtualhost config with https using
23 ssl config provided by let's encrypt and my standard
24 location for storing certs.
25
26 EXTRA_SETTINGS_FILE can be - for stdin
27 -p PORT
28 -i Insecure, no ssl
29 -c CERT_DIR Default is /p/c/machine_specific/\$HOSTNAME/webservercerts
30 -h|--help Print help and exit
31 -r DocumentRoot
32 -- Subsequent arguments are never treated as options
33
34 Note: options and non-options can be in any order.
35 EOF
36 exit $1
37 }
38
39 ##### begin command line parsing ########
40
41 cert_dir=/p/c/machine_specific/$HOSTNAME/webservercerts
42 ssl=true
43 extra_settings=
44 args=()
45 port="*:443"
46 while [[ $1 ]]; do
47 case $1 in
48 -i) ssl=false; shift ;; # i for insecure
49 -c) cert_dir="$2"; shift 2 ;;
50 -p) port="$2"; shift 2 ;;
51 -r) root="$2"; shift 2 ;;
52 --) shift; break ;;
53 -?*|-h|--help) usage ;;
54 *) args+=("$1"); shift ;;
55 esac
56 done
57 args+=("$@")
58
59 if (( ${#args[@]} == 2 )); then
60 read extra_settings h <<<"${args[@]}"
61 else
62 read h <<<"${args[@]}"
63 fi
64
65 if [[ ! $h ]]; then
66 echo "$0: error: expected domain arg"
67 usage 1
68 fi
69
70 if [[ ! $root ]]; then
71 root=/var/www/$h/html
72 fi
73
74
75 ##### end command line parsing ########
76
77 # taken from the let's encrypt generated site, using
78 # ./certbot-auto --apache (should use the test mode to check if there are updates)
79 # on 5/29/2016
80
81 # I could have also used the mozilla generator this, but it had some open issues
82 # with no response
83 # so I figured I would check out let's encrypt.
84 # It's a little more liberal, but still get's an A in ssl labs,
85 # so, meh, I'll use it.
86 # https://mozilla.github.io/server-side-tls/ssl-config-generator/
87
88
89 sudo rm -f /etc/apache2/sites-enabled/000-default.conf
90
91 sudo mkdir -p $root
92 sudo dd of=/etc/apache2/sites-enabled/$h.conf <<EOF
93 <VirtualHost $port>
94 ServerName $h
95 ServerAlias www.$h
96 DocumentRoot $root
97 EOF
98
99 if [[ $extra_settings ]]; then
100 cat -- $extra_settings | sudo tee -a /etc/apache2/sites-enabled/$h.conf
101 fi
102
103 if $ssl; then
104 sudo tee -a /etc/apache2/sites-enabled/$h.conf <<EOF
105 SSLCertificateFile $cert_dir/$h-chained.pem
106 SSLCertificateKeyFile $cert_dir/$h-domain.key
107 Include /etc/letsencrypt/options-ssl-apache.conf
108 EOF
109
110 sudo dd of=/etc/apache2/sites-enabled/httpsredir.conf <<'EOF'
111 <VirtualHost *:80>
112 ServerAdmin webmaster@localhost
113 DocumentRoot /var/www/html
114
115
116 ErrorLog ${APACHE_LOG_DIR}/error.log
117 CustomLog ${APACHE_LOG_DIR}/access.log combined
118
119 RewriteEngine on
120 # ian: removed so it's for all sites
121 #RewriteCond %{SERVER_NAME} =certbot.iank.bid
122 RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,QSA,R=permanent]
123 </VirtualHost>
124 EOF
125
126 sudo mkdir -p /etc/letsencrypt
127 sudo dd of=/etc/letsencrypt/options-ssl-apache.conf <<'EOF'
128 # Baseline setting to Include for SSL sites
129
130 SSLEngine on
131
132 # Intermediate configuration, tweak to your needs
133 SSLProtocol all -SSLv2 -SSLv3
134 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
135 SSLHonorCipherOrder on
136 SSLCompression off
137
138 SSLOptions +StrictRequire
139
140 # Add vhost name to log entries:
141 LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-agent}i\"" vhost_combined
142 LogFormat "%v %h %l %u %t \"%r\" %>s %b" vhost_common
143
144 #CustomLog /var/log/apache2/access.log vhost_combined
145 #LogLevel warn
146 #ErrorLog /var/log/apache2/error.log
147
148 # Always ensure Cookies have "Secure" set (JAH 2012/1)
149 #Header edit Set-Cookie (?i)^(.*)(;\s*secure)??((\s*;)?(.*)) "$1; Secure$3$4"
150 EOF
151
152 fi
153 sudo tee -a /etc/apache2/sites-enabled/$h.conf <<EOF
154 ErrorLog \${APACHE_LOG_DIR}/error.log
155 CustomLog \${APACHE_LOG_DIR}/access.log combined
156 </VirtualHost>
157
158 # vim: syntax=apache ts=4 sw=4 sts=4 sr noet
159 EOF
160
161 sudo a2enmod ssl rewrite # rewrite needed for httpredir
162 sudo service apache2 restart