Skip to content

Commit 7dacac7

Browse files
committed
If root dir starts with /, don't use /var/www on nginx
Enable the nginx script to accept absolute path on third parameter instead to use a folder within /var/www
1 parent e6dcfa2 commit 7dacac7

File tree

1 file changed

+22
-15
lines changed

1 file changed

+22
-15
lines changed

virtualhost-nginx.sh

Lines changed: 22 additions & 15 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
sitesEnable='/etc/nginx/sites-enabled/'
1111
sitesAvailable='/etc/nginx/sites-available/'
@@ -28,10 +28,17 @@ do
2828
read domain
2929
done
3030

31-
if [ "$rootdir" == "" ]; then
32-
rootdir=${domain//./}
31+
if [ "$rootDir" == "" ]; then
32+
rootDir=${domain//./}
3333
fi
3434

35+
### if root dir starts with '/', don't use /var/www as default starting point
36+
if [[ "$rootDir" =~ ^/ ]]; then
37+
userDir=''
38+
fi
39+
40+
rootDir=$userDir$rootDir
41+
3542
if [ "$action" == 'create' ]
3643
then
3744
### check if domain already exists
@@ -41,25 +48,25 @@ if [ "$action" == 'create' ]
4148
fi
4249

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

5966
### create virtual host rules file
6067
if ! echo "server {
6168
listen 80;
62-
root $userDir$rootdir;
69+
root $userDir$rootDir;
6370
index index.php index.html index.htm;
6471
server_name $domain;
6572
@@ -117,9 +124,9 @@ if [ "$action" == 'create' ]
117124
fi
118125

119126
if [ "$owner" == "" ]; then
120-
chown -R $(whoami):www-data $userDir$rootdir
127+
chown -R $(whoami):www-data $userDir$rootDir
121128
else
122-
chown -R $owner:www-data $userDir$rootdir
129+
chown -R $owner:www-data $userDir$rootDir
123130
fi
124131

125132
### enable website
@@ -129,7 +136,7 @@ if [ "$action" == 'create' ]
129136
service nginx restart
130137

131138
### show the finished message
132-
echo -e $"Complete! \nYou now have a new Virtual Host \nYour new host is: http://$domain \nAnd its located at $userDir$rootdir"
139+
echo -e $"Complete! \nYou now have a new Virtual Host \nYour new host is: http://$domain \nAnd its located at $userDir$rootDir"
133140
exit;
134141
else
135142
### check whether domain already exists
@@ -152,13 +159,13 @@ if [ "$action" == 'create' ]
152159
fi
153160

154161
### check if directory exists or not
155-
if [ -d $userDir$rootdir ]; then
162+
if [ -d $userDir$rootDir ]; then
156163
echo -e $"Delete host root directory ? (s/n)"
157164
read deldir
158165

159166
if [ "$deldir" == 's' -o "$deldir" == 'S' ]; then
160167
### Delete the directory
161-
rm -rf $userDir$rootdir
168+
rm -rf $userDir$rootDir
162169
echo -e $"Directory deleted"
163170
else
164171
echo -e $"Host directory conserved"

0 commit comments

Comments
 (0)