1111# to provide certifacate outputs correctly. Runing just as sudo will save certs to sudo's home path
1212# sudo -E ./4a-install-ssl-self-signed-nginx.sh [your-dns-name.local] [3650]
1313
14+ # Prepare text output colours
15+ GREY=' \033[0;37m'
16+ DGREY=' \033[0;90m'
17+ GREYB=' \033[1;37m'
18+ RED=' \033[0;31m'
19+ LRED=' \033[0;91m'
20+ GREEN=' \033[0;32m'
21+ LGREEN=' \033[0;92m'
22+ YELLOW=' \033[0;33m'
23+ LYELLOW=' \033[0;93m'
24+ BLUE=' \033[0;34m'
25+ LBLUE=' \033[0;94m'
26+ CYAN=' \033[0;36m'
27+ LCYAN=' \033[0;96m'
28+ MAGENTA=' \033[0;35m'
29+ LMAGENTA=' \033[0;95m'
30+ NC=' \033[0m' # No Colour
31+
32+ echo
33+ echo
34+ echo -e " ${LGREEN} Setting up self signed SSL certificates for Nginx...${GREY} "
35+ echo
36+
1437# Hack to assist with displaying "$" symbols and " ' quotes in a (cut/pasteable) bash screen output format for Nginx configs
1538SHOWASTEXT1=' $mypwd'
1639SHOWASTEXT2=' "Cert:\LocalMachine\Root"'
1740
1841# Discover all IPv4 interfaces addresses to bind to new SSL certficates
19- echo
2042 echo -e " ${GREY} Discovering the default route interface and DNS names to bind with the new SSL certificate..."
2143 # Dump interface info and copy this output to a temp file
2244 DUMP_IPS=$( ip -o addr show up primary scope global | while read -r num dev fam addr rest; do echo ${addr%/* } ; done)
@@ -38,14 +60,11 @@ if [ $? -ne 0 ]; then
3860 echo -e " ${RED} Failed. See ${LOG_LOCATION}${GREY} " 1>&2
3961 exit 1
4062 else
41- echo -e " ${GREEN } OK${GREY} "
63+ echo -e " ${LGREEN } OK${GREY} "
4264 echo
4365fi
4466
45- echo
46- echo -e " ${GREY} New self signed SSL certificate attributes are shown below...${GREY} "
47- echo -e " ${DGREY} "
48-
67+ echo -e " ${GREY} New self signed SSL certificate attributes are shown below...${DGREY} "
4968# Display the new SSL cert parameters. Prompt for change if required
5069cat << EOF | tee -a $TMP_DIR /cert_attributes.txt
5170[req]
@@ -86,13 +105,6 @@ DIR_SSL_KEY="/etc/nginx/ssl/private"
86105SSLNAME=$1
87106SSLDAYS=$2
88107
89- if [[ $SSLDAYS == " " ]]; then
90- $SSLDAYS = 3650
91- fi
92-
93- echo " Creating a new Certificate ..."
94- openssl req -x509 -nodes -newkey rsa:2048 -keyout $SSLNAME .key -out $SSLNAME .crt -days $SSLDAYS -config $TMP_DIR /cert_attributes.txt
95-
96108# Make directories to place SSL Certificate if they don't exist
97109if [[ ! -d $DIR_SSL_KEY ]]; then
98110 sudo mkdir -p $DIR_SSL_KEY
@@ -102,19 +114,34 @@ if [[ ! -d $DIR_SSL_CERT ]]; then
102114 sudo mkdir -p $DIR_SSL_CERT
103115fi
104116
117+ if [[ $SSLDAYS == " " ]]; then
118+ $SSLDAYS = 3650
119+ fi
120+
121+ echo
122+ echo " {$GREY }Creating a new Nginx SSL Certificate ..."
123+ openssl req -x509 -nodes -newkey rsa:2048 -keyout $SSLNAME .key -out $SSLNAME .crt -days $SSLDAYS -config $TMP_DIR /cert_attributes.txt
124+ if [ $? -ne 0 ]; then
125+ echo -e " ${RED} Failed. See ${LOG_LOCATION}${GREY} " 1>&2
126+ exit 1
127+ else
128+ echo -e " ${LGREEN} OK${GREY} "
129+ echo
130+ fi
131+
105132# Place SSL Certificate within defined path
106133 sudo cp $SSLNAME .key $DIR_SSL_KEY /$SSLNAME .key
107134 sudo cp $SSLNAME .crt $DIR_SSL_CERT /$SSLNAME .crt
108135
109136# Create a PFX formatted key for easier import to Windows hosts and change permissions to enable copying elsewhere
137+ echo -e " ${GREY} Creating client certificates for Windows & Linux...${GREY} "
110138 sudo openssl pkcs12 -export -out $SSLNAME .pfx -inkey $SSLNAME .key -in $SSLNAME .crt -password pass:1234
111139 sudo chmod 0774 $SSLNAME .pfx
112- echo -e " ${GREY} Creating a selection of self signed certificates for Nginx and Windows/Linux browser clients...${GREY} "
113140if [ $? -ne 0 ]; then
114141 echo -e " ${RED} Failed. See ${LOG_LOCATION}${GREY} " 1>&2
115142 exit 1
116143 else
117- echo -e " ${GREEN } OK${GREY} "
144+ echo -e " ${LGREEN } OK${GREY} "
118145 echo
119146fi
120147
@@ -125,12 +152,14 @@ if [ $? -ne 0 ]; then
125152 echo -e " ${RED} Failed. See ${LOG_LOCATION}${GREY} " 1>&2
126153 exit 1
127154 else
128- echo -e " ${GREEN } OK${GREY} "
155+ echo -e " ${LGREEN } OK${GREY} "
129156 echo
130157fi
131158
132159# Update Nginx config to accept the new certificates
133- cat > /etc/nginx/sites-available/$PROXY_SITE << EOL | > /dev/null
160+ echo -e " ${GREY} Configuring Nginx proxy to use self signed SSL certificates and setting up automatic HTTP to HTTPS redirect...${DGREY} "
161+ # cat > /etc/nginx/sites-available/$PROXY_SITE <<EOL | > /dev/null
162+ cat << EOF | tee /etc/nginx/sites-available/$PROXY_SITE
134163server {
135164 #listen 80 default_server;
136165 root /var/www/html;
@@ -167,31 +196,30 @@ server {
167196 access_log off;
168197 }
169198}
170- EOL
171-
172- echo -e " ${GREY} Configuring Nginx proxy to use self signed SSL certificates and setting up automatic HTTP to HTTPS redirect...${GREY} "
199+ EOF
173200if [ $? -ne 0 ]; then
174201 echo -e " ${RED} Failed. See ${LOG_LOCATION}${GREY} " 1>&2
175202 exit 1
176203 else
177- echo -e " ${GREEN } OK${GREY} "
204+ echo -e " ${LGREEN } OK${GREY} "
178205 echo
179206fi
180207
208+
181209printf " ${GREY} +-------------------------------------------------------------------------------------------------------------
182- ${GREEN } + WINDOWS CLIENT SELF SIGNED SSL BROWSER CONFIG - SAVE THIS BEFORE CONTINUING!${GREY}
210+ ${LGREEN } + WINDOWS CLIENT SELF SIGNED SSL BROWSER CONFIG - SAVE THIS BEFORE CONTINUING!${GREY}
183211+
184- + 1. In ${DOWNLOAD_DIR} is a Windows friendly version of the new certificate ${LYELLOW} $SSLNAME .pfx${GREY}
212+ + 1. In ${DOWNLOAD_DIR} is a new Windows friendly version of the new certificate ${LYELLOW} $SSLNAME .pfx${GREY}
185213+ 2. Copy this .pfx file to a location accessible by Windows.
186- + 3. Import the PFX file into your Windows client with the below Powershell commands (as administrator ):
214+ + 3. Import the PFX file into your Windows client with the below Powershell commands (as Administrator ):
187215\n"
188216echo -e " ${SHOWASTEXT1} = ConvertTo-SecureString -String " 1234" -Force -AsPlainText"
189217echo -e " Import-pfxCertificate -FilePath $SSLNAME .pfx -Password " ${SHOWASTEXT1} " -CertStoreLocation " ${SHOWASTEXT2} " "
190218echo -e " (Clear your browser cache and restart your browser to test.)"
191219printf " ${GREY} +-------------------------------------------------------------------------------------------------------------
192- ${GREEN } + LINUX CLIENT SELF SIGNED SSL BROWSER CONFIG - SAVE THIS BEFORE CONTINUING!${GREY}
220+ ${LGREEN } + LINUX CLIENT SELF SIGNED SSL BROWSER CONFIG - SAVE THIS BEFORE CONTINUING!${GREY}
193221+
194- + 1. In In ${DOWNLOAD_DIR} is also the Linux native OpenSSL certificate ${LYELLOW} $SSLNAME .crt${GREY}
222+ + 1. In ${DOWNLOAD_DIR} is a new Linux native OpenSSL certificate ${LYELLOW} $SSLNAME .crt${GREY}
195223+ 2. Copy this file to a location accessible by Linux.
196224+ 3. Import the CRT file into your Linux client certificate store with the below command (as sudo):
197225\n"
@@ -212,7 +240,6 @@ if [ $? -ne 0 ]; then
212240 exit 1
213241else
214242 echo -e " ${LGREEN} OK${GREY} "
215- echo
216243fi
217244
218245# Done
0 commit comments