forked from nextcloud/vm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnextcloud-startup-script.sh
577 lines (513 loc) · 16.3 KB
/
nextcloud-startup-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
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
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
#!/bin/bash
# Tech and Me - ©2016, https://www.techandme.se/
# Check for errors + debug code and abort if something isn't right
# 1 = ON
# 0 = OFF
DEBUG=0
WWW_ROOT=/var/www
NCPATH=$WWW_ROOT/nextcloud
NCDATA=/var/ncdata
SCRIPTS=/var/scripts
PW_FILE=/var/mysql_password.txt # Keep in sync with nextcloud_install_production.sh
IFACE=$(lshw -c network | grep "logical name" | awk '{print $3; exit}')
CLEARBOOT=$(dpkg -l linux-* | awk '/^ii/{ print $2}' | grep -v -e `uname -r | cut -f1,2 -d"-"` | grep -e [0-9] | xargs sudo apt-get -y purge)
PHPMYADMIN_CONF="/etc/apache2/conf-available/phpmyadmin.conf"
GITHUB_REPO="https://raw.githubusercontent.com/nextcloud/vm/master"
STATIC="https://raw.githubusercontent.com/nextcloud/vm/master/static"
LETS_ENC="https://raw.githubusercontent.com/nextcloud/vm/master/lets-encrypt"
UNIXUSER=ncadmin
UNIXPASS=nextcloud
# DEBUG mode
if [ $DEBUG -eq 1 ]
then
set -e
set -x
else
sleep 1
fi
# Check if root
if [ "$(whoami)" != "root" ]
then
echo
echo -e "\e[31mSorry, you are not root.\n\e[0mYou must type: \e[36msudo \e[0mbash $SCRIPTS/nextcloud-startup-script.sh"
echo
exit 1
fi
# Check network
echo "Testing if network is OK..."
sleep 2
service networking restart
wget -q --spider http://github.com
if [ $? -eq 0 ]
then
echo -e "\e[32mOnline!\e[0m"
else
echo "Setting correct interface..."
# Set correct interface
{ sed '/# The primary network interface/q' /etc/network/interfaces; printf 'auto %s\niface %s inet dhcp\n# This is an autoconfigured IPv6 interface\niface %s inet6 auto\n' "$IFACE" "$IFACE" "$IFACE"; } > /etc/network/interfaces.new
mv /etc/network/interfaces.new /etc/network/interfaces
service networking restart
fi
# Check network
echo "Testing if network is OK..."
sleep 2
service networking restart
wget -q --spider http://github.com
if [ $? -eq 0 ]
then
echo -e "\e[32mOnline!\e[0m"
else
echo
echo "Network NOT OK. You must have a working Network connection to run this script."
echo "Please report this issue here: https://github.com/nextcloud/vm/issues/new"
exit 1
fi
ADDRESS=$(hostname -I | cut -d ' ' -f 1)
echo "Getting scripts from GitHub to be able to run the first setup..."
# Get spreedme script
if [ -f $SCRIPTS/spreedme.sh ]
then
rm $SCRIPTS/spreedme.sh
wget -q $STATIC/spreedme.sh -P $SCRIPTS
else
wget -q $STATIC/spreedme.sh -P $SCRIPTS
fi
# Get script for temporary fixes
if [ -f $SCRIPTS/temporary.sh ]
then
rm $SCRIPTS/temporary-fix.sh
wget -q $STATIC/temporary-fix.sh -P $SCRIPTS
else
wget -q $STATIC/temporary-fix.sh -P $SCRIPTS
fi
# Get security script
if [ -f $SCRIPTS/security.sh ]
then
rm $SCRIPTS/security.sh
wget -q $STATIC/security.sh -P $SCRIPTS
else
wget -q $STATIC/security.sh -P $SCRIPTS
fi
# Get the latest nextcloud_update.sh
if [ -f $SCRIPTS/update.sh ]
then
rm $SCRIPTS/update.sh
wget -q $STATIC/update.sh -P $SCRIPTS
else
wget -q $STATIC/update.sh -P $SCRIPTS
fi
# phpMyadmin
if [ -f $SCRIPTS/phpmyadmin_install_ubuntu16.sh ]
then
rm $SCRIPTS/phpmyadmin_install_ubuntu16.sh
wget -q $STATIC/phpmyadmin_install_ubuntu16.sh -P $SCRIPTS
else
wget -q $STATIC/phpmyadmin_install_ubuntu16.sh -P $SCRIPTS
fi
# Update Config
if [ -f $SCRIPTS/update-config.php ]
then
rm $SCRIPTS/update-config.php
wget -q $STATIC/update-config.php -P $SCRIPTS
else
wget -q $STATIC/update-config.php -P $SCRIPTS
fi
# Activate SSL
if [ -f $SCRIPTS/activate-ssl.sh ]
then
rm $SCRIPTS/activate-ssl.sh
wget -q $LETS_ENC/activate-ssl.sh -P $SCRIPTS
else
wget -q $LETS_ENC/activate-ssl.sh -P $SCRIPTS
fi
# The update script
if [ -f $SCRIPTS/nextcloud_update.sh ]
then
rm $SCRIPTS/nextcloud_update.sh
wget -q $GITHUB_REPO/nextcloud_update.sh -P $SCRIPTS
else
wget -q $GITHUB_REPO/nextcloud_update.sh -P $SCRIPTS
fi
# Sets trusted domain in when nextcloud-startup-script.sh is finished
if [ -f $SCRIPTS/trusted.sh ]
then
rm $SCRIPTS/trusted.sh
wget -q $STATIC/trusted.sh -P $SCRIPTS
else
wget -q $STATIC/trusted.sh -P $SCRIPTS
fi
# Sets static IP to UNIX
if [ -f $SCRIPTS/ip.sh ]
then
rm $SCRIPTS/ip.sh
wget -q $STATIC/ip.sh -P $SCRIPTS
else
wget -q $STATIC/ip.sh -P $SCRIPTS
fi
# Tests connection after static IP is set
if [ -f $SCRIPTS/test_connection.sh ]
then
rm $SCRIPTS/test_connection.sh
wget -q $STATIC/test_connection.sh -P $SCRIPTS
else
wget -q $STATIC/test_connection.sh -P $SCRIPTS
fi
# Sets secure permissions after upgrade
if [ -f $SCRIPTS/setup_secure_permissions_nextcloud.sh ]
then
rm $SCRIPTS/setup_secure_permissions_nextcloud.sh
wget -q $STATIC/setup_secure_permissions_nextcloud.sh -P $SCRIPTS
else
wget -q $STATIC/setup_secure_permissions_nextcloud.sh -P $SCRIPTS
fi
# Change MySQL password
if [ -f $SCRIPTS/change_mysql_pass.sh ]
then
rm $SCRIPTS/change_mysql_pass.sh
wget -q $STATIC/change_mysql_pass.sh
else
wget -q $STATIC/change_mysql_pass.sh -P $SCRIPTS
fi
# Get figlet Tech and Me
if [ -f $SCRIPTS/nextcloud.sh ]
then
rm $SCRIPTS/nextcloud.sh
wget -q $STATIC/nextcloud.sh -P $SCRIPTS
else
wget -q $STATIC/nextcloud.sh -P $SCRIPTS
fi
# Get the Welcome Screen when http://$address
if [ -f $SCRIPTS/index.php ]
then
rm $SCRIPTS/index.php
wget -q $GITHUB_REPO/index.php -P $SCRIPTS
else
wget -q $GITHUB_REPO/index.php -P $SCRIPTS
fi
mv $SCRIPTS/index.php $WWW_ROOT/index.php && rm -f $WWW_ROOT/html/index.html
chmod 750 $WWW_ROOT/index.php && chown www-data:www-data $WWW_ROOT/index.php
# Change 000-default to $WEB_ROOT
sed -i "s|DocumentRoot /var/www/html|DocumentRoot $WWW_ROOT|g" /etc/apache2/sites-available/000-default.conf
# Make $SCRIPTS excutable
chmod +x -R $SCRIPTS
chown root:root -R $SCRIPTS
# Allow $UNIXUSER to run figlet script
chown $UNIXUSER:$UNIXUSER $SCRIPTS/nextcloud.sh
clear
echo "+--------------------------------------------------------------------+"
echo "| This script will configure your Nextcloud and activate SSL. |"
echo "| It will also do the following: |"
echo "| |"
echo "| - Generate new SSH keys for the server |"
echo "| - Generate new MySQL password |"
echo "| - Install phpMyadmin and make it secure |"
echo "| - Upgrade your system to latest version |"
echo "| - Set secure permissions to Nextcloud |"
echo "| - Set new passwords to Ubuntu Server and Nextcloud |"
echo "| - Set new keyboard layout |"
echo "| - Change timezone |"
echo "| - Set static IP to the system (you have to set the same IP in |"
echo "| your router) https://www.techandme.se/open-port-80-443/ |"
echo "| |"
echo "| The script will take about 10 minutes to finish, |"
echo "| depending on your internet connection. |"
echo "| |"
echo "| ####################### Tech and Me - 2016 ####################### |"
echo "+--------------------------------------------------------------------+"
echo -e "\e[32m"
read -p "Press any key to start the script..." -n1 -s
clear
echo -e "\e[0m"
# Set keyboard layout
echo "Current keyboard layout is Swedish"
echo "You must change keyboard layout to your language"
echo -e "\e[32m"
read -p "Press any key to change keyboard layout... " -n1 -s
echo -e "\e[0m"
dpkg-reconfigure keyboard-configuration
echo
clear
# Change IP
echo -e "\e[0m"
echo "The script will now configure your IP to be static."
echo -e "\e[36m"
echo -e "\e[1m"
echo "Your internal IP is: $ADDRESS"
echo -e "\e[0m"
echo -e "Write this down, you will need it to set static IP"
echo -e "in your router later. It's included in this guide:"
echo -e "https://www.techandme.se/open-port-80-443/ (step 1 - 5)"
echo -e
echo -e "Please note that we will backup the interfaces file to:"
echo -e "/etc/network/interfaces.backup"
echo -e "If you run this script on a remote VPS the IP is probably wrong. "
echo -e "But no worries - we will restore the interfaces.backup in the end of this script."
echo -e "\e[32m"
read -p "Press any key to set static IP..." -n1 -s
cp /etc/network/interfaces /etc/network/interfaces.backup
clear
echo -e "\e[0m"
ifdown $IFACE
sleep 2
ifup $IFACE
sleep 2
bash $SCRIPTS/ip.sh
ifdown $IFACE
sleep 2
ifup $IFACE
sleep 2
echo
echo "Testing if network is OK..."
sleep 1
echo
bash $SCRIPTS/test_connection.sh
sleep 2
echo
echo -e "\e[0mIf the output is \e[32mConnected! \o/\e[0m everything is working."
echo -e "\e[0mIf the output is \e[31mNot Connected!\e[0m you should change\nyour settings manually in the next step."
echo -e "\e[32m"
read -p "Press any key to open /etc/network/interfaces..." -n1 -s
echo -e "\e[0m"
nano /etc/network/interfaces
service networking restart
clear
echo "Testing if network is OK..."
ifdown $IFACE
sleep 2
ifup $IFACE
sleep 2
echo
bash $SCRIPTS/test_connection.sh
sleep 2
clear
# Pretty URLs
echo "Setting RewriteBase to "/" in config.php..."
chown www-data:www-data $NCPATH/config/config.php
sudo -u www-data php $NCPATH/occ config:system:set htaccess.RewriteBase --value="/"
sudo -u www-data php $NCPATH/occ maintenance:update:htaccess
# Generate new SSH Keys
echo
echo "Generating new SSH keys for the server..."
sleep 1
rm -v /etc/ssh/ssh_host_*
dpkg-reconfigure openssh-server
# Generate new MySQL password
echo
bash $SCRIPTS/change_mysql_pass.sh
rm $SCRIPTS/change_mysql_pass.sh
# Install phpMyadmin
echo
bash $SCRIPTS/phpmyadmin_install_ubuntu16.sh
rm $SCRIPTS/phpmyadmin_install_ubuntu16.sh
clear
# Install SpreedMe
function ask_yes_or_no() {
read -p "$1 ([y]es or [N]o): "
case $(echo $REPLY | tr '[A-Z]' '[a-z]') in
y|yes) echo "yes" ;;
*) echo "no" ;;
esac
}
if [[ "yes" == $(ask_yes_or_no "Do you want to install SpreedMe?") ]]
then
bash $SCRIPTS/spreedme.sh
rm $SCRIPTS/spreedme.sh
else
echo
echo "OK, but if you want to run it later, just type: sudo bash $SCRIPTS/spreedme.sh"
echo -e "\e[32m"
read -p "Press any key to continue... " -n1 -s
echo -e "\e[0m"
fi
clear
# Add extra security
function ask_yes_or_no() {
read -p "$1 ([y]es or [N]o): "
case $(echo $REPLY | tr '[A-Z]' '[a-z]') in
y|yes) echo "yes" ;;
*) echo "no" ;;
esac
}
if [[ "yes" == $(ask_yes_or_no "Do you want to add extra security, based on this: http://goo.gl/gEJHi7 ?") ]]
then
bash $SCRIPTS/security.sh
rm $SCRIPTS/security.sh
else
echo
echo "OK, but if you want to run it later, just type: sudo bash $SCRIPTS/security.sh"
echo -e "\e[32m"
read -p "Press any key to continue... " -n1 -s
echo -e "\e[0m"
fi
clear
# Change Timezone
echo "Current timezone is Europe/Stockholm"
echo "You must change timezone to your timezone"
echo -e "\e[32m"
read -p "Press any key to change timezone... " -n1 -s
echo -e "\e[0m"
dpkg-reconfigure tzdata
echo
sleep 3
clear
if [ "$UNIXUSER" = "ncadmin" ]
then
# Change password
echo -e "\e[0m"
echo "For better security, change the Linux password for [$UNIXUSER]"
echo "The current password is [$UNIXPASS]"
echo -e "\e[32m"
read -p "Press any key to change password for Linux... " -n1 -s
echo -e "\e[0m"
sudo passwd $UNIXUSER
if [[ $? > 0 ]]
then
sudo passwd $UNIXUSER
else
sleep 2
fi
echo
clear
echo -e "\e[0m"
echo "For better security, change the Nextcloud password for [$UNIXUSER]"
echo "The current password is [$UNIXPASS]"
echo -e "\e[32m"
read -p "Press any key to change password for Nextcloud... " -n1 -s
echo -e "\e[0m"
sudo -u www-data php $NCPATH/occ user:resetpassword $UNIXUSER
if [[ $? > 0 ]]
then
sudo -u www-data php $NCPATH/occ user:resetpassword $UNIXUSER
else
sleep 2
fi
clear
else
echo "Not changing password as you already changed <user> and <pass> in the script"
fi
# Upgrade system
echo "System will now upgrade..."
sleep 2
echo
bash $SCRIPTS/update.sh
# Increase max filesize (expects that changes are made in /etc/php/7.0/apache2/php.ini)
# Here is a guide: https://www.techandme.se/increase-max-file-size/
VALUE="# php_value upload_max_filesize 513M"
if grep -Fxq "$VALUE" $NCPATH/.htaccess
then
echo "Value correct"
else
sed -i 's/ php_value upload_max_filesize 513M/# php_value upload_max_filesize 513M/g' $NCPATH/.htaccess
sed -i 's/ php_value post_max_size 513M/# php_value post_max_size 513M/g' $NCPATH/.htaccess
sed -i 's/ php_value memory_limit 512M/# php_value memory_limit 512M/g' $NCPATH/.htaccess
fi
# Add temporary fix if needed
bash $SCRIPTS/temporary-fix.sh
rm $SCRIPTS/temporary-fix.sh
# Cleanup 1
apt-get autoremove -y
apt-get autoclean
echo "$CLEARBOOT"
clear
# Cleanup 2
sudo -u www-data php $NCPATH/occ maintenance:repair
rm $SCRIPTS/ip.sh
rm $SCRIPTS/test_connection.sh
rm $SCRIPTS/instruction.sh
rm $NCDATA/nextcloud.log
rm $SCRIPTS/nextcloud-startup-script.sh
sed -i "s|instruction.sh|nextcloud.sh|g" /home/$UNIXUSER/.bash_profile
cat /dev/null > ~/.bash_history
cat /dev/null > /var/spool/mail/root
cat /dev/null > /var/spool/mail/$UNIXUSER
cat /dev/null > /var/log/apache2/access.log
cat /dev/null > /var/log/apache2/error.log
cat /dev/null > /var/log/cronjobs_success.log
sed -i "s|sudo -i||g" /home/$UNIXUSER/.bash_profile
cat /dev/null > /etc/rc.local
cat << RCLOCAL > "/etc/rc.local"
#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.
exit 0
RCLOCAL
ADDRESS2=$(grep "address" /etc/network/interfaces | awk '$1 == "address" { print $2 }')
# Success!
clear
echo -e "\e[32m"
echo "+--------------------------------------------------------------------+"
echo "| Congratulations! You have successfully installed Nextcloud! |"
echo "| |"
echo -e "| \e[0mLogin to Nextcloud in your browser:\e[36m" $ADDRESS2"\e[32m |"
echo "| |"
echo -e "| \e[0mPublish your server online! \e[36mhttps://goo.gl/iUGE2U\e[32m |"
echo "| |"
echo -e "| \e[0mYour MySQL password is stored in: \e[36m$PW_FILE\e[32m |"
echo "| |"
echo -e "| \e[91m#################### Tech and Me - 2016 ####################\e[32m |"
echo "+--------------------------------------------------------------------+"
echo
echo
# VPS?
function ask_yes_or_no() {
read -p "$1 ([y]es or [N]o): "
case $(echo $REPLY | tr '[A-Z]' '[a-z]') in
y|yes) echo "yes" ;;
*) echo "no" ;;
esac
}
if [[ "yes" == $(ask_yes_or_no "Do you run this on a *remote* VPS?") ]]
then
echo "Ok, then your IP are probably wrong, we will use the backup file to recover it so that you can connect after reboot"
echo -e "\e[32m"
read -p "Press any key to continue... " -n1 -s
echo -e "\e[0m"
mv /etc/network/interfaces.backup /etc/network/interfaces
else
sleep 1
fi
clear
echo
echo
cat << LETSENC
+-----------------------------------------------+
| Ok, now the last part - a proper SSL cert. |
| |
| The following script will install a trusted |
| SSL certificate through Let's Encrypt. |
+-----------------------------------------------+
LETSENC
# Let's Encrypt
function ask_yes_or_no() {
read -p "$1 ([y]es or [N]o): "
case $(echo $REPLY | tr '[A-Z]' '[a-z]') in
y|yes) echo "yes" ;;
*) echo "no" ;;
esac
}
if [[ "yes" == $(ask_yes_or_no "Do you want to install SSL?") ]]
then
bash $SCRIPTS/activate-ssl.sh
else
echo
echo "OK, but if you want to run it later, just type: sudo bash $SCRIPTS/activate-ssl.sh"
echo -e "\e[32m"
read -p "Press any key to continue... " -n1 -s
echo -e "\e[0m"
fi
# Change Trusted Domain and CLI
bash $SCRIPTS/trusted.sh
rm $SCRIPTS/trusted.sh
rm $SCRIPTS/update-config.php
# Reboot
reboot
exit 0