forked from nextcloud/vm
-
Notifications
You must be signed in to change notification settings - Fork 5
/
nextcloud_install_production.sh
547 lines (470 loc) · 14.7 KB
/
nextcloud_install_production.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
#!/bin/bash
# shellcheck disable=2034,2059,2140
true
# shellcheck source=lib.sh
FIRST_IFACE=1 && CHECK_CURRENT_REPO=1 . <(curl -sL https://raw.githubusercontent.com/techandme/NextBerry/master/lib.sh)
unset FIRST_IFACE
unset CHECK_CURRENT_REPO
# Tech and Me © - 2017, https://www.techandme.se/
# Check for errors + debug code and abort if something isn't right
# 1 = ON
# 0 = OFF
DEBUG=0
debug_mode
# Check if root
if ! is_root
then
printf "\n${Red}Sorry, you are not root.\n${Color_Off}You must type: ${Cyan}sudo ${Color_Off}bash %s/nextcloud_install_production.sh\n" "$SCRIPTS"
exit 1
fi
# Erase some dev tracks
cat /dev/null > /var/log/syslog
# Prefer IPv4
sed -i "s|#precedence ::ffff:0:0/96 100|precedence ::ffff:0:0/96 100|g" /etc/gai.conf
# Change hostname
hostnamectl set-hostname nextberry
sed -i 's|raspberrypi|nextberry localhost nextcloud|g' /etc/hosts
# Show current user
clear
echo
echo "Current user with sudo permissions is: $UNIXUSER".
echo "This script will set up everything with that user."
echo "If the field after ':' is blank you are probably running as a pure root user."
echo "It's possible to install with root, but there will be minor errors."
echo
echo "Please create a user with sudo permissions if you want an optimal installation."
run_static_script adduser
echo
# Check if key is available
if ! wget -q -T 10 -t 2 "$NCREPO" > /dev/null
then
echo "Nextcloud repo is not available, exiting..."
exit 1
fi
# Check if it's a clean server
is_this_installed postgresql
is_this_installed apache2
is_this_installed php
is_this_installed mysql-common
is_this_installed mariadb-server
echo "deb http://mirrordirector.raspbian.org/raspbian/ stretch main contrib non-free rpi" >> /etc/apt/sources.list
cat > /etc/apt/preferences <<EOF
Package: *
Pin: release n=jessie
Pin-Priority: 600
EOF
# Update and upgrade
clear
printf "${Cyan}Performing autoclean...${Color_Off}\n\n"
"$APT" autoclean -q4 & spinner_loading
printf "Done...\n\n"
echo
printf "${Cyan}Performing autoremove...${Color_Off}\n\n"
"$APT" autoremove -y -q4 & spinner_loading
printf "Done...\n\n"
echo
printf "${Cyan}Updating system...${Color_Off}\n\n"
"$APT" update -q4 & spinner_loading
printf "Done...\n\n"
echo
printf "${Cyan}Upgrading system...${Color_Off}\n\n"
"$APT" upgrade -y -q4 & spinner_loading
"$APT" dist-upgrade -y -q4 & spinner_loading
printf "Done...\n\n"
echo
printf "${Cyan}Installing missing packages...${Color_Off}\n\n"
"$APT" install -fy -q4 & spinner_loading
printf "Done...\n\n"
echo
printf "${Cyan}Performing: dpkg configure${Color_Off}\n\n"
dpkg --configure --pending
printf "Done...\n\n"
echo
printf "${Cyan}Installing additional packages...${Color_Off}\n\n"
"$APT" install -y htop git ntp ntpdate figlet ufw dnsutils pastebinit
#libgd3 libwebp5 libc-client2007e libmcrypt4 libpg5 libxslt1.1
printf "Done...\n\n"
# Enable apps to connect to RPI and read vcgencmd
usermod -aG video $NCUSER
# Create $SCRIPTS dir
if [ ! -d "$SCRIPTS" ]
then
mkdir -p "$SCRIPTS"
fi
# Set swap if we're using a HD
if [ ! -f "$NCUSER/.hd" ]
then
# Only use swap to prevent out of memory. Speed and less tear on SD
echo "vm.swappiness = 0" >> /etc/sysctl.conf
sysctl -p
else
sed -i 's|#CONF_SWAPFILE=/var/swap|CONF_SWAPFILE=/var/swap|g' /etc/dphys-swapfile
sed -i 's|#CONF_SWAPSIZE=100|CONF_SWAPSIZE=1000|g' /etc/dphys-swapfile
sed -i 's|#CONF_MAXSWAP=2048|CONF_MAXSWAP=2048|g' /etc/dphys-swapfile
/etc/init.d/dphys-swapfile stop
/etc/init.d/dphys-swapfile start
/etc/init.d/dphys-swapfile swapon
echo "vm.swappiness = 10" >> /etc/sysctl.conf
sysctl -p
fi
# Setup firewall-rules
wget -q "$STATIC/firewall-rules" -P /usr/sbin/
chmod +x /usr/sbin/firewall-rules
echo "y" | sudo ufw enable
ufw default deny incoming
ufw limit 22/tcp
ufw allow 80/tcp
ufw allow 443/tcp
# Set NextBerry version for the updater tool
echo "$NEXTBERRYVERSION" > "$SCRIPTS"/.version-nc
echo "$NEXTBERRYVERSIONCLEAN" >> "$SCRIPTS"/.version-nc
# Change DNS
if ! [ -x "$(command -v resolvconf)" ]
then
"$APT" install resolvconf -y -q
dpkg-reconfigure resolvconf
fi
echo "nameserver 8.8.8.8" > /etc/resolv.conf.head
echo "nameserver 8.8.4.4" >> /etc/resolv.conf.head
# Check network
if ! [ -x "$(command -v nslookup)" ]
then
"$APT" install dnsutils -y -q
fi
if ! [ -x "$(command -v ifup)" ]
then
"$APT" install ifupdown -y -q
fi
sudo ifdown "$IFACE" && sudo ifup "$IFACE"
if ! nslookup google.com
then
echo "Network NOT OK. You must have a working Network connection to run this script."
exit 1
fi
# Set locales
#"$APT" install language-pack-en-base -y
sudo locale-gen "en_US.UTF-8" && sudo dpkg-reconfigure --frontend=noninteractive locales
# Set keyboard layout
echo "Current keyboard layout is $(localectl status | grep "Layout" | awk '{print $3}')"
if [[ "no" == $(ask_yes_or_no "Do you want to change keyboard layout?") ]]
then
echo "Not changing keyboard layout..."
sleep 1
clear
else
dpkg-reconfigure keyboard-configuration
clear
fi
# Write MARIADB pass to file and keep it safe
cat << LOGIN > "$MYCNF"
[client]
password='$MARIADB_PASS'
default-character-set = utf8mb4
[mariadb]
innodb_use_fallocate = 1
innodb_use_atomic_writes = 1
innodb_use_trim = 1
[mysql]
default-character-set = utf8mb4
[mysqld]
innodb_large_prefix=on
innodb_file_format=barracuda
innodb_flush_neighbors=0
innodb_adaptive_flushing=1
innodb_flush_method = O_DIRECT
innodb_doublewrite = 0
innodb_file_per_table = 1
innodb_flush_log_at_trx_commit=1
init-connect='SET NAMES utf8mb4'
collation_server=utf8mb4_unicode_ci
character_set_server=utf8mb4
skip-character-set-client-handshake
LOGIN
chmod 0600 $MYCNF
chown root:root $MYCNF
# Install MARIADB
"$APT" install software-properties-common -y
# add repo, figure this out on rasbian
sudo debconf-set-selections <<< "mariadb-server-10.2 mysql-server/root_password password $MARIADB_PASS"
sudo debconf-set-selections <<< "mariadb-server-10.2 mysql-server/root_password_again password $MARIADB_PASS"
"$APT" update -q4 & spinner_loading
check_command "$APT" install mariadb-server-10.2 -y
# Prepare for Nextcloud installation
# https://blog.v-gar.de/2017/02/en-solved-error-1698-28000-in-mysqlmariadb/
mysql -u root mysql -p"$MARIADB_PASS" -e "UPDATE user SET plugin='' WHERE user='root';"
mysql -u root mysql -p"$MARIADB_PASS" -e "UPDATE user SET password=PASSWORD('$MARIADB_PASS') WHERE user='root';"
mysql -u root -p"$MARIADB_PASS" -e "flush privileges;"
# mysql_secure_installation
aptitude -y install expect
SECURE_MYSQL=$(expect -c "
set timeout 10
spawn mysql_secure_installation
expect \"Enter current password for root (enter for none):\"
send \"$MARIADB_PASS\r\"
expect \"Change the root password?\"
send \"n\r\"
expect \"Remove anonymous users?\"
send \"y\r\"
expect \"Disallow root login remotely?\"
send \"y\r\"
expect \"Remove test database and access to it?\"
send \"y\r\"
expect \"Reload privilege tables now?\"
send \"y\r\"
expect eof
")
echo "$SECURE_MYSQL"
aptitude -y purge expect
# Install Apache
check_command "$APT" install -t stretch apache2 -y
a2enmod rewrite \
headers \
env \
dir \
mime \
ssl \
setenvif
a2dissite 000-default.conf
# Install PHP 7.0
check_command "$APT" install -t stretch -y \
php7.0 \
libapache2-mod-php7.0 \
php7.0-common \
php7.0-mysql \
php7.0-intl \
php7.0-mcrypt \
php7.0-ldap \
php7.0-imap \
php7.0-cli \
php7.0-gd \
php7.0-pgsql \
php7.0-json \
php7.0-sqlite3 \
php7.0-curl \
php7.0-xml \
php7.0-zip \
php7.0-mbstring \
php7.0-opcache \
php7.0-fpm
"$APT" install -y
libxml2-dev \
php-zip \
php-dom \
php-xmlwriter \
php-xmlreader \
php-gd \
php-curl \
php-mbstring
"$APT" install -y php-smbclient
# Enable SMB client
echo '# This enables php-smbclient' >> /etc/php/7.0/apache2/php.ini
echo 'extension="smbclient.so"' >> /etc/php/7.0/apache2/php.ini
# Download and validate Nextcloud package
check_command download_verify_nextcloud_stable
if [ ! -f "$HTML/$STABLEVERSION.tar.bz2" ]
then
echo "Aborting,something went wrong with the download of $STABLEVERSION.tar.bz2"
exit 1
fi
# Extract package
tar -xjf "$HTML/$STABLEVERSION.tar.bz2" -C "$HTML" & spinner_loading
rm "$HTML/$STABLEVERSION.tar.bz2"
# Secure permissions
download_static_script setup_secure_permissions_nextcloud
bash "$SECURE" & spinner_loading
# Create database nextcloud_db
mysql -u root -p"$MARIADB_PASS" -e "CREATE DATABASE IF NOT EXISTS nextcloud_db;"
# Install Nextcloud
cd "$NCPATH"
check_command sudo -u www-data php occ maintenance:install \
--data-dir "$NCDATA" \
--database "mysql" \
--database-name "nextcloud_db" \
--database-user "root" \
--database-pass "$MARIADB_PASS" \
--admin-user "$NCUSER" \
--admin-pass "$NCPASS"
echo
echo "Nextcloud version:"
sudo -u www-data php "$NCPATH"/occ status
sleep 3
echo
# Prepare cron.php to be run every 15 minutes
crontab -u www-data -l | { cat; echo "*/15 * * * * php -f $NCPATH/cron.php > /dev/null 2>&1"; } | crontab -u www-data -
# Change values in php.ini (increase max file size)
# max_execution_time
sed -i "s|max_execution_time =.*|max_execution_time = 3500|g" /etc/php/7.0/apache2/php.ini
# max_input_time
sed -i "s|max_input_time =.*|max_input_time = 3600|g" /etc/php/7.0/apache2/php.ini
# memory_limit
sed -i "s|memory_limit =.*|memory_limit = 256M|g" /etc/php/7.0/apache2/php.ini
# post_max
sed -i "s|post_max_size =.*|post_max_size = 1100M|g" /etc/php/7.0/apache2/php.ini
# upload_max
sed -i "s|upload_max_filesize =.*|upload_max_filesize = 1000M|g" /etc/php/7.0/apache2/php.ini
# Set max upload in Nextcloud .htaccess
configure_max_upload
# Set SMTP mail
sudo -u www-data php "$NCPATH"/occ config:system:set mail_smtpmode --value="smtp"
# Enable OPCache for PHP
# https://docs.nextcloud.com/server/12/admin_manual/configuration_server/server_tuning.html#enable-php-opcache
phpenmod opcache
{
echo "# OPcache settings for Nextcloud"
echo "opcache.enable=1"
echo "opcache.enable_cli=1"
echo "opcache.interned_strings_buffer=8"
echo "opcache.max_accelerated_files=10000"
echo "opcache.memory_consumption=128"
echo "opcache.save_comments=1"
echo "opcache.revalidate_freq=1"
echo "opcache.validate_timestamps=1"
} >> /etc/php/7.0/apache2/php.ini
# Enable http2
cat >/etc/apache2/conf-available/http2.conf <<EOF
Protocols h2 h2c http/1.1
H2Push on
H2PushPriority * after
H2PushPriority text/css before
H2PushPriority image/jpeg after 32
H2PushPriority image/png after 32
H2PushPriority application/javascript interleaved
SSLProtocol all -SSLv2 -SSLv3
SSLHonorCipherOrder on
SSLCipherSuite 'EECDH+ECDSA+AESGCM EECDH+aRSA+AESGCM EECDH+ECDSA+SHA384 EECDH+ECDSA+SHA256 EECDH+aRSA+SHA384 EECDH+aRSA+SHA256 EECDH+aRSA+RC4 EECDH EDH+aRSA !RC4 !aNULL !eNULL !LOW !3DES !MD5 !EXP !PSK !SRP !DSS'
EOF
a2enmod http2
a2enconf http2
# Install preview generator
run_app_script previewgenerator
# Generate $HTTP_CONF
if [ ! -f $HTTP_CONF ]
then
touch "$HTTP_CONF"
cat << HTTP_CREATE > "$HTTP_CONF"
<VirtualHost *:80>
### YOUR SERVER ADDRESS ###
# ServerAdmin [email protected]
# ServerName example.com
# ServerAlias subdomain.example.com
### SETTINGS ###
DocumentRoot $NCPATH
<Directory $NCPATH>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
Satisfy Any
</Directory>
<IfModule mod_dav.c>
Dav off
</IfModule>
<Directory "$NCDATA">
# just in case if .htaccess gets disabled
Require all denied
</Directory>
SetEnv HOME $NCPATH
SetEnv HTTP_HOME $NCPATH
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
</VirtualHost>
HTTP_CREATE
echo "$HTTP_CONF was successfully created"
fi
# Generate $SSL_CONF
if [ ! -f $SSL_CONF ]
then
touch "$SSL_CONF"
cat << SSL_CREATE > "$SSL_CONF"
<VirtualHost *:443>
Header add Strict-Transport-Security: "max-age=15768000; includeSubdomains; preload"
SSLEngine on
### YOUR SERVER ADDRESS ###
# ServerAdmin [email protected]
# ServerName example.com
# ServerAlias subdomain.example.com
### SETTINGS ###
DocumentRoot $NCPATH
<Directory $NCPATH>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
Satisfy Any
</Directory>
<IfModule mod_dav.c>
Dav off
</IfModule>
<Directory "$NCDATA">
# just in case if .htaccess gets disabled
Require all denied
</Directory>
SetEnv HOME $NCPATH
SetEnv HTTP_HOME $NCPATH
### LOCATION OF CERT FILES ###
SSLCertificateFile /etc/ssl/certs/ssl-cert-snakeoil.pem
SSLCertificateKeyFile /etc/ssl/private/ssl-cert-snakeoil.key
</VirtualHost>
SSL_CREATE
echo "$SSL_CONF was successfully created"
fi
# Enable new config
a2ensite nextcloud_ssl_domain_self_signed.conf
a2ensite nextcloud_http_domain_self_signed.conf
a2dissite default-ssl
service apache2 restart
# Install Libreoffice Writer to be able to read MS documents.
sudo "$APT" install --no-install-recommends libreoffice-writer -y
sudo -u www-data php "$NCPATH"/occ config:system:set preview_libreoffice_path --value="/usr/bin/libreoffice"
# Nextcloud apps
whiptail --title "Which apps/programs do you want to install?" --checklist --separate-output "" 10 40 3 \
"Calendar" " " on \
"Contacts" " " on \
"Webmin" " " off 2>results
while read -r -u 9 choice
do
case "$choice" in
Calendar)
run_app_script calendar
;;
Contacts)
run_app_script contacts
;;
Webmin)
run_app_script webmin
;;
*)
;;
esac
done 9< results
rm -f results
# Get needed scripts for first bootup
if [ ! -f "$SCRIPTS"/nextcloud-startup-script.sh ]
then
check_command wget -q "$GITHUB_REPO"/nextcloud-startup-script.sh -P "$SCRIPTS"
fi
download_static_script instruction
download_static_script history
# Make $SCRIPTS excutable
chmod +x -R "$SCRIPTS"
chown root:root -R "$SCRIPTS"
# Prepare first bootup
check_command run_static_script change-ncadmin-profile
check_command run_static_script change-root-profile
# Install Redis
run_static_script redis-server-ubuntu16
# Cleanup login screen
cat /dev/null > /etc/motd
# Cleanup
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)
echo "$CLEARBOOT"
"$APT" autoremove -y
"$APT" autoclean
find /root "/home/$UNIXUSER" -type f \( -name '*.sh*' -o -name '*.html*' -o -name '*.tar*' -o -name '*.zip*' \) -delete
# Set secure permissions final (./data/.htaccess has wrong permissions otherwise)
bash "$SECURE" & spinner_loading
# Set version
sudo -u www-data php "$NCPATH"/occ status | grep "versionstring" | awk '{print $3}' > "$SCRIPTS/.versionnc"
chmod 777 :$SCRIPTS/.versionnc
# Reboot
echo "Installation done, system will now reboot..."
reboot