forked from papac/mkvirtualhost
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvirtual
executable file
·249 lines (224 loc) · 5.18 KB
/
virtual
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
#!/bin/bash
APACHE_SITE_DIR="/etc/apache2/sites-available";
# color a output message
c_echo() {
if [[ $2 == "red" ]]; then
echo -e "\033[0;31m$1\033[00m"
elif [[ $2 == "green" ]]; then
echo -e "\033[0;32m$1\033[00m";
else
echo -e "\033[0;35m$1\033[00m"
fi
}
# verify if a program exist, else tell you if you want install it
prog_exist() {
if ! test `which $1`; then
c_echo "✗ $1 not found" "red"
echo -n "You want install it? Y/y/yes/*: "
read -r response
case $response in
"Y"|"y"|"yes"|"")
apt-get install $1 -y > /dev/null
exit 0
;;
*)
exit 1
;;
esac
exit 0
fi
}
# `usage` function
usage() {
DEF=" ─ USAGE:
sudo $0 add <docroot> <servername> [--with-git]
sudo $0 remove <servername>
sudo $0 list
sudo $0 has <servername>"
FR="
─ \033[0;4mCommande\033[00m
add............Ajout un projet
remove.........Supprimé un projet
has............Informe si le domaine exist
list...........List tout le domaine
─ \033[0;4mvaleur\033[00m
<docroot>......Répertoire ou le serveur va trouvé les fichiers à servir.
<servername>...Le nom de domaine sur projet
─ \033[0;4moption\033[00m:
--with-git.....Initialise un dépot git.
--with-local-log.....Ajout les logs générés par appache dans votre documentroot."
EN="
─ \033[0;4mCommand\033[00m
add............Add project
remove.........Remove project
has............Tell you if domaine exist
list...........List all domaine
─ \033[0;4mvalue\033[00m
<docroot>......Documentroot.
<servername>...You project domain name.
─ \033[0;4moption\033[00m:
--with-git.....If has enabled, git repository will be initialize.
--with-local-log.....Add apache logs in you documentroot."
EG="
─ Exemples:
☑ add
$ sudo $0 add /path/to/my/project/folder project.dev
☑ remove
$ sudo $0 remove project.dev
☑ has
$ sudo $0 has project.dev
☑ list
$ sudo $0 list
\033[0;4mFork me\033[00m to https://github.com/papac/mkvirtualhost.git
"
echo -e "$DEF"
case $1 in
"help")
if [[ $2 == "--fr" ]]; then
echo -e "$FR"
else
echo -e "$EN"
fi
;;
*)
echo -e "$EN"
;;
esac
echo -e "$EG"
}
# add configuration function
add_vhost() {
SERVERNAME=""
HTTP=80
C=0
LOGLEV="warn"
ERRORLOGDIR="$APACHE_LOG_DIR/error.log";
ACCESSLOGDIR="$APACHE_LOG_DIR/error.log";
if [ ! -d "$1" ]; then
c_echo "✗ $1 are not directory" "red"
echo -n "You want create it Y/y/yes/*: "
read -r response
case $response in
"Y"|"y"|"yes"|"")
mkdir -p $1
;;
*)
exit 1
;;
esac
fi
if [ -f $APACHE_SITE_DIR/$2".conf" ]; then
c_echo "✗ this domaine already exist." "red"
exit 1
else
DOCROOT=$1
SERVERNAME=$2
C=1
fi
for option in $@; do
if [ $option == "--with-local-log" ]; then
if [ ! -d "$DOCROOT/.syslog" ]; then
mkdir "$DOCROOT/.syslog"
fi
ERRORLOGDIR="$DOCROOT/.syslog/error.log";
ACCESSLOGDIR="$DOCROOT/.syslog/access.log";
fi
done
if [[ $C == 1 ]]; then
echo "<VirtualHost *:$HTTP>
ServerName $SERVERNAME
serverAlias www.$SERVERNAME
DocumentRoot $DOCROOT
<Directory $DOCROOT/>
Options Indexes Multiviews FollowSymLinks
AllowOverride All
require all granted
</Directory>
ErrorLog $ERRORLOGDIR
CustomLog $ACCESSLOGDIR combined
LogLevel $LOGLEV
</VirtualHost>" > $APACHE_SITE_DIR/$SERVERNAME".conf"
cd $APACHE_SITE_DIR
a2ensite $SERVERNAME".conf" > /dev/null 2>&1
if `service apache2 reload > /dev/null`; then
chmod 755 -R $DOCROOT
if [ "$3" == "--with-git" -o "$4" == "--with-git" ]; then
if prog_exist "git"; then
cd $DOCROOT
git init > /dev/null
c_echo "✓ git repository has initilazed."
fi
fi
echo "127.0.0.1 $SERVERNAME www.$SERVERNAME" >> /etc/hosts
c_echo "✓ project domain has created." "green"
exit 0
else
c_echo "✗ an error is comming" "red"
a2dissite $SERVERNAME".conf" > /dev/null
rm $APACHE_SITE_DIR/$SERVERNAME".conf"
exit 1
fi
else
c_echo "✗ an error is comming." "red"
exit 1
fi
}
# delete configuration function
remove_vhost() {
if [ -f "$APACHE_SITE_DIR/$1.conf" ]; then
cd $APACHE_SITE_DIR
a2dissite "${1}.conf" > /dev/null
service apache2 reload > /dev/null
rm "${1}.conf"
sed -re "/^\n?127\.0\.0\.1\s+${1}\swww.${1}\n?$/ d" < /etc/hosts > /tmp/hosts
if [ -s /tmp/hosts ]; then
cp /tmp/hosts /etc/hosts
fi
c_echo "✓ $1 has removed" "green"
exit 0
else
c_echo "✗ this domaine not exist." "red"
exit 1
fi
}
# busness logic
PARAM=$1
if ! test `id -u` -eq 0; then
c_echo "✗ permission denied" "red";
exit 1
fi
prog_exist "apache2"
if [[ $PARAM == "add" ]]; then
case $# in
3) add_vhost $2 $3 ;;
4) add_vhost $2 $3 $4 ;;
5) add_vhost $2 $3 $4 $5 ;;
*) usage
exit 1 ;;
esac
elif [[ $PARAM == "remove" ]]; then
remove_vhost $2
exit 0
elif [[ $PARAM == "list" ]]; then
i=0
for file in `ls /etc/apache2/sites-enabled`; do
i=$(expr $i + 1)
len=$(expr length $file)
end=$(expr $len - 5)
file=$(expr substr $file 1 $end)
c_echo "☑ $file" "green"
done
exit 0
elif [[ $PARAM == "has" ]]; then
if [ -f "$APACHE_SITE_DIR/$2.conf" ]; then
c_echo "✗ domaine $2 exist." "green"
else
c_echo "✗ domaine $2 not found" "red"
fi
elif [[ $PARAM == "help" ]]; then
usage $PARAM $2
exit 0
else
usage
exit 1
fi