Skip to content

Commit 494e44c

Browse files
committed
If root dir starts with /, don't use /var/www as default starting point
1 parent ce99290 commit 494e44c

File tree

1 file changed

+21
-14
lines changed

1 file changed

+21
-14
lines changed

virtualhost.sh

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ TEXTDOMAIN=virtualhost
55
### Set default parameters
66
action=$1
77
domain=$2
8-
rootdir=$3
8+
rootDir=$3
99
owner=$(who am i | awk '{print $1}')
1010
email='webmaster@localhost'
1111
sitesEnable='/etc/apache2/sites-enabled/'
@@ -32,10 +32,17 @@ do
3232
read domain
3333
done
3434

35-
if [ "$rootdir" == "" ]; then
35+
if [ "$rootDir" == "" ]; then
3636
rootdir=${domain//./}
3737
fi
3838

39+
### if root dir starts with '/', don't use /var/www as default starting point
40+
if [[ "$rootDir" =~ ^/ ]]; then
41+
userDir=''
42+
fi
43+
44+
rootDir=$userDir$rootDir
45+
3946
if [ "$action" == 'create' ]
4047
then
4148
### check if domain already exists
@@ -45,18 +52,18 @@ if [ "$action" == 'create' ]
4552
fi
4653

4754
### check if directory exists or not
48-
if ! [ -d $userDir$rootdir ]; then
55+
if ! [ -d $rootDir ]; then
4956
### create the directory
50-
mkdir $userDir$rootdir
57+
mkdir $rootDir
5158
### give permission to root dir
52-
chmod 755 $userDir$rootdir
59+
chmod 755 $rootDir
5360
### write test file in the new domain dir
54-
if ! echo "<?php echo phpinfo(); ?>" > $userDir$rootdir/phpinfo.php
61+
if ! echo "<?php echo phpinfo(); ?>" > $rootDir/phpinfo.php
5562
then
5663
echo $"ERROR: Not able to write in file $userDir/$rootdir/phpinfo.php. Please check permissions"
5764
exit;
5865
else
59-
echo $"Added content to $userDir$rootdir/phpinfo.php"
66+
echo $"Added content to $rootDir/phpinfo.php"
6067
fi
6168
fi
6269

@@ -66,11 +73,11 @@ if [ "$action" == 'create' ]
6673
ServerAdmin $email
6774
ServerName $domain
6875
ServerAlias $domain
69-
DocumentRoot $userDir$rootdir
76+
DocumentRoot $rootDir
7077
<Directory />
7178
AllowOverride All
7279
</Directory>
73-
<Directory $userDir$rootdir>
80+
<Directory $rootDir>
7481
Options Indexes FollowSymLinks MultiViews
7582
AllowOverride all
7683
Require all granted
@@ -96,9 +103,9 @@ if [ "$action" == 'create' ]
96103
fi
97104

98105
if [ "$owner" == "" ]; then
99-
chown -R $(whoami):$(whoami) $userDir$rootdir
106+
chown -R $(whoami):$(whoami) $rootDir
100107
else
101-
chown -R $owner:$owner $userDir$rootdir
108+
chown -R $owner:$owner $rootDir
102109
fi
103110

104111
### enable website
@@ -108,7 +115,7 @@ if [ "$action" == 'create' ]
108115
/etc/init.d/apache2 reload
109116

110117
### show the finished message
111-
echo -e $"Complete! \nYou now have a new Virtual Host \nYour new host is: http://$domain \nAnd its located at $userDir$rootdir"
118+
echo -e $"Complete! \nYou now have a new Virtual Host \nYour new host is: http://$domain \nAnd its located at $rootDir"
112119
exit;
113120
else
114121
### check whether domain already exists
@@ -131,13 +138,13 @@ if [ "$action" == 'create' ]
131138
fi
132139

133140
### check if directory exists or not
134-
if [ -d $userDir$rootdir ]; then
141+
if [ -d $rootDir ]; then
135142
echo -e $"Delete host root directory ? (y/n)"
136143
read deldir
137144

138145
if [ "$deldir" == 'y' -o "$deldir" == 'Y' ]; then
139146
### Delete the directory
140-
rm -rf $userDir$rootdir
147+
rm -rf $rootDir
141148
echo -e $"Directory deleted"
142149
else
143150
echo -e $"Host directory conserved"

0 commit comments

Comments
 (0)