-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgenerate-certs-mac.sh
executable file
·88 lines (69 loc) · 2.33 KB
/
generate-certs-mac.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#!/bin/bash
# This is heavily borrowed from this answer on StackOverflow:
# https://stackoverflow.com/questions/43752615/enable-apache-ssl-in-docker-for-local-development/57315696#57315696
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )/"
# Grab the site url from .env
if [ -f "${DIR}.env" ]; then
SITE_URL="$(grep SITE_URL ${DIR}.env | cut -d '=' -f 2-)"
else
echo "ERROR: ${DIR}.env does not exist"
exit 1
fi
# Make sure the SITE_URL isn't empty
if [ -z "$SITE_URL" ]; then
echo "ERROR: SITE_URL in ${DIR}.env is empty"
exit 1
fi
# Make sure the homebrew deps are installed
WHICH_MKCERT="$(which mkcert)"
if [ -z "$WHICH_MKCERT" ]; then
echo "ERROR: mkcert is not installed. In the terminal, run: brew install mkcert"
exit 1
fi
if brew ls --versions nss > /dev/null; then
# The package is installed
:
else
echo "ERROR: nss is not installed. In the terminal, run: brew install nss"
exit 1
fi
# Create the certs
mkcert -install "$SITE_URL"
CERT_FILE="$SITE_URL.pem"
KEY_FILE="$SITE_URL-key.pem"
# Copy the certs into the right place
cp "$CERT_FILE" "${DIR}dockerized-dev-env/certs/$CERT_FILE"
cp "$KEY_FILE" "${DIR}dockerized-dev-env/certs/$KEY_FILE"
# Update the apache config
cat > "${DIR}/dockerized-dev-env/config/vhosts/${SITE_URL}-ssl.conf" <<EOF
<VirtualHost *:80>
ServerAdmin webmaster@${SITE_URL}
DocumentRoot "/var/www/html"
ServerName ${SITE_URL}
<Directory "/var/www/html/">
AllowOverride all
</Directory>
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L]
</VirtualHost>
<VirtualHost *:443>
ServerAdmin webmaster@${SITE_URL}
DocumentRoot "/var/www/html"
ServerName ${SITE_URL}
<Directory "/var/www/html/">
AllowOverride all
</Directory>
SSLEngine on
SSLCertificateFile /etc/apache2/ssl/${CERT_FILE}
SSLCertificateKeyFile /etc/apache2/ssl/${KEY_FILE}
</VirtualHost>
EOF
# Add an entry to the /etc/hosts file (I'll uncomment this myself when I use it...)
# echo "Sudo permissions needed to add entry to /etc/hosts."
# sudo -- sh -c -e "echo '\n# Autogenerated entry for ${SITE_URL} by docker-compose-lamp-https\n127.0.0.1 ${SITE_URL}' >> /etc/hosts"
# Make this a file... not a directory
touch "${DIR}dockerized-dev-env/config/php/php.ini"
# Cleanup
rm "$CERT_FILE" "$KEY_FILE"
echo "Done. You will not need to run this script again. Now run docker-compose up!"