-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscript.sh
296 lines (236 loc) · 6.77 KB
/
script.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
#!/bin/bash
set -eu
trap end_install EXIT
#
##
update_system() {
#
apt update
apt -yqq upgrade
}
#
##
install_base_system() {
#
apt -yqq install --no-install-recommends apt-utils 2>&1
apt -yqq install --no-install-recommends apt-transport-https 2>&1
#
apt -yqq install ca-certificates build-essential net-tools curl wget lsb-release procps 2>&1
apt -yqq install perl unzip git nano htop iftop mariadb-client 2>&1
}
#
##
define_passwords() {
# check if existing passwords file exists
if [ ! -f /root/passwords.txt ]; then
# root database user
rootdbpass=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 32 | head -n 1)
# database user
dbname="app"
dbuser="app"
dbpass=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 32 | head -n 1)
echo "#
# Deploy Script - Credentials file
#
# Database
#
# Root user password
rootdbpass=\"$rootdbpass\"
#
# Database Name
dbname=\"$dbname\"
#
# Database User
dbuser=\"$dbuser\"
#
# Database Password
dbpass=\"$dbpass\"
" > /root/passwords.txt
else
# load existing file
set -o allexport
source /root/passwords.txt
set +o allexport
fi
}
#
##
install_apache() {
# apache2 and Utils
apt -yqq install apache2 apache2-utils
# enable apache modules
a2enmod headers
a2enmod rewrite
#
awk '/<Directory \/var\/www\/>/,/AllowOverride None/{sub("None", "All",$0)}{print}' /etc/apache2/apache2.conf > tmp.conf && mv tmp.conf /etc/apache2/apache2.conf
#
service apache2 restart
#
rm -f /var/www/html/index.html
}
#
##
install_adminer() {
#
wget http://www.adminer.org/latest.php -O /var/www/html/adminer.php
chown www-data:www-data /var/www/html/adminer.php
}
#
##
install_php() {
# is PHP5
if [ "$VERSION_ID" = "12.04" ] || [ "$VERSION_ID" = "14.04" ] || [ "$VERSION_ID" = "15.04" ]; then
PHP_VERSION="5"
fi
# is PHP7
if [ "$VERSION_ID" = "16.04" ] || [ "$VERSION_ID" = "16.10" ] || [ "$VERSION_ID" = "17.04" ] || [ "$VERSION_ID" = "17.10" ]; then
PHP_VERSION="7.0"
fi
# is PHP7.2
if [ "$VERSION_ID" = "18.04" ] || [ "$VERSION_ID" = "18.10" ]; then
PHP_VERSION="7.2"
fi
# is PHP7.4
if [ "$VERSION_ID" = "20.04" ] || [ "$VERSION_ID" = "20.10" ]; then
PHP_VERSION="7.4"
fi
#
# install PHP5
if [ "$PHP_VERSION" = "5" ]; then
#
echo "Installing PHP$PHP_VERSION"
apt -yqq install php$PHP_VERSION php$PHP_VERSION-cli
apt -yqq install php$PHP_VERSION-{curl,gd,mcrypt,json,mysql,sqlite}
#
apt -yqq install libapache2-mod-php$PHP_VERSION
#
# enable mods
php5enmod mcrypt
#
change_php_ini
#
service apache2 restart
fi
# install PHP7
if [ "$PHP_VERSION" = "7.0" ]; then
#
echo "Installing PHP$PHP_VERSION"
apt -yqq install php$PHP_VERSION php$PHP_VERSION-cli
apt -yqq install php$PHP_VERSION-{mbstring,curl,gd,mcrypt,json,xml,mysql,sqlite}
#
apt -yqq install libapache2-mod-php$PHP_VERSION
#
change_php_ini
#
service apache2 restart
fi
# install PHP[7.2]
if [ "$PHP_VERSION" = "7.2" ] || [ "$PHP_VERSION" = "7.4" ]; then
#
echo "Installing PHP$PHP_VERSION"
apt -yqq install php$PHP_VERSION php$PHP_VERSION-cli
apt -yqq install php$PHP_VERSION-{mbstring,curl,gd,json,xml,mysql,sqlite3,opcache,zip}
#
apt -yqq install libapache2-mod-php$PHP_VERSION
#
change_php_ini
#
service apache2 restart
fi
# install PHP[7.4]
if [ "$PHP_VERSION" = "7.4" ]; then
#
echo "Installing PHP"
apt -yqq install php php-cli
apt -yqq install php-{common,bz2,getid3,imagick,intl,mbstring,curl,gd,json,xml,mysql,sqlite3,opcache,zip}
#
apt -yqq install libapache2-mod-php
#
change_php_ini
#
service apache2 restart
fi
}
change_php_ini() {
#
sed -i 's/memory_limit\s*=.*/memory_limit = 1024M/g' /etc/php/$PHP_VERSION/apache2/php.ini
sed -i 's/max_execution_time\s*=.*/max_execution_time = 300/g' /etc/php/$PHP_VERSION/apache2/php.ini
sed -i 's/post_max_size\s*=.*/post_max_size = 128M/g' /etc/php/$PHP_VERSION/apache2/php.ini
sed -i 's/upload_max_filesize\s*=.*/upload_max_filesize = 128M/g' /etc/php/$PHP_VERSION/apache2/php.ini
sed -i 's/max_file_uploads\s*=.*/max_file_uploads = 5/g' /etc/php/$PHP_VERSION/apache2/php.ini
sed -i "s/.*upload_tmp_dir.*/upload_tmp_dir = \/tmp\/php-uploads/" /etc/php/$PHP_VERSION/apache2/php.ini
}
#
##
install_mysql() {
#
debconf-set-selections <<< "mariadb-server-10.0 mysql-server/root_password password $rootdbpass"
debconf-set-selections <<< "mariadb-server-10.0 mysql-server/root_password_again password $rootdbpass"
#
apt -yqq install mariadb-server
apt -yqq install mariadb-client
#
sed -i "s/.*bind-address.*/bind-address = 0.0.0.0/" /etc/mysql/mariadb.conf.d/50-server.cnf
#
service mysql start
#
while ! mysqladmin ping --silent -u"root" -p"$rootdbpass"; do
echo >&2 "Deploy-Script: [database] - waiting for database server to start +2s"
sleep 2
done
#
mysql -u root -p"$rootdbpass" -e "CREATE DATABASE IF NOT EXISTS \`$dbname\` /*\!40100 DEFAULT CHARACTER SET utf8mb4 */;"
mysql -u root -p"$rootdbpass" -e "CREATE USER IF NOT EXISTS $dbuser@'%' IDENTIFIED BY '$dbpass';"
mysql -u root -p"$rootdbpass" -e "GRANT ALL PRIVILEGES ON \`$dbname\`.* TO '$dbuser'@'%';"
mysql -u root -p"$rootdbpass" -e "GRANT ALL PRIVILEGES on *.* to 'root'@'localhost' IDENTIFIED BY '$rootdbpass';"
mysql -u root -p"$rootdbpass" -e "FLUSH PRIVILEGES;"
}
start_install(){
#
. /etc/os-release
# check is root user
if [[ $EUID -ne 0 ]]; then
echo "You must be root user to install scripts."
sudo su
fi
# check os is ubuntu
if [[ $ID != "ubuntu" ]]; then
echo "Wrong OS! Sorry only Ubuntu is supported."
exit 1
fi
export DEBIAN_FRONTEND=noninteractive
echo >&2 "Deploy-Script: [OS] $PRETTY_NAME"
}
end_install(){
# clean up apt
apt-get autoremove -y && apt-get autoclean -y && apt-get clean -y \
&& rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
# return to dialog
export DEBIAN_FRONTEND=dialog
# remove script
rm -f script.sh
}
#
##
main() {
#
start_install
#
update_system
#
install_base_system
#
define_passwords
#
install_apache
#
install_php
#
install_adminer
#
install_mysql
#
end_install
echo >&2 "LAMP install completed - Database user and passwords are in /root/passwords.txt"
}
main