Skip to content

Instantly share code, notes, and snippets.

@Sental
Created December 29, 2021 14:13
Show Gist options
  • Save Sental/63f7f4916d7e39aeab43136bf966e50c to your computer and use it in GitHub Desktop.
Save Sental/63f7f4916d7e39aeab43136bf966e50c to your computer and use it in GitHub Desktop.
Bash file for creating basic apache virtual host conf files for apache2.4
#!/bin/bash
set -o errexit
############################################################
# die #
############################################################
die () {
echo >&2 "$@"
exit 1
}
############################################################
# Help #
############################################################
Help()
{
# Display Help
echo "apache vhost conf file generator"
echo
echo "Syntax: vhost.sh [-h] <port> <ServerName> <DocumentRoot> <MAGE_RUN_CODE> <MAGE_RUN_TYPE>"
echo "options:"
echo "h Print this Help."
echo "m Move file to apache directory after creation"
echo "All parameters are required if -h is ommitted"
echo "parameters:"
echo "port 80 unless the vhost is being configured for varnish"
echo "ServerName FQDN for the site"
echo "DocumentRoot magento folder that apache looks at. eg. /var/www/magento"
echo "MAGE_RUN_CODE assigned website or store code in the database. Store 0 is 'base' by default"
echo "MAGE_RUN_TYPE Run Type is either 'website' or 'store'. 'website' should be used if the website has only one store view."
echo
}
############################################################
############################################################
# Main program #
############################################################
############################################################
############################################################
# Process the input options. Add options as needed. #
############################################################
move=0
arguments=5
# Get the options
while getopts ":hm:" option; do
case $option in
h) # display Help
Help
exit;;
m) # move conf to apache dir
arguments=6
move=1;;
\?) # Invalid option
echo "Error: Invalid option"
exit;;
esac
done
############################################################
# Check Arguments are there & valid. #
############################################################
[ "$#" -eq $arguments ] || die "$arguments arguments required, $# provided"
if [ "$arguments" -eq 5 ]
then
[[ "$1" != *[^0-9]* ]] || die "The port argument must be a number"
[[ "$2" != *^[a-zA-Z\.]+$* ]] || die "The ServerName argument must be in the correct format eg. example.com"
[[ "$3" != *^[a-zA-Z\.\_\s\/]+$* ]] || die "The DocumentRoot argument must be in the correct format eg. /var/www/magento"
[[ "$4" != *^[a-z]+[a-z0-9\_]*$* ]] || die "The MAGE_RUN_CODE argument must be in the correct format matching the following regex *^[a-z]+[a-z0-9_]*$*"
[[ "$5" != *^[a-z]+$* ]] || die "The MAGE_RUN_TYPE argument must be in lower case and only contain letters, see -h for details."
elif [ "$arguments" -eq 6 ]
then
[[ "$2" != *[^0-9]* ]] || die "The port argument must be a number"
[[ "$3" != *^[a-zA-Z\.]+$* ]] || die "The ServerName argument must be in the correct format eg. example.com"
[[ "$4" != *^[a-zA-Z\.\_\s\/]+$* ]] || die "The DocumentRoot argument must be in the correct format eg. /var/www/magento"
[[ "$5" != *^[a-z]+[a-z0-9\_]*$* ]] || die "The MAGE_RUN_CODE argument must be in the correct format matching the following regex *^[a-z]+[a-z0-9_]*$*"
[[ "$6" != *^[a-z]+$* ]] || die "The MAGE_RUN_TYPE argument must be in lower case and only contain letters, see -h for details."
fi
############################################################
# Assign Arguments. #
############################################################
if [ "$arguments" -eq 5 ]
then
port="$1"
name="$2"
doc="$3"
code="$4"
type="$5"
elif [ "$arguments" -eq 6 ]
then
port="$2"
name="$3"
doc="$4"
code="$5"
type="$6"
fi
############################################################
# Name & create file #
############################################################
OUTFILE="$name"".conf"
(
cat <<sometext
<VirtualHost *:$port>
ServerAdmin webmaster@root.local
ServerName $name
DocumentRoot $doc
<Directory $doc>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
</Directory>
php_flag display_errors 1
php_value error_reporting 4433
#Mod Rewrite
RewriteEngine On
RewriteRule .* - [E=MAGE_RUN_CODE:$code]
RewriteRule .* - [E=MAGE_RUN_TYPE:$type]
</VirtualHost>
# vim: syntax=apache ts=4 sw=4 sts=4 sr noet
sometext
) > $OUTFILE
############################################################
# Confirm file was generated & terminate script. #
############################################################
if [ -f "$OUTFILE" ]
then
echo "Conf created \"$OUTFILE\""
else
echo "Problem in creating file: \"$OUTFILE\""
fi
if [ "$move" -eq 1 ]
then
sudo mv $OUTFILE /etc/apache2/sites-available/$OUTFILE
fi
if [ \( "$move" -eq 1 \) -a \( -f "/etc/apache2/sites-available/$OUTFILE" \) ]
then
echo "Conf \"$OUTFILE\" moved to \"/etc/apache2/sites-available/$OUTFILE\""
elif [ \( "$move" -eq 1 \) -a \( !-f "/etc/apache2/sites-available/$OUTFILE" \) ]
then
echo "Problem in moving file: \"$OUTFILE\""
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment