Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
125 changes: 123 additions & 2 deletions virtualhost.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ TEXTDOMAIN=virtualhost
action=$1
domain=$2
rootDir=$3
port=$4
owner=$(who am i | awk '{print $1}')
email='webmaster@localhost'
sitesEnable='/etc/apache2/sites-enabled/'
Expand All @@ -20,9 +21,9 @@ if [ "$(whoami)" != 'root' ]; then
exit 1;
fi

if [ "$action" != 'create' ] && [ "$action" != 'delete' ]
if [ "$action" != 'create' ] && [ "$action" != 'delete' ] && [ "$action" != 'create-proxy' ]
then
echo $"You need to prompt for action (create or delete) -- Lower-case only"
echo $"You need to prompt for action (create or create-proxy or delete) -- Lower-case only"
exit 1;
fi

Expand All @@ -43,6 +44,126 @@ fi

rootDir=$userDir$rootDir

if [ "$action" == 'create-proxy' ]
then
### check if domain already exists
if [ -e $sitesAvailabledomain ]; then
echo -e $"This domain already exists.\nPlease Try Another one"
exit;
fi

### check if directory exists or not
if ! [ -d $rootDir ]; then
### create the directory
mkdir $rootDir
### give permission to root dir
chmod 755 $rootDir
### write test file in the new domain dir
if ! echo "<?php echo phpinfo(); ?>" > $rootDir/phpinfo.php
then
echo $"ERROR: Not able to write in file $rootDir/phpinfo.php. Please check permissions"
exit;
else
echo $"Added content to $rootDir/phpinfo.php"
fi
fi

### create virtual host rules file
if ! echo "
<VirtualHost *:80>
ServerAdmin $email
ServerName $domain
ServerAlias $domain
DocumentRoot $rootDir
<Directory />
Options -Indexes +FollowSymLinks
AllowOverride None
Require all granted
</Directory>
ProxyPreserveHost On
ProxyVia Full
<Proxy *>
Require all granted
</Proxy>
ProxyPass / http://127.0.0.1:$port/
ProxyPassReverse / http://127.0.0.1:$port/
ErrorLog /var/log/apache2/$domain-error.log
LogLevel error
CustomLog /var/log/apache2/$domain-access.log combined
</VirtualHost>" > $sitesAvailabledomain
then
echo -e $"There is an ERROR creating $domain file"
exit;
else
echo -e $"\nNew Virtual Host Created\n"
fi

### Add domain in /etc/hosts
if ! echo "127.0.0.1 $domain" >> /etc/hosts
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):$(whoami) $rootDir
else
chown -R $owner:$owner $rootDir
fi

### enable website
a2ensite $domain

### restart Apache
/etc/init.d/apache2 reload

### show the finished message
echo -e $"Complete! \nYou now have a new Virtual Host \nYour new host is: http://$domain \nAnd its located at $rootDir"
exit;

else
### check whether domain already exists
if ! [ -e $sitesAvailabledomain ]; then
echo -e $"This domain does not exist.\nPlease try another one"
exit;
else
### Delete domain in /etc/hosts
newhost=${domain//./\\.}
sed -i "/$newhost/d" /etc/hosts

### disable website
a2dissite $domain

### restart Apache
/etc/init.d/apache2 reload

### Delete virtual host rules files
rm $sitesAvailabledomain
fi

### check if directory exists or not
if [ -d $rootDir ]; then
echo -e $"Delete host root directory ? (y/n)"
read deldir

if [ "$deldir" == 'y' -o "$deldir" == 'Y' ]; then
### Delete the directory
rm -rf $rootDir
echo -e $"Directory deleted"
else
echo -e $"Host directory conserved"
fi
else
echo -e $"Host directory not found. Ignored"
fi

### show the finished message
echo -e $"Complete!\nYou just removed Virtual Host $domain"
exit 0;
fi

if [ "$action" == 'create' ]
then
### check if domain already exists
Expand Down