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