Last active
April 24, 2017 22:33
-
-
Save fjarrett/6df0c306cea8ba0b9c43 to your computer and use it in GitHub Desktop.
Create a new virtual host in Apache
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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