-
Notifications
You must be signed in to change notification settings - Fork 5
/
setup.sh
472 lines (416 loc) · 13.5 KB
/
setup.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
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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
#!/bin/bash
# Came from https://github.com/adnnor/magento2.3-setup
# @author: Adnan Shahzad
# @Email: [email protected]
# TODO
# 1. PHP FPM
# 2. Composer
# 3. n98-magerun2
# 4. SendMail
# 5. VirtualHost param
# 6. PHP version param
DUB="/var/www"
DIR="mage23"
ServerName="local.$DIR.com"
VHOST_FILE="${ServerName}.conf"
ServerAlias="local.$DIR.com"
DocumentRoot="$DUB/$DIR/public_html"
DocumentRoot_PUB="$DocumentRoot/pub"
FPM_HOST="127.0.0.1"
FPM_PORT="7210"
IP="127.0.0.1"
SAMPLE_CONF="conf.conf"
SAMPLE_POOL="sample.www.conf"
LOG_FILE=".log"
OUTPUT=""
WWW_POOL="/etc/php/7.2/fpm/pool.d/www.conf"
DATE_TIME=$(date '+%d%m%Y_%H%M%S');
RESTORE=$(echo -en '\033[0m')
RED=$(echo -en '\033[00;31m')
GREEN=$(echo -en '\033[00;32m')
YELLOW=$(echo -en '\033[00;33m')
LRED=$(echo -en '\033[01;31m')
ABORT=$(echo -en '\033[07;31m')
LGREEN=$(echo -en '\033[01;32m')
LYELLOW=$(echo -en '\033[01;33m')
FILE="$0"
if [ "$EUID" -ne 0 ]; then
echo "Run as root e.g. sudo bash ${FILE} -u johndoe"
exit 1
fi
if [[ -z $2 ]]; then
echo "Run command with param e.g. sudo bash ${FILE} -u johndoe"
exit 1
fi
CURRENT_USER=${2}
# Thanks to BR0kEN-/string_functions.sh for substr and strpos functions
# URL: https://gist.github.com/BR0kEN-/a84b18717f8c67ece6f7
# @param string $1
# Input string.
# @param int $2
# Cut an amount of characters from left side of string.
# @param int [$3]
# Leave an amount of characters in the truncated string.
substr()
{
local length=${3}
if [ -z "${length}" ]; then
length=$((${#1} - ${2}))
fi
local str=${1:${2}:${length}}
if [ "${#str}" -eq "${#1}" ]; then
echo "${1}"
else
echo "${str}"
fi
}
# @param string $1
# Input string.
# @param string $2
# String that will be searched in input string.
# @param int [$3]
# Offset of an input string.
strpos()
{
local str=${1}
local offset=${3}
if [ -n "${offset}" ]; then
str=`substr "${str}" ${offset}`
else
offset=0
fi
str=${str/${2}*/}
if [ "${#str}" -eq "${#1}" ]; then
return 0
fi
echo $((${#str}+${offset}))
}
# END BR0kEN- credit
log() {
local arg1=${1}
local arg2=${2}
if [[ $# -eq 2 ]] && [[ ! -z "$1" ]] && [[ ! -z "$2" ]]; then
printf "$arg1" "$arg2" | sed -r "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[mGK]//g" &>> "${LOG_FILE}"
printf "$arg1" "$arg2" 2>&1
elif [[ $# -eq 1 ]] && [[ ! -z "$1" ]]; then
printf "$arg1" | sed -r "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[mGK]//g" &>> "${LOG_FILE}"
printf "$arg1" 2>&1
else
echo "error"
fi
}
abort() {
log "${LRED}ERROR!${RESTORE}\n"
printf '======\n' &>> "${LOG_FILE}"
printf '%s\n' "$1" &>> "${LOG_FILE}"
printf '======\n' &>> "${LOG_FILE}"
printf "\n\n${ABORT} Execution Interrupted ${RESTORE} Check ${LOG_FILE} for the error details ...\n\n"
exit 1
}
apache_status() {
log "Checking apache2 status ... "
APACHE=`service --status-all | grep apache2`
APACHE_STATUS=$(strpos "${APACHE}" "+")
if [ "$APACHE_STATUS" == "" ]; then
log "${LYELLOW}Oops! It is not running${RESTORE} ... starting apache2 service "
APACHE_ACTIVE=`sudo service apache2 restart 2>&1`
if [ $? -eq 0 ]; then
log "... ${LGREEN}done${RESTORE}, apache is running now! \m/\n"
else
abort "$APACHE_STATUS"
fi
else
log "${LGREEN}it is running ... (Y)${RESTORE}\n"
fi
}
mysql_status() {
log "Checking MySQL status ... "
MYSQL=`service --status-all | grep mysql`
MYSQL_STATUS=$(strpos "${MYSQL}" "+")
if [ "$MYSQL_STATUS" == "" ]; then
log "${LYELLOW}Oops! It is not running${RESTORE} ... starting mysql service "
MYSQL_ACTIVE=`sudo service mysql restart 2>&1`
if [ $? -eq 0 ]; then
log "... ${LGREEN}done${RESTORE}, mysql is running now! \m/\n"
else
abort "$MYSQL_ACTIVE"
fi
else
log "${LGREEN}it is running ... (Y)${RESTORE}\n"
fi
}
printf "\n"
COLUMNS=$(tput cols)
space="#############################################################"
title="Welcome to Magento 2.3 Environment Setup and Installer"
printf "%*s\n" $(((${#space}+$COLUMNS)/2)) "$space"
printf "%*s\n" $(((${#title}+$COLUMNS)/2)) "$title"
printf "%*s\n" $(((${#space}+$COLUMNS)/2)) "$space"
printf "\n"
OS=$(lsb_release -rs)
DISTRO=$(lsb_release -is)
if [[ ! $OS == "18.04" ]] && [[ ! $DISTRO == "Ubuntu" ]]; then
printf "\n${ABORT} Execution Interrupted ${RESTORE} INCOMPATIBLE OS, this script is made for Ubuntu 18.04 LTS (Bionic Beaver) ...\n"
exit 1
else
log "Happy to see Ubuntu 18.04 LTS (Bionic Beaver) \m/ ... \n\n"
fi
log "Searching for apache ... "
APACHE_VERSION=$(dpkg -l | awk '$2 ~ /^apache2$/ { print $3 }')
if [ ! -z $APACHE_VERSION ]; then
if dpkg --compare-versions $APACHE_VERSION ge 2.2; then
log "${LGREEN}ok ... apache2 $APACHE_VERSION found!${RESTORE} \n"
apache_status
else
log "ok ... ${YELLOW}INCOMPATIBLE${RESTORE} apache2 $APACHE_VERSION found.\n"
log "Updating apache2 ... "
APACHE_UPDATE=`sudo apt-get --only-upgrade install -y apache2 2>&1`
if [ $? -eq 0 ]; then
log "... ${LGREEN}done!${RESTORE} Considering latest version ;) \n"
apache_status
else
abort "$APACHE_UPDATE"
fi
fi
else
log "${LYELLOW}Ohh! Failed to find apache2 :(${RESTORE}\n"
log "Installing Apache ... "
APACHE_INSTALL=`sudo apt-get install -y apache2 2>&1`
if [ $? -eq 0 ]; then
log "${LGREEN}done! \m/${RESTORE}\n"
else
abort "$APACHE_INSTALL"
fi
fi
log "Checking ownership of $DUB ..."
DIR_USER=$(stat -c '%U' "$DUB")
DIR_GROUP=$(stat -c '%G' "$DUB")
if [[ ! -z $DIR_USER ]] || [[ ! -z $DIR_GROUP ]]; then
if [[ $DIR_USER == "root" ]] || [[ $DIR_GROUP == "root" ]]; then
log "Well .. it is $DIR_USER:$DIR_GROUP - ${YELLOW}not good.${RESTORE} Recommended setting is $USER:$USER ... \n"
log "Changing ownership of $DUB ... "
DUB_OWNERSHIP=`sudo chown $CURRENT_USER:$CURRENT_USER "$DUB" 2>&1`
if [ $? -eq 0 ]; then
log "${LGREEN}done!${RESTORE}\n"
else
abort "$DUB_OWNERSHIP"
fi
else
log "${LGREEN} looks good ... ${RESTORE}it is $DIR_USER:$DIR_GROUP ... (Y) \n"
fi
fi
log "Creating required directories ... "
MAKE_DIR=`mkdir -p "$DUB/$DIR/public_html" && mkdir -p "$DUB/$DIR/backups" 2>&1`
if [ $? -eq 0 ]; then
sudo chown "$CURRENT_USER" -R "$DUB/$DIR"
OUTPUT="${OUTPUT}\nDirectory created: ${GREEN}$DocumentRoot${RESTORE}\n"
OUTPUT="${OUTPUT}Directory created: ${GREEN}$DocumentRoot/backups${RESTORE}\n"
OUTPUT="${OUTPUT}DocumentRoot: ${GREEN}$DocumentRoot/pub${RESTORE}\n"
log "${LGREEN}done!${RESTORE}\n"
else
abort "$MAKE_DIR"
fi
log "Enabling require apache2 mods ... "
REQ_MOD_ENABLE=`sudo a2enmod rewrite && sudo a2enmod alias && \
sudo a2enmod env && sudo a2enmod setenvif 2>&1`
if [ $? -eq 0 ]; then
log "${LGREEN}done!${RESTORE}\n"
else
abort "$REQ_MOD_ENABLE"
fi
log "Creating VirtualHost (${ServerName}) ... "
OUTPUT="${OUTPUT}VirtualHost: ${GREEN}http://$ServerName/${RESTORE}\n"
GENERATE_CONF=`cp "${SAMPLE_CONF}" "${VHOST_FILE}" 2>&1`
if [ ! $? -eq 0 ]; then
abort "$GENERATE_CONF"
fi
[ ! -z "${CURRENT_USER}" ] && sed -i "s/!USER!/${CURRENT_USER}/" $VHOST_FILE
[ ! -z "${DIR}" ] && sed -i "s/!DIR!/${DIR}/g" $VHOST_FILE
[ ! -z "${ServerName}" ] && sed -i "s/!ServerName!/${ServerName}/g" $VHOST_FILE
[ ! -z "${ServerAlias}" ] && sed -i "s/!ServerAlias!/${ServerAlias}/g" $VHOST_FILE
[ ! -z "${DocumentRoot}" ] && sed -i "s#!DocumentRoot!#${DocumentRoot}/pub#" $VHOST_FILE
[ ! -z "${FPM_HOST}" ] && sed -i "s#!FPM_HOST!#${FPM_HOST}#" $VHOST_FILE
[ ! -z "${FPM_PORT}" ] && sed -i "s#!FPM_PORT!#${FPM_PORT}#" $VHOST_FILE
COPY_CONF=`cp "${ServerName}.conf" /etc/apache2/sites-available/ 2>&1`
if [ ! $? -eq 0 ]; then
abort "$COPY_CONF"
fi
ACTIVATE_SITE=`sudo a2ensite "${ServerName}.conf" 2>&1`
if [ ! $? -eq 0 ]; then
abort "$ACTIVATE_SITE"
fi
UPDATE_HOSTS=`sudo echo "$IP ${ServerName}" >> /etc/hosts 2>&1`
if [ ! $? -eq 0 ]; then
abort "$UPDATE_HOSTS"
fi
RESTART_APACHE=`sudo service apache2 restart 2>&1`
if [ ! $? -eq 0 ]; then
abort "$RESTART_APACHE"
fi
if [ $? -eq 0 ]; then
log "${LGREEN}done!${RESTORE}\n"
fi
log "Taking backup of /etc/apache2/apache2.conf ... "
CREATE_BACKUP_DIR=`mkdir -p ./backups 2>&1`
if [ ! $? -eq 0 ]; then
abort "$CREATE_BACKUP_DIR"
fi
CONF_BK=`cp /etc/apache2/apache2.conf ./backups/apache2.conf.original."${DATE_TIME}" 2>&1`
if [ $? -eq 0 ]; then
log "${LGREEN}done!${RESTORE}\n"
else
abort "$CONF_BK"
fi
log "Searching for MariaDB or MySQL ... "
MARIA_SEARCH=$(dpkg -l | awk '$2 ~ /^mariadb$/ { print $3 }')
MYSQL_SEARCH=$(dpkg -l | awk '$2 ~ /^mysql-server-5.7$/ { print $3 }')
if [ ! -z $MARIA_SEARCH ]; then
if dpkg --compare-versions $MARIA_SEARCH ge 10.2; then
log "${LGREEN}ok ... MariaDB $MARIA_SEARCH found!${RESTORE} \n"
mysql_status
else
log "ok ... ${YELLOW}INCOMPATIBLE${RESTORE} MariaDB $MARIA_SEARCH found.\n"
log "Updating MariaDB ... "
MYSQL_UPDATE=`sudo apt-get --only-upgrade install -y mariadb 2>&1`
if [ $? -eq 0 ]; then
log "... ${LGREEN}done!${RESTORE} Considering latest version ;) \n"
mysql_status
else
abort "$MARIA_SEARCH"
fi
fi
else
log "${LYELLOW}Ohh! Failed to find MariaDB,${RESTORE} it's ok ... \nLet's search for MySQL now ... "
if [ ! -z $MYSQL_SEARCH ]; then
if dpkg --compare-versions $MYSQL_SEARCH ge 5.6; then
log "${LGREEN}ok ... MySQL $MYSQL_SEARCH found!${RESTORE} \n"
mysql_status
else
log "ok ... ${YELLOW}INCOMPATIBLE${RESTORE} MySQL $MYSQL_SEARCH found.\n"
log "Updating MySQL ... "
MYSQL_UPDATE=`sudo apt-get --only-upgrade -y install mysql-server mysql-client 2>&1`
if [ $? -eq 0 ]; then
log "... ${LGREEN}done!${RESTORE} Considering latest version ;) \n"
mysql_status
else
abort "$MYSQL_UPDATE"
fi
fi
else
log "Ohh! Failed to find MySQL also :(\n"
log "Preferring MariaDB 10.3 ... installing, please be patient ... "
MARIADB_SOFT_PROP=`sudo apt-get install -y software-properties-common 2>&1`
if [ ! $? -eq 0 ]; then
abort "$MARIADB_SOFT_PROP"
fi
MARIADB_KEYSERVER=`sudo apt-key adv --recv-keys --keyserver hkp://keyserver.ubuntu.com:80 0xF1656F24C74CD1D8 2>&1`
if [ ! $? -eq 0 ]; then
abort "$MARIADB_KEYSERVER"
fi
MARIADB_APT_REPO=`sudo add-apt-repository 'deb [arch=amd64] http://mirror.zol.co.zw/mariadb/repo/10.3/ubuntu bionic main' 2>&1`
if [ ! $? -eq 0 ]; then
abort "$MARIADB_APT_REPO"
fi
MARIADB_APT_UPDT=`sudo apt-get update 2>&1`
if [ ! $? -eq 0 ]; then
abort "$MARIADB_APT_UPDT"
fi
export DEBIAN_FRONTEND="noninteractive"
sudo debconf-set-selections <<< "maria-db-10.3 mysql-server/root_password password 123abc"
sudo debconf-set-selections <<< "maria-db-10.3 mysql-server/root_password_again password 123abc"
MYSQL_INSTALL=`sudo apt-get install -y mariadb-server 2>&1`
if [ $? -eq 0 ]; then
log "${LGREEN}done! \m/${RESTORE}\n"
OUTPUT="${OUTPUT}MariaDB 10.3 Root Password: ${GREEN}123abc${RESTORE}\n"
else
abort "$MYSQL_INSTALL"
fi
fi
fi
log "Searching for installing PHP 7.2 and all of its required extensions ...\n"
install_php() {
local ext="${1}"
log ">> $ext ... "
dpkg -s $ext &> /dev/null
if [ $? -eq 0 ]; then
log "${LGREEN}found!${RESTORE}\n"
else
log "${LYELLOW}not found${RESTORE} ... installing ... "
EXT_INSTALL=`sudo apt-get install -y "${ext}" 2>&1`
if [ $? -eq 0 ]; then
log "${LGREEN}done!${RESTORE}\n"
else
abort "$EXT_INSTALL"
fi
fi
}
extensions=(libapache2-mod-php7.2 php7.2 php7.2-bcmath php7.2-common php7.2-curl php7.2-gd php7.2-intl \
php7.2-mbstring php7.2-mysql php7.2-soap php7.2-xml php7.2-xsl php7.2-zip php7.2-json)
for i in "${extensions[@]}"; do
install_php "$i"
done
log "Installing PHP-FPM and required mods (if any) ... "
REQ_MOD_CONFIG=`sudo apt-get install libapache2-mod-fcgid && \
sudo apt-get install php7.2-fpm && \
sudo a2dismod php7.2 && sudo a2enmod proxy && sudo a2enmod proxy_fcgi && \
sudo a2enmod proxy_http && sudo a2enmod fcgid 2>&1`
if [ $? -eq 0 ]; then
log "${LGREEN}done!${RESTORE}\n"
else
abort "$REQ_MOD_DISABLE"
fi
log "Taking backup of $WWW_POOL ... "
CONF_BK=`cp "${WWW_POOL}" ./backups/www.conf.original."${DATE_TIME}" 2>&1`
if [ $? -eq 0 ]; then
log "${LGREEN}done!${RESTORE}\n"
OUTPUT="${OUTPUT}Config file backups (file.conf.original.[DATE_TIME]): ${GREEN}./backups${RESTORE}\n"
else
abort "$CONF_BK"
fi
log "Configuring PHP-FPM ... "
CREATE_DIR_PHP=`sudo mkdir -p /var/log/php7.2/ 2>&1`
if [ ! $? -eq 0 ]; then
abort "$CREATE_DIR_PHP"
fi
OUTPUT="${OUTPUT}PHP logs directory: ${GREEN}/var/log/php7.2${RESTORE}\n"
CHOWN_DIR_PHP=`sudo chown -R www-data:www-data /var/log/php7.2/ 2>&1`
if [ ! $? -eq 0 ]; then
abort "$CHOWN_DIR_PHP"
fi
GENERATE_CONF=`cp "${SAMPLE_POOL}" "www.conf" 2>&1`
if [ ! $? -eq 0 ]; then
abort "$GENERATE_CONF"
fi
[ ! -z "${CURRENT_USER}" ] && sed -i "s/!USER!/${CURRENT_USER}/" www.conf
[ ! -z "${CURRENT_USER}" ] && sed -i "s/!GROUP!/${CURRENT_USER}/" www.conf
[ ! -z "${FPM_HOST}" ] && sed -i "s#!FPM_HOST!#${FPM_HOST}#" www.conf
[ ! -z "${FPM_PORT}" ] && sed -i "s#!FPM_PORT!#${FPM_PORT}#" www.conf
COPY_WWW_POOL=`mv "www.conf" "${WWW_POOL}" 2>&1`
if [ ! $? -eq 0 ]; then
abort "$COPY_WWW_POOL"
fi
OUTPUT="${OUTPUT}PHP-FPM pool file: ${GREEN}$WWW_POOL${RESTORE}\n"
CHOWN_DIR_PHP=`sudo chown -R www-data:www-data /var/log/php7.2/ 2>&1`
if [ ! $? -eq 0 ]; then
abort "$CHOWN_DIR_PHP"
fi
RESTART_FPM=`service php7.2-fpm restart 2>&1`
if [ $? -eq 0 ]; then
log "${LGREEN}done!${RESTORE}\n"
else
abort "$RESTART_FPM"
fi
log "Activating PHP-FPM for ${ServerName} ... "
FPM_FOR_VH=`sed -i "s;##;;" $VHOST_FILE 2>&1`
COPY_CONF=`mv "${ServerName}.conf" /etc/apache2/sites-available/ 2>&1`
if [ ! $? -eq 0 ]; then
abort "$COPY_CONF"
fi
RESTART_APACHE=`service apache2 restart 2>&1`
if [ $? -eq 0 ]; then
log "${LGREEN}done!${RESTORE}\n"
else
abort "$RESTART_APACHE"
fi
log "${OUTPUT}"
printf "\n"