-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathch.sh
143 lines (121 loc) · 3.31 KB
/
ch.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#!/bin/bash
### Set default parameters
action=$1
domain=$2
owner=$(who am i | awk '{print $1}')
email='[email protected]'
sitesEnable='/etc/apache2/sites-enabled/'
sitesAvailable='/etc/apache2/sites-available/'
userDir='/var/www/'
if [ "$(whoami)" != 'root' ]; then
echo "You have no permission to run $0 as non-root user. Use sudo !!!"
exit 1;
fi
if [ "$action" != 'create' ] && [ "$action" != 'delete' ]
then
echo "You need to prompt for action (create or delete) -- Lower-case only !!!!!!"
exit 1;
fi
while [ "$domain" == "" ]
do
echo -e "Please provide domain. e.g.dev,staging"
read domain
done
#rootdir=${domain//./}
rootdir=$domain
if [ "$action" == 'create' ]
then
### check if domain already exists
if [ -e $sitesAvailable$domain ]; then
echo -e 'This domain already exists.\nPlease Try Another one'
exit;
fi
### check if directory exists or not
if [ -d $userDir$rootdir ]; then
echo -e 'Directory already exists !'
exit;
fi
### create the directory
mkdir $userDir$rootdir
### create virtual host rules file
if ! echo "<VirtualHost *:80>
ServerAdmin hostmaster@localhost
ServerName $domain
ServerAlias $domain www.$domain
DocumentRoot $userDir$rootdir
<Directory />
AllowOverride All
</Directory>
<Directory $rootdir>
AllowOverride All
</Directory>
ErrorLog /var/log/apache2/$domain
LogLevel error
CustomLog /var/log/apache2/$domain custom
</VirtualHost>" > $sitesAvailable$domain".conf"
then
echo -e 'There is an ERROR create $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 write in /etc/hosts"
exit;
else
echo -e "Host added to /etc/hosts file \n"
fi
### enable website
a2ensite $domain".conf"
### restart Apache
service apache2 reload
### give permission to root dir
chmod 755 $userDir$rootdir
### write test file in the new domain dir
if ! echo "<?php echo phpinfo(); ?>" > $userDir$rootdir/index.php
then
echo "ERROR: Not able to write in file "$userDir"/"$rootdir"/index.php. Please check permissions."
exit;
else
echo "Added content to "$userDir$rootdir"/phpinfo.php."
fi
if [ "$owner" == "" ]; then
chown -R $(whoami):$(whoami) $userDir$rootdir
else
chown -R $owner:$owner $userDir$rootdir
fi
### show the finished message
echo -e "Complete!
You now have a new Virtual Host
Your new host is: http://"$domain"
And its located at "$userDir$rootdir
exit;
else
### check whether domain already exists
if ! [ -e $sitesAvailable$domain".conf" ]; then
echo -e 'This domain dont exists.\nPlease Try Another one'
exit;
fi
### check if directory exists or not
if ! [ -d $userDir$rootdir ]; then
echo -e 'Directory not exists !'
exit;
fi
### Delete the directory
rm -rf $userDir$rootdir
### Delete virtual host rules file
rm $sitesAvailable$domain".conf"
### Delete domain in /etc/hosts
newhost=${domain//./\\.}
sed -i "/$newhost/d" /etc/hosts
### enable website
rm $sitesEnable$domain".conf"
### restart Apache
service apache2 reload
### show the finished message
echo -e "Complete!
You just removed Virtual Host "$domain
exit 0;
fi