Skip to content

Instantly share code, notes, and snippets.

@dmitrymomot
Last active December 20, 2015 03:09
Show Gist options
  • Save dmitrymomot/6061605 to your computer and use it in GitHub Desktop.
Save dmitrymomot/6061605 to your computer and use it in GitHub Desktop.
Creation hosts from CLI
#!/bin/bash
success_color='\E[0;32m'
error_color='\E[0;31m'
end_color="\033[0m"
# custom echo
# Example:
# cwrite 'test' $success_color
# cwrite 'test' $error_color
function cwrite()
{
echo -e $2$1$end_color
tput sgr0
}
ptdr=$HOME/www
default_subdir=http
root_dir=root
echo 'Enter domain name: '
read domain
echo 'Enter the subdirectory for the domain, if it needed (default "http"): '
read subfolder
if [ -z $subfolder ]
then
domain_path=$domain/$default_subdir
elif [ $subfolder == $root_dir ]
then
domain_path=$domain
else
domain_path=$domain/$subfolder
fi
email=webmaster@localhost
echo "Creating a virtual host"
sudo touch /etc/apache2/sites-available/$domain
sudo chmod 777 /etc/apache2/sites-available/$domain
sudo echo -e "<VirtualHost *:80>\nServerName $domain\nServerAlias www.$domain\nServerAdmin webmaster@localhost\nDocumentRoot $ptdr/$domain_path\nErrorLog $ptdr/.apache/logs/$domain/error.log\nCustomLog $ptdr/.apache/logs/$domain/access.log combined\nphp_admin_value open_basedir $ptdr/$domain/\nphp_admin_value doc_root $ptdr/$domain_path/\n</VirtualHost>" > /etc/apache2/sites-available/$domain
echo "Creating directories"
mkdir -p $ptdr/.apache/logs/$domain
mkdir -p $ptdr/$domain_path
echo "Creating file index.php"
touch $ptdr/$domain_path/index.php
echo -e "<?php \n\n echo 'Domain $domain is ready for use';" > $ptdr/$domain_path/index.php
sudo ln -s /etc/apache2/sites-available/$domain /etc/apache2/sites-enabled/$domain
echo "Add $domain and ip in list"
if [ -z $EDITOR ]
then
# echo -e "Variable $EDITOR is empty! \nSet editor for editing"
cwrite "Variable $EDITOR is empty! \nSet editor for editing" $error_color
read custom_editor
else
custom_editor=$EDITOR
fi
if [ $custom_editor !== $EDITOR ]
then
echo "Set as default editor? (y/n)"
read answer
if [ $answer == "y" ]
then
export EDITOR=$custom_editor
cwrite "$custom_editor is set as the default editor." $success_color
fi
fi
if [ -z $custom_editor ]
then
custom_editor=vim
fi
sudo $custom_editor /etc/hosts
echo "Apache restart..."
sudo /etc/init.d/apache2 restart
cwrite 'DONE!' $success_color
echo 'Browser opens...'
x-www-browser "http://$domain"
exit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment