This repository was archived by the owner on Jul 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup-server
More file actions
executable file
·424 lines (346 loc) · 11.5 KB
/
setup-server
File metadata and controls
executable file
·424 lines (346 loc) · 11.5 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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
#!/usr/bin/env bash
# setup-server - Configura servidor web automaticamente
# Obs.: Usa arquivos do diretório apache como base
#
# Site : http://programadorboard.epizy.com/autor
# Autor : Oliver Silva <programadorboard@gmail.com>
# Munutençâo: Oliver Silva <programadorboard@gmail.com>
#
# ----------------------------------------------------
# Este programa configura um servidor web de forma automática e retorna o resultado na saída padrão (STDOUT).
#
# Exemplo:
#
# $ ./setup-server --setup
# [*] Instalando requisitos...
#
# Histórico:
#
# v0.1 2022-11-28, Oliver Silva:
#
# - Versão inicial configuração do servidor web
# - Adicionado instalação do gerenciador de serviços
# Apache e MariaDB
# v0.2 2023-01-30, Oliver Silva:
# - Adicionada suporte de acesso a pasta /sdcard/htdocs
# v0.3 2023-02-02, Oliver Silva:
# - Adicionada suporte de backup
# v0.4 2023-03-27, Oliver Silva:
# - Adicionado suporte para habilitar, desabilitar e atualizar o certificado
# v0.5 2023-04-04, Oliver Silva:
# - Adicionado suporte a opçôes --setup, --disable-ssl, --enable-ssl e update-ssl
#
# Licença: MIT License
#
# Versão 0.1: Instalação, configuração do servidor web e gerenciador server-web
# Versão 0.2: Configurado a hospedagem de projetos na pasta /sdcard/htdocs
# Versão 0.3: Realizar backup de projetos antigos antes
# Versão 0.4: Adicionado suporte para habilitar, desabilitar e atualizar o certificado
# Versão 0.5: Adicionado suporte para opções --setup, --disable-ssl, --enable-ssl e --update-ssl
#
# Mensagens explicativas no processo de instalação completa
text_proc=(
"Installing package"
"Configuring apache"
"Configuring MySQL root user"
"Configuring server-web manager"
)
reqList=(
"mysql"
"mariadb"
"httpd"
"apachectl"
"openssl"
"php"
"curl"
)
# banner
banner() {
echo -e "\n\e[34;1m
█▀▀ █▀▀ ▀▀█▀▀ █░░█ █▀▀█ █▀▀ █▀▀ █▀▀█ ▀█░█▀ █▀▀ █▀▀█
▀▀█ █▀▀ ░░█░░ █░░█ █░░█ ▀▀█ █▀▀ █▄▄▀ ░█▄█░ █▀▀ █▄▄▀
▀▀▀ ▀▀▀ ░░▀░░ ░▀▀▀ █▀▀▀ ▀▀▀ ▀▀▀ ▀░▀▀ ░░▀░░ ▀▀▀ ▀░▀▀
\e[0m\n\e[34;2;3m$(version | tr A-Z a-z)\e[0m\n"
}
# Version
version() {
echo -n "$(basename "$0")"
grep "^# Versão" "$0" | tail -1 | cut -d : -f 1 | tr -d \# | tr A-Z a-z
}
# Animation
animation() {
local delay=0.25
local pid=$!
spinner=('\' '/' '-' '\' '/' '-')
while [ $(ps a | awk '{print $1}' | grep $pid) ] ; do
for i in ${spinner[@]}; do
tput civis
printf "\r\e[32m[*] $1...${i}"
sleep $delay
printf "\b\b\b\b\b\b\b\b";
done
done
printf "\b\b\b\b\b\b"
printf "\r\e[32m[+] $1...\e[32;1mOk\e[0m\n"
}
# Install requirements
install_req() {
apt update > /dev/null 2>&1
apt install curl apache2 openssl openssl-tool php php-apache phpmyadmin mariadb -y > /dev/null 2>&1
}
# Setup apache
configure_apache() {
if [ -d apache -a -d apache/extra -a -f apache/extra/httpd.conf ] ; then
cp apache/extra/httpd.conf $PREFIX/etc/apache2
fi
if [ -d apache -a -d apache/ssl -a -f apache/ssl/cert.crt -a -f apache/ssl/cert.key ] ; then
if [ ! -d $PREFIX/etc/apache2/ssl ] ; then
mkdir $PREFIX/etc/apache2/ssl
chmod 700 $PREFIX/etc/apache2/ssl
cp apache/ssl/cert.* $PREFIX/etc/apache2/ssl/
else
rm $PREFIX/etc/apache2/ssl -rf
mkdir $PREFIX/etc/apache2/ssl
chmod 700 $PREFIX/etc/apache2/ssl
cp apache/ssl/cert.* $PREFIX/etc/apache2/ssl/
fi
fi
if [ -d apache -a -d apache/extra -a -f apache/extra/httpd-ssl.conf -a apache/extra/httpd-vhosts.conf ] ; then
cp apache/extra/httpd-ssl.conf $PREFIX/etc/apache2/extra
cp apache/extra/httpd-vhosts.conf $PREFIX/etc/apache2/extra
fi
if [ -d apache -a -d apache/phpmyadmin -a -f apache/phpmyadmin/config.inc.php ] ; then
cp apache/phpmyadmin/config.inc.php $PREFIX/etc/phpmyadmin/config.inc.php
fi
touch $PREFIX/etc/apache2/extra/php_module.conf
echo "<?php phpinfo(); ?>" > /sdcard/htdocs/index.php
sleep 1
}
# Setup user of MySQL
configure_mysql() {
pkill mariadb
maridbd-safe -u root 1> /dev/null &
sleep 2
mariadb -u root -D mysql -e "set password for 'root'@'localhost' = password('toor');"
mariadb -u root -D mysql -e "flush privileges;"
pkill mariadb
}
# Setup manager server-web
configure_manager() {
curl -LO https://raw.githubusercontent.com/Olliv3r/Server-web/main/server-web > /dev/null 2>&1
sleep 1
mv server-web $PREFIX/bin
chmod +x $PREFIX/bin/server-web
}
# Check all requirements
checkReq() {
printf "\r\e[0m\e[32m[*] Checking $1...\e[0m"
sleep 1
if [ -f $PREFIX/bin/$1 ] ; then
printf "\r\e[0m\e[32m[+] Checking $1...\e[32;1mOK\e[0m\n"
else
printf "\r\e[0m\e[33;1m[!] Checking $1...\e[33;1mNO\e[0m\n"
fi
}
checkReqAll() {
for package in ${reqList[*]} ; do
checkReq $package
done
}
check_apache_files() {
local dir_e=$PREFIX/etc/apache2/extra
local dir_s=$PREFIX/etc/apache2/ssl
local date=", Date: $(date +%d/%m/%y) - $(date +%H:%M) e $(date +%S) segundos"
command="grep -Eo"
regex="By:.*${date:57}"
httpd="$($command $regex ${PREFIX}/etc/apache2/httpd.conf)"
httpd_ssl="$($command $regex ${dir_e}/httpd-ssl.conf)"
httpd_vhosts="$($command $regex ${dir_e}/httpd-vhosts.conf)"
printf "\r\e[0m\e[32m[*] Checking files apache2...\e[0m"
sleep 1
if [ -z "$httpd" -a -z "$httpd_ssl" -a -z "$httpd_vhosts" -a ! -f $dir_s/cert.crt -a ! -f $dir_s/cert.key ] ; then
printf "\r\e[0m\e[33m[!] Checking files apache2...\e[33;1mNo\e[0m\n"
sleep 0.5
elif [ -n "$httpd" -a -n "$httpd_ssl" -a -n "$httpd_vhosts" -a -f $dir_s/cert.crt -a -f $dir_s/cert.key ] ; then
sleep 0.5
printf "\r\e[0m\e[32m[+] Checking files apache2...\e[32;1mOk\e[0m\n"
fi
}
check_manager() {
local dir=$PREFIX/bin
printf "\r\e[0m\e[32m[*] Checking manager server-web...\e[0m"
sleep 1
if [ ! -f $dir/server-web ] ; then
printf "\r\e[0m\e[33m[!] Checking manager server-web...\e[33;1mNo\e[0m\n"
sleep 0.5
elif [ -f $dir/server-web ] ; then
printf "\r\e[0m\e[32m[+] Checking manager server-web...\e[32;1mOk\e[0m\n"
sleep 0.5
fi
}
# Checar backup
check_htdocs_backup() {
if [ -d /sdcard/backup_htdocs ] ; then
echo -e "\e[0m\e[32m[+] Backup disponível em \e[32;1m/sdcard/backup_htdocs\e[0m"
sleep 0.1
fi
}
# Mensagem do autor
message_author() {
echo -e "\e[0m\e[32m[+] Hospede todos os seus projetos em \e[32;1m/sdcard/htdocs\e[0m"
sleep 0.1
check_htdocs_backup
echo -e "\e[0m\e[32m[+] Bug, relate no telegram \e[33;1;4mhttps://t.me/tiooliver_sh\e[0m"
sleep 0.1
}
# Mostra informaçôes úteis no final da instalação
final_info() {
clear
banner
checkReqAll
check_manager
check_apache_files
message_author
tput cnorm
}
# Permite o acesso a memoria interna
permission_storage_access() {
echo -e "\e[33;1mPermita o acesso a memoria interna!\e[0m" | tr a-z A-Z
sleep 2
termux-setup-storage
sleep 2
}
# Configura htaccess
htaccess() {
if [ -f apache/extra/.htaccess ] ; then
cp apache/extra/.htaccess /sdcard/htdocs
else
echo -e "RewriteEngine On\nRewriteCond %{SCRIPT_FILENAME} !-f\nRewriteCond %{SCRIPT_FILENAME} !-d\nRewriteRule ^(.*)$ index.php?url=\$1" > /sdcard/htdocs/.htaccess
fi
}
# Renomeia e excluir htdocs antigo
copy_backup_htdocs() {
cp -rf /sdcard/htdocs /sdcard/backup_htdocs/"htdocs-$(date +%d:%m:%Y-%H:%M:%S)"
rm -rf /sdcard/htdocs
}
# Cria htdocs e configura htaccess
create_htdocs_htaccess() {
mkdir -p /sdcard/htdocs
htaccess
}
# Checa diretório htdocs
check_htdocs() {
if [ -d /sdcard/htdocs ] ; then
echo -e "\e[33;1mPasta htdocs encontrada, deseja fazer backup y/n? : " ; read res
if [ -z "$res" ] ; then
echo -e "\e[33;1mErro, tente \e[0my\e[33;1m \e[0m,n\e[33;1m ou \e[0mctr+c\e[33;1m para encerrar\e[0m\n"
check_htdocs
fi
if [ -n "$res" -a "$res" == "y" -o "$res" == "Y" ] ; then
if [ ! -d /sdcard/backup_htdocs ] ; then
mkdir -p /sdcard/backup_htdocs
fi
copy_backup_htdocs
create_htdocs_htaccess
elif [ -n "$res" -a "$res" == "n" -o "$res" == "N" ] ; then
rm -rf /sdcard/htdocs
create_htdocs_htaccess
else
echo -e "\n\e[33;1mErro, tente \e[0my\e[33;1m \e[0m,n\e[33;1m ou \e[0mctr+c\e[33;1m para encerrar\e[0m\n"
check_htdocs
fi
fi
if [ ! -d /sdcard/htdocs ] ; then
create_htdocs_htaccess
fi
}
# Checa se tem permissão a memoria interna
check_storage_access() {
if [ ! -d $HOME/storage ] ; then
permission_storage_access
fi
if [ -d $HOME/storage ] ; then
check_htdocs
fi
}
# Função principal
main() {
echo -e "\e[0m\e[33;1mDer ENTER para continuar" ; read
check_storage_access
apt update && apt upgrade -y && apt install ncurses-utils -y
clear
banner
install_req &> /dev/null & animation "${text_proc[0]}"
sleep 1
configure_apache &> /dev/null & animation "${text_proc[1]}"
sleep 1
configure_mysql &> /dev/null & animation "${text_proc[2]}"
sleep 1
configure_manager &> /dev/null & animation "${text_proc[3]}"
sleep 1
final_info
}
helper() {
echo -e "Uso: $(basename "$0") OPÇÃO\n\t-h, --help\tMostra esta tela de ajuda\n\t-v, --version\tMostra a versão atual do programa\n\t--setup\t\tConfigura o servidor web de forma automática\n\t--disable-ssl\tDesabilita o certificado ssl\n\t--enable-ssl\tHabilita o certificado ssl\n\t--update-ssl\tAtualiza o certificado\n"
}
disableSsl() {
if [ -f apache/extra/httpd-vhosts.conf ] ; then
cp apache/extra/httpd-vhosts.conf .
sed -i 's/Redirect/#Redirect/g' httpd-vhosts.conf
if [ -f httpd-vhosts.conf ] ; then
cp httpd-vhosts.conf $PREFIX/etc/apache2/extra/httpd-vhosts.conf
rm httpd-vhosts.conf
echo -e "\e[0m\e[32m[+] Certificado desabilitado com sucesso\e[0m"
else
echo -e "\e[0m\e[31m[!] Erro ao encontrar o arquivo vhosts.conf\e[0m"
fi
else
echo -e "\e[0m\e[31m[!] Erro ao encontrar o arquivo vhosts.conf\e[0m"
fi
}
enableSsl() {
if [ -f apache/extra/httpd-vhosts.conf ] ; then
cp apache/extra/httpd-vhosts.conf $PREFIX/etc/apache2/extra
echo -e "\e[0m\e[32m[+] Certificado habilitado com sucesso\e[0m"
else
echo -e "\e[0m\e[31;1m[!] Erro ao encontrar o arquivo vhosts.conf\e[0m"
fi
}
updateSsl() {
dir_local=apache/ssl
dir_default=$PREFIX/etc/apache2/ssl
echo -e "\e[0m\e[33;1mDer ENTER para continuar\e[0m" ; read
openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout $dir_local/cert.key -out $dir_local/cert.crt
if [ -f $dir_local/cert.crt -a -f $dir_local/cert.key ] ; then
cp $dir_local/cert.crt $dir_default
cp $dir_local/cert.key $dir_default
if [ -f $PREFIX/etc/apache2/ssl/cert.crt -a -f $PREFIX/etc/apache2/ssl/cert.key ] ; then
echo -e "\e[0m\e[32m[+] Certificado atualizado\e[0m"
else
echo -e "\e[0m\e[31m[!] Erro ao atualizar o certificado\e[0m"
fi
else
echo -e "\e[0m\e[31m[!] Erro ao encontrar arquivos SSL\e[0m"
fi
}
test -z "$1" && banner && echo -e "\e[0m\e[33mErro, tente -h ou --help para mais informaçôes\e[0m"
while [ -n "$1" ] ; do
case "$1" in
-h | --help)
helper;;
-v | --version)
version;;
--setup)
main;;
--disable-ssl)
disableSsl;;
--enable-ssl)
enableSsl;;
--update-ssl)
updateSsl;;
*)
echo -e "\e[0m\e[31mErro, opção inválida: $1\e[0m"
exit 0
esac
shift
done