Skip to content

Instantly share code, notes, and snippets.

@fjarrett
Last active April 24, 2017 22:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fjarrett/6df0c306cea8ba0b9c43 to your computer and use it in GitHub Desktop.
Save fjarrett/6df0c306cea8ba0b9c43 to your computer and use it in GitHub Desktop.
Create a new virtual host in Apache
#!/bin/bash
echo "Enter the domain of the site you want to create:"
read DOMAIN
if [ -d /var/www/$DOMAIN ]; then
echo -e "\x1b[1;31mError:\e[0m $DOMAIN already exists!"
exit 1
fi
echo "Making directories..."
sudo mkdir -p /var/www/$DOMAIN/public_html
sudo chown -R www-data:www-data /var/www/$DOMAIN/public_html
sudo mkdir /var/www/$DOMAIN/log
echo "Creating config..."
CONF_FILE="$DOMAIN.conf"
CONF_PATH="/etc/apache2/sites-available/$CONF_FILE"
sudo touch $CONF_PATH
sudo echo '<VirtualHost *:80>' | sudo tee --append $CONF_PATH
sudo echo " ServerName $DOMAIN" | sudo tee --append $CONF_PATH
sudo echo " ServerAlias www.$DOMAIN" | sudo tee --append $CONF_PATH
sudo echo " DocumentRoot /var/www/$DOMAIN/public_html" | sudo tee --append $CONF_PATH
sudo echo " ErrorLog \"| /usr/bin/rotatelogs -t /var/www/$DOMAIN/log/error.log 5M\"" | sudo tee --append $CONF_PATH
sudo echo " CustomLog \"| /usr/bin/rotatelogs -t /var/www/$DOMAIN/log/access.log 5M\" combined" | sudo tee --append $CONF_PATH
sudo echo '</VirtualHost>' | sudo tee --append $CONF_PATH
sudo a2ensite -q $CONF_FILE
echo "Updating hosts..."
IP=$(hostname -I)
sudo echo "$IP $DOMAIN" | sudo tee --append /etc/hosts
echo "Reloading Apache..."
sudo service apache2 force-reload
echo -e "\x1b[1;32mSuccess:\e[0m $DOMAIN created!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment