Created
January 7, 2017 08:51
-
-
Save shoaibali/b9248e613c4f3215572a26fb7e6cdae3 to your computer and use it in GitHub Desktop.
This is a shell script that automatically adds a vhost entry and /etc/hosts entry for a given project
This file contains hidden or 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 | |
### Author: Shoaib Ali <[email protected]> | |
### This is a shell script that automatically adds a vhost entry and /etc/hosts entry for a given project | |
### Usage: ./virtualhost.sh mywebsite.dev mywebsite | |
### Future Enhancements: SSL Support, Configuration based on Nginx/Apache2, git clone etc. | |
### Set default parameters | |
domain=$1 | |
project=$2 | |
rootDir='/usr/local/var/www/htdocs/' | |
vhostFile='/usr/local/etc/apache2/2.4/extra/httpd-vhosts.conf' | |
owner=$(who am i | awk '{print $1}') | |
group=$(stat "$rootDir" | awk '{print $6'}) | |
email='webmaster@localhost' | |
logDirectory='/usr/local/var/log/apache2/' | |
### don't modify from here unless you know what you are doing #### | |
# if [ "$(whoami)" != 'root' ]; then | |
# echo $"You have no permission to run as non-root user. Use sudo, we need it to restart webserver " | |
# exit 1; | |
# fi | |
if [ ! -d "$rootDir" ]; then | |
echo -e $"Please make sure $rootDir exists!" | |
exit; | |
fi | |
# check to see if they provided a domain name | |
while [ "$domain" == "" ] | |
do | |
echo -e $"Please provide local development domain name you will be accessing this with. e.g. mywebsite.dev" | |
read domain | |
done | |
# check to see if they provide us with project directory | |
while [ "$project" == "" ] | |
do | |
echo -e $"Please provide project name. e.g. mywebsite" | |
read project | |
done | |
# check to see if this domain already exists in vhost file or not | |
existingDomain=`cat "$vhostFile" | grep -F "$domain" | wc -l` | |
if [[ $existingDomain -gt 0 ]]; then | |
echo -e $"Sorry, $domain already exists in vhost" | |
exit; | |
# else | |
#echo -e $"Checked for existing vhost for $domain" | |
fi | |
projectDir=$rootDir$project | |
### check if directory exists or not | |
if ! [ -d $projectDir ]; then | |
### create the directory | |
mkdir $projectDir | |
### give permission to root dir | |
chmod 755 $projectDir | |
### write test file in the new domain dir | |
if ! echo "<?php echo 'Temporary index for $domain' ?>" > $projectDir/index.php | |
then | |
echo $"ERROR: Not able to write in file $projectDir/index.php. Please check permissions" | |
exit; | |
else | |
echo $"Added content to $projectDir/index.php" | |
fi | |
fi | |
### create virtual host rules file | |
if ! echo " | |
<VirtualHost *:80> | |
ServerAdmin $email | |
ServerName $domain | |
ServerAlias $domain | |
DocumentRoot $projectDir | |
<Directory /> | |
AllowOverride All | |
</Directory> | |
<Directory $projectDir> | |
Options Indexes FollowSymLinks MultiViews | |
AllowOverride all | |
Require all granted | |
</Directory> | |
ErrorLog $logDirectory$domain-error_log | |
CustomLog $logDirectory$domain-access_log common | |
LogLevel error | |
</VirtualHost>" >> $vhostFile | |
then | |
echo -e $"There is an ERROR creating $domain vhost entry" | |
exit; | |
else | |
echo -e $"\nNew Virtual Host Created\n" | |
fi | |
echo -e $"Sudo password is required to write to /etc/hosts file and restart webserver" | |
### Add domain in /etc/hosts | |
if ! echo "127.0.0.1 $domain" | sudo tee -a /etc/hosts > /dev/null | |
then | |
echo $"ERROR: Not able to write in /etc/hosts" | |
exit; | |
else | |
echo -e $"Host added to /etc/hosts file \n" | |
fi | |
if [ "$owner" == "" ]; then | |
chown -R $(whoami):$(group) $projectDir | |
else | |
chown -R $owner:$group $projectDir | |
fi | |
### stopping/starting apache | |
sudo apachectl stop | |
sudo apachectl start | |
### show the finished message | |
echo -e $"Complete! \nYou now have a new Virtual Host \nYour new host is: http://$domain \nAnd its located at $projectDir" | |
exit; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment