Last active
December 22, 2015 06:09
-
-
Save Illizian/6428793 to your computer and use it in GitHub Desktop.
A zsh / bash script for setting up Apache virtual hosts automatically on ubuntu. Add to your .zshrc or .profile and run www domain.com
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
www() { | |
bold=`tput bold` | |
normal=`tput sgr0` | |
if [ ! $1 ] ; then | |
print "Usage: \n$ www url" | |
return 1 | |
elif [ $1 ] ; then | |
username=$(id -un)@$(hostname) | |
echo "${bold}Creating www directories in /var/www/$1/${normal}" | |
mkdir -pv /var/www/$1/htdocs /var/www/$1/logs | |
echo "${bold}Creating landing page at /var/www/$1/htdocs/index.html${normal}" | |
printf "<html><body><h1>Welcome to the "$1" Virtual Host!</h1><p>This is the default web page for this host.</p></body></html>" | sudo tee /var/www/$1/htdocs/index.html | |
echo "\n${bold}Creating new default config at /etc/apache2/sites-available/$1${normal}" | |
printf "<VirtualHost *:80>\n ServerAdmin "$username"\n ServerName "$1"\n ServerAlias www."$1"\n DirectoryIndex index.html index.php server.php\n DocumentRoot /var/www/"$1"/htdocs/\n <Directory /var/www/"$1"/htdocs/>\n Options Indexes FollowSymLinks MultiViews\n AllowOverride All\n Order allow,deny\n allow from all\n </Directory>\n\n ErrorLog /var/www/"$1"/logs/error.log\n CustomLog /var/www/"$1"/logs/access.log combined\n</VirtualHost>" | sudo tee /etc/apache2/sites-available/$1 | |
echo "\n\n${bold}Enabling new configuration${normal}" | |
sudo a2ensite $1 | |
echo "\n${bold}Restarting apache2 service${normal}" | |
sudo service apache2 restart | |
echo "\n${bold}Creating new entry in /etc/hosts${normal}" | |
printf "\n#Entry autogenerated by bash script\n127.0.0.1 "$1"\n" | sudo tee -a /etc/hosts | |
fi | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment