Skip to content

Instantly share code, notes, and snippets.

@r8928
Last active February 6, 2022 01:21
Show Gist options
  • Save r8928/773c7e648934094bcef3f5a1bf10a042 to your computer and use it in GitHub Desktop.
Save r8928/773c7e648934094bcef3f5a1bf10a042 to your computer and use it in GitHub Desktop.
apache virtual host creation script
#!/bin/bash
if [ $# -eq 0 ]
then
echo "No arguments supplied"
exit;
fi
if [ -z "$2" ]
then
echo "Please enter both arguments. DOMAIN_NAME and DIRECTORY"
exit;
fi
DOMAIN_NAME=$1
WEB_ROOT_DIR=$2
sitesEnable='/etc/apache2/sites-enabled/'
sitesAvailable='/etc/apache2/sites-available/'
sitesAvailabledomain=$sitesAvailable$DOMAIN_NAME.conf
echo "Creating a vhost for $sitesAvailabledomain with a webroot $WEB_ROOT_DIR"
### create virtual host rules file
echo "
<VirtualHost *:80>
ServerName $DOMAIN_NAME
DocumentRoot $WEB_ROOT_DIR
<Directory $WEB_ROOT_DIR/>
AllowOverride All
allow from all
Options +Indexes
</Directory>
</VirtualHost>" > $sitesAvailabledomain
echo -e $"\nNew Virtual Host Created\n"
sed -i "1s/^/127.0.0.1 $DOMAIN_NAME\n/" /etc/hosts
a2ensite $DOMAIN_NAME
sudo systemctl reload apache2
sudo /etc/init.d/webmin restart;
echo "Done, please browse to http://$DOMAIN_NAME to check!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment