Skip to content

Instantly share code, notes, and snippets.

@vladutilie
Last active June 15, 2021 07:13
Show Gist options
  • Save vladutilie/6ae7745d4b97f8316705c1965bb817e4 to your computer and use it in GitHub Desktop.
Save vladutilie/6ae7745d4b97f8316705c1965bb817e4 to your computer and use it in GitHub Desktop.
Create local test domain on MacOS
#!/bin/bash
read -p 'Domain name (no extension): ' DOMAIN_NAME
read -p 'Domain extension [test]: ' DOMAIN_EXT
read -p 'WWW directory path [/Users/vlad/www/]: ' READ_WWW_PATH
read -p 'SSL directory path [/Users/vlad/ssl/]: ' READ_SSL_PATH
WWW_PATH=${READ_WWW_PATH:='/Users/vlad/www'}
EXTENSION=${DOMAIN_EXT:=test}
SSL_PATH=${READ_SSL_PATH:='/Users/vlad/ssl'}
if [ -d "$WWW_PATH" ]; then
cd $WWW_PATH
if mkdir -p "$DOMAIN_NAME"; then
echo "$DOMAIN_NAME created successfully"
echo "# $DOMAIN_NAME.$EXTENSION
<VirtualHost *:80>
DocumentRoot \"$WWW_PATH/$DOMAIN_NAME\"
ServerName $DOMAIN_NAME.$EXTENSION
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://$DOMAIN_NAME.$EXTENSION/\$1 [R,L]
</VirtualHost>
<VirtualHost *:443>
DocumentRoot \"$WWW_PATH/$DOMAIN_NAME\"
ServerName $DOMAIN_NAME.$EXTENSION
SSLEngine on
SSLCertificateFile \"$SSL_PATH/$DOMAIN_NAME/$DOMAIN_NAME.$EXTENSION.pem\"
SSLCertificateKeyFile \"$SSL_PATH/$DOMAIN_NAME/$DOMAIN_NAME.$EXTENSION-key.pem\"
</VirtualHost>
" >>/usr/local/etc/httpd/extra/httpd-vhosts.conf
# check if httpd-vhosts.conf has been written
if [ $? -eq 0 ]; then
echo 'httpd-vhosts.conf written successfully'
cd $SSL_PATH
if mkdir -p "$DOMAIN_NAME" && cd $_; then
echo "$DOMAIN_NAME created in ssl successfully"
mkcert $DOMAIN_NAME.$EXTENSION
# openssl req -x509 -out $DOMAIN_NAME.crt -keyout $DOMAIN_NAME.key \
# -newkey rsa:2048 -nodes -sha256 \
# -subj "/CN=$DOMAIN_NAME.$EXTENSION" -extensions EXT -config <( \
# printf "[dn]\nCN=$DOMAIN_NAME.$EXTENSION\n[req]\ndistinguished_name = dn\n[EXT]\nsubjectAltName=DNS:$DOMAIN_NAME.$EXTENSION\nkeyUsage=digitalSignature\nextendedKeyUsage=serverAuth"
# )
# open $DOMAIN_NAME.crt
sudo apachectl -k restart
else
echo "$DOMAIN_NAME could not be created in ssl directory"
fi
else
echo 'httpd-vhosts.conf could not be written'
fi
else
echo "$DOMAIN_NAME could not be created."
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment