Repository navigation
Expand file tree
/
Copy pathvirtual
More file actions
executable file
·305 lines (262 loc) · 6.27 KB
/
Copy pathvirtual
File metadata and controls
executable file
·305 lines (262 loc) · 6.27 KB
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
#!/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
}
# Verif the domain definition
verify_domain() {
if ! expr $1 : '^[a-z0-9_-]\+\.[a-z]\{2,\}$' >> /dev/null; then
c_echo "✗ domain name is not valid" "red"
c_echo "eg: domain.dev"
exit 1
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 "Do you want install it? Y/n: "
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 command add <docroot> <servername> [--with-git] [--with-local-log]
sudo command remove <servername>
sudo command list
sudo command has <servername>
sudo command docroot-of <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
docroot-of - Affiche le docroot
─ \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
docroot-of - Display the document root
─ \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
\033[0;31m$\033[00m] sudo command add /path/to/my/project/folder project.dev
☑ remove
\033[0;31m$\033[00m] sudo command remove project.dev
☑ has
\033[0;31m$\033[00m] sudo command has project.dev
☑ list
\033[0;31m$\033[00m] sudo command list
☑ docroot-of
\033[0;31m$\033[00m] sudo command docroot-of project.dev
\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() {
local SERVERNAME=""
local HTTP=80
local C=0
local LOGLEV="warn"
local ERRORLOGDIR="$APACHE_LOG_DIR/error.log";
local ACCESSLOGDIR="$APACHE_LOG_DIR/access.log";
if [ ! -d "$1" ]; then
c_echo "✗ $1 are not directory" "red"
echo -n "Do you want create it? Y/n: "
read -r response
case $response in
"Y"|"y"|"yes"|"")
mkdir -p $1
chown 1000:1000 -R $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
if [[ $C == 1 ]]; then
for option in $@; do
if [ $option == "--with-local-log" ]; then
if [ ! -d "$DOCROOT/.syslog" ]; then
mkdir -m 777 "$DOCROOT/.syslog"
fi
ERRORLOGDIR="$DOCROOT/.syslog/error.log";
fi
done
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 774 -R $DOCROOT
# chown 1000:1000 -R $DOCROOT
if [ "$3" == "--with-git" -o "$4" == "--with-git" ]; then
if prog_exist "git"; then
cd $DOCROOT
if [ ! -d ".git" ]; then
git init > /dev/null
c_echo "✓ Initialized empty git repository."
else
c_echo "✗ git repository is alrealy initilazed." "red"
fi
fi
fi
echo -e "127.0.0.1 $SERVERNAME\n127.0.0.1 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 2>&1
service apache2 reload > /dev/null 2>&1
rm "${1}.conf" 2> /dev/null
sed -re "/^\n?127\.0\.0\.1\s+(www\.)?${1}\n?$/ d" < /etc/hosts > /tmp/hosts
if [ -s /tmp/hosts ]; then
cp /tmp/hosts /etc/hosts
fi
c_echo "✓ $1 removed" "green"
exit 0
else
c_echo "✗ $1 not exists" "red"
exit 1
fi
}
display_docroot() {
local domain=$1
r=`sed -n '/DocumentRoot/p' $APACHE_SITE_DIR/$domain.conf | sed -r 's/^\t*DocumentRoot\s*//'`
c_echo $r 'green'
}
domain_exists() {
if [ -f "$APACHE_SITE_DIR/$1.conf" ]; then
if [ "$2" == 'display' ]; then
c_echo "✗ $1 exists" "green"
fi
else
c_echo "✗ $1 not found" "red"
exit 1
fi
}
# busness logic
PARAM=$1
if ! test `id -u` -eq 0; then
c_echo "✗ permission denied - use administrator privilege" "red";
exit 1
fi
prog_exist "apache2"
if [[ $PARAM == "add" ]]; then
verify_domain $3
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
verify_domain $2
remove_vhost $2
exit 0
elif [[ $PARAM == "docroot-of" ]]; then
verify_domain $2
domain_exists $2
display_docroot $2
exit 0
elif [[ $PARAM == "list" ]]; then
i=0
for file in `ls $APACHE_SITE_DIR`; do
i=$(expr $i + 1)
len=$(expr length $file)
end=$(expr $len - 5)
file=$(expr substr $file 1 $end)
c_echo "-> $file" "green"
done
c_echo "${i} have available"
exit 0
elif [[ $PARAM == "has" ]]; then
verify_domain $2
domain_exists $2 'display'
exit 0
elif [[ $PARAM == "help" ]]; then
usage $PARAM $2
exit 0
else
usage
exit 1
fi