-
Notifications
You must be signed in to change notification settings - Fork 14
/
nextcloud-behind-wireguard.sh
347 lines (292 loc) · 16.4 KB
/
nextcloud-behind-wireguard.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
#!/bin/bash
# visual text settings
RED="\e[31m"
GREEN="\e[32m"
GRAY="\e[37m"
YELLOW="\e[93m"
REDB="\e[41m"
GREENB="\e[42m"
GRAYB="\e[47m"
ENDCOLOR="\e[0m"
clear
echo -e " ${GRAYB}#######################################################################################################################################${ENDCOLOR}"
echo -e " ${GRAYB}#${ENDCOLOR} ${GREEN}Nextcloud addon to my wireguard_dnscrypt_setup.sh ${ENDCOLOR}${GRAYB}#${ENDCOLOR}"
echo -e " ${GRAYB}#${ENDCOLOR} ${GRAYB} !!! This addon is for Debian 12 only !!! ${ENDCOLOR}${GRAYB}#${ENDCOLOR}"
echo -e " ${GRAYB}#${ENDCOLOR} ${GREEN}My target, a secure Nextcloud instance, behind wireguard. ${ENDCOLOR}${GRAYB}#${ENDCOLOR}"
echo -e " ${GRAYB}#${ENDCOLOR} ${GREEN}So no wiregard connection, no nextcloud connection ${ENDCOLOR}${GRAYB}#${ENDCOLOR}"
echo -e " ${GRAYB}#######################################################################################################################################${ENDCOLOR}"
echo -e " ${GRAYB}#${ENDCOLOR} Version XXXX.XX.XX - no changelog now 8 ${GRAYB}#${ENDCOLOR}"
echo -e " ${GRAYB}#######################################################################################################################################${ENDCOLOR}"
echo ""
echo ""
echo ""
echo -e " ${RED}To EXIT this script press any key${ENDCOLOR}"
echo ""
echo -e " ${GREEN}Press [Y] to begin${ENDCOLOR}"
read -p "" -n 1 -r
echo ""
echo ""
if [[ ! $REPLY =~ ^[Yy]$ ]]
then
exit 1
fi
### root check
if [[ "$EUID" -ne 0 ]]; then
echo -e "${RED}Sorry, you need to run this as root${ENDCOLOR}"
exit 1
fi
#
# OS check
#
echo -e "${GREEN}OS check ${ENDCOLOR}"
. /etc/os-release
if [[ "$ID" = 'debian' ]]; then
if [[ "$VERSION_ID" = '12' ]]; then
echo -e "${GREEN}OS = Debian ${ENDCOLOR}"
systemos=debian
fi
fi
if [[ "$systemos" = '' ]]; then
clear
echo ""
echo ""
echo -e "${RED}This script is only for Debian 12!${ENDCOLOR}"
exit 1
fi
### check if script installed
if [[ -e /root/Wireguard-DNScrypt-VPN-Server.README ]]; then
echo -e "${GREEN}OK = Wireguard-DNScrypt-VPN-Server is installed ${ENDCOLOR}"
else
echo -e "${RED} !!! my wireguard script is needed !!!${ENDCOLOR}"
echo -e "${RED} Download here: https://github.com/zzzkeil/Wireguard-DNScrypt-VPN-Server${ENDCOLOR}"
exit 1
fi
ipv4network=$(sed -n 7p /root/Wireguard-DNScrypt-VPN-Server.README)
ipv6network=$(sed -n 9p /root/Wireguard-DNScrypt-VPN-Server.README)
#
# OS updates
#
echo -e "${GREEN}update upgrade and install ${ENDCOLOR}"
if [[ "$systemos" = 'debian' ]]; then
apt update && apt upgrade -y && apt autoremove -y
apt install apache2 libapache2-mod-php mariadb-server php-xml php-cli php-cgi php-mysql php-mbstring php-gd php-curl php-intl php-gmp php-bcmath php-imagick php-zip php-bz2 php-opcache php-common php-redis php-igbinary php-apcu memcached php-memcached unzip libmagickcore-6.q16-6-extra -y
fi
###your vars
clear
randomkey1=$(date +%s | cut -c 3-)
randomkey2=$(</dev/urandom tr -dc 'A-Za-z0-9.:_' | head -c 32 ; echo)
randomkey3=$(</dev/urandom tr -dc 'A-Za-z0-9.:_' | head -c 24 ; echo)
echo ""
echo ""
echo -e " ${GREEN}-- Your turn, make some decisions -- ${ENDCOLOR}"
echo ""
echo "--------------------------------------------------------------------------------------------------------"
echo "--------------------------------------------------------------------------------------------------------"
read -p "Your apache https port: " -e -i 23443 httpsport
echo "--------------------------------------------------------------------------------------------------------"
echo "--------------------------------------------------------------------------------------------------------"
read -p "Your mariaDB port: " -e -i 3306 dbport
echo "--------------------------------------------------------------------------------------------------------"
echo "--------------------------------------------------------------------------------------------------------"
read -p "sql databasename: " -e -i db$randomkey1 databasename
echo "--------------------------------------------------------------------------------------------------------"
echo "--------------------------------------------------------------------------------------------------------"
read -p "sql databaseuser: " -e -i dbuser$randomkey1 databaseuser
echo "--------------------------------------------------------------------------------------------------------"
echo "--------------------------------------------------------------------------------------------------------"
read -p "sql databaseuserpasswd: " -e -i $randomkey2 databaseuserpasswd
echo "--------------------------------------------------------------------------------------------------------"
echo "--------------------------------------------------------------------------------------------------------"
read -p "nextcloud logtimezone (TZ identifier): " -e -i Europe/Berlin ltz
echo "--------------------------------------------------------------------------------------------------------"
echo "--------------------------------------------------------------------------------------------------------"
read -p "nextcloud default phone region (Country code): " -e -i DE dpr
echo "--------------------------------------------------------------------------------------------------------"
echo "--------------------------------------------------------------------------------------------------------"
read -p "nextcloud admin user name: " -e -i nextroot nextroot
echo "--------------------------------------------------------------------------------------------------------"
echo "--------------------------------------------------------------------------------------------------------"
read -p "nextcloud admin password : " -e -i $randomkey3 nextpass
echo "--------------------------------------------------------------------------------------------------------"
echo "--------------------------------------------------------------------------------------------------------"
read -p "nextcloud data folder : " -e -i /opt/nextcloud_data ncdatafolder
echo "--------------------------------------------------------------------------------------------------------"
echo "--------------------------------------------------------------------------------------------------------"
### self-signed certificate
openssl req -x509 -newkey ec:<(openssl ecparam -name secp384r1) -days 1800 -nodes -keyout /etc/ssl/private/nc-selfsigned.key -out /etc/ssl/certs/nc-selfsigned.crt -subj "/C=DE/ST=Your/L=Nextcloud/O=Behind/OU=Wireguard/CN=10.$ipv4network.1"
### apache part
if [[ "$systemos" = 'debian' ]]; then
a2enmod ssl
a2enmod rewrite
a2enmod headers
fi
if [[ "$systemos" = 'debian' ]]; then
systemctl stop apache2.service
fi
mv /etc/apache2/ports.conf /etc/apache2/ports.conf.bak
echo "
Listen 2380
<IfModule ssl_module>
Listen $httpsport
</IfModule>
<IfModule mod_gnutls.c>
Listen $httpsport
</IfModule>
" >> /etc/apache2/ports.conf
cat <<EOF >> /etc/apache2/sites-available/nc.conf
<VirtualHost *:$httpsport>
ServerName 10.$ipv4network.1
DocumentRoot /var/www/nextcloud
SSLEngine on
SSLCertificateFile /etc/ssl/certs/nc-selfsigned.crt
SSLCertificateKeyFile /etc/ssl/private/nc-selfsigned.key
<Directory /var/www/nextcloud/>
AllowOverride All
Require host localhost
Require ip 10.$ipv4network
</Directory>
<IfModule mod_headers.c>
Header always set Strict-Transport-Security "max-age=15552000; includeSubDomains"
</IfModule>
</VirtualHost>
EOF
mkdir -p $ncdatafolder
cd /var/www
curl -o nextcloud.zip https://download.nextcloud.com/server/releases/latest.zip
unzip -qq nextcloud.zip
chown -R www-data:www-data /var/www/nextcloud
chown -R www-data:www-data $ncdatafolder
##php settings nextcloud
cp /etc/php/8.2/apache2/php.ini /etc/php/8.2/apache2/php.ini.bak
sed -i "s/memory_limit = 128M/memory_limit = 1G/" /etc/php/8.2/apache2/php.ini
sed -i "s/output_buffering =.*/output_buffering = '0'/" /etc/php/8.2/apache2/php.ini
sed -i "s/max_execution_time =.*/max_execution_time = 3600/" /etc/php/8.2/apache2/php.ini
sed -i "s/max_input_time =.*/max_input_time = 3600/" /etc/php/8.2/apache2/php.ini
sed -i "s/post_max_size =.*/post_max_size = 10G/" /etc/php/8.2/apache2/php.ini
sed -i "s/upload_max_filesize =.*/upload_max_filesize = 10G/" /etc/php/8.2/apache2/php.ini
sed -i "s/;date.timezone.*/date.timezone = Europe\/\Berlin/" /etc/php/8.2/apache2/php.ini
sed -i "s/;cgi.fix_pathinfo.*/cgi.fix_pathinfo=0/" /etc/php/8.2/apache2/php.ini
sed -i "s/;session.cookie_secure.*/session.cookie_secure = True/" /etc/php/8.2/apache2/php.ini
sed -i "s/;opcache.enable=.*/opcache.enable=1/" /etc/php/8.2/apache2/php.ini
sed -i "s/;opcache.validate_timestamps=.*/opcache.validate_timestamps=0/" /etc/php/8.2/apache2/php.ini
sed -i "s/;opcache.enable_cli=.*/opcache.enable_cli=1/" /etc/php/8.2/apache2/php.ini
sed -i "s/;opcache.memory_consumption=.*/opcache.memory_consumption=256/" /etc/php/8.2/apache2/php.ini
sed -i "s/;opcache.interned_strings_buffer=.*/opcache.interned_strings_buffer=64/" /etc/php/8.2/apache2/php.ini
sed -i "s/;opcache.max_accelerated_files=.*/opcache.max_accelerated_files=100000/" /etc/php/8.2/apache2/php.ini
sed -i "s/;opcache.revalidate_freq=.*/opcache.revalidate_freq=60/" /etc/php/8.2/apache2/php.ini
sed -i "s/;opcache.save_comments=.*/opcache.save_comments=1/" /etc/php/8.2/apache2/php.ini
sed -i "s/max_file_uploads =.*/max_file_uploads = 20/" /etc/php/8.2/apache2/php.ini
sed -i '$aopcache.jit=1255' /etc/php/8.2/apache2/php.ini
sed -i '$aopcache.jit_buffer_size=256M' /etc/php/8.2/apache2/php.ini
sed -i '$aapc.enable_cli=1' /etc/php/8.2/apache2/php.ini
sed -i '$aapc.enable_cli=1' /etc/php/8.2/mods-available/apcu.ini
sed -i '$aopcache.jit=1255' /etc/php/8.2/mods-available/opcache.ini
sed -i '$aopcache.jit_buffer_size=256M' /etc/php/8.2/mods-available/opcache.ini
sed -i '$a[mysql]' /etc/php/8.2/mods-available/mysqli.ini
sed -i '$amysql.allow_local_infile=On' /etc/php/8.2/mods-available/mysqli.ini
sed -i '$amysql.allow_persistent=On' /etc/php/8.2/mods-available/mysqli.ini
sed -i '$amysql.cache_size=2000' /etc/php/8.2/mods-available/mysqli.ini
sed -i '$amysql.max_persistent=-1' /etc/php/8.2/mods-available/mysqli.ini
sed -i '$amysql.max_links=-1' /etc/php/8.2/mods-available/mysqli.ini
sed -i '$amysql.default_port=3306' /etc/php/8.2/mods-available/mysqli.ini
sed -i '$amysql.connect_timeout=60' /etc/php/8.2/mods-available/mysqli.ini
sed -i '$amysql.trace_mode=Off' /etc/php/8.2/mods-available/mysqli.ini
a2ensite nc.conf
if [[ "$systemos" = 'debian' ]]; then
systemctl start apache2.service
fi
### DB part
mv /etc/mysql/my.cnf /etc/mysql/my.cnf.bak
echo "
[mysqld]
bind-address = 127.0.0.1
port = $dbport
slow_query_log_file = /var/log/mysql/mariadb-slow.log
long_query_time = 10
log_slow_rate_limit = 1000
log_slow_verbosity = query_plan
log-queries-not-using-indexes
" > /etc/mysql/my.cnf
echo "--------------------------------------------------------------------------------------------------------"
echo "--------------------------------------------------------------------------------------------------------"
echo " Your database server will now be hardened - just follow the instructions."
echo " Keep in mind: your MariaDB root password is still NOT set !"
echo -e "${YELLOW} You should set a root password, when asked${ENDCOLOR}"
echo "--------------------------------------------------------------------------------------------------------"
echo "--------------------------------------------------------------------------------------------------------"
mysql_secure_installation
mysql -uroot <<EOF
CREATE DATABASE $databasename CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
CREATE USER '$databaseuser'@'localhost' identified by '$databaseuserpasswd';
GRANT ALL PRIVILEGES on $databasename.* to '$databaseuser'@'localhost' identified by '$databaseuserpasswd';
FLUSH privileges;
EOF
if [[ "$systemos" = 'debian' ]]; then
systemctl restart mariadb.service
fi
(crontab -l ; echo "*/5 * * * * sudo -u www-data php -f /var/www/nextcloud/cron.php") | sort - | uniq - | crontab -
cat <<EOF >> /var/www/nextcloud/config/myextra.config.php
<?php
\$CONFIG = array (
'memcache.local' => '\OC\Memcache\APCu',
'memcache.locking' => '\OC\Memcache\Memcached',
'default_phone_region' => '$dpr',
'skeletondirectory' => '',
);
EOF
echo ""
echo ""
echo ""
echo ""
echo "--------------------------------------------------------------------------------------------------------"
echo "--------------------------------------------------------------------------------------------------------"
echo -e "${GREEN}Wait please, nextcloud occ setup is in progress....${ENDCOLOR}"
echo "--------------------------------------------------------------------------------------------------------"
echo "--------------------------------------------------------------------------------------------------------"
cd /var/www/nextcloud
sudo -u www-data php occ maintenance:install --database "mysql" --database-name "$databasename" --database-user "$databaseuser" --database-pass "$databaseuserpasswd" --database-host "localhost:$dbport" --admin-user "$nextroot" --admin-pass "$nextpass" --data-dir "$ncdatafolder"
sudo -u www-data php occ config:system:set logtimezone --value="$ltz"
sudo -u www-data php occ config:system:set trusted_domains 1 --value=10.$ipv4network.1
sudo -u www-data php occ app:enable encryption
sudo -u www-data php occ encryption:enable
sudo -u www-data php occ encryption:encrypt-all
#sudo -u www-data php occ encryption:enable-master-key
# 2023.08 E2EE really not working as it should??? sudo -u www-data php occ app:enable end_to_end_encryption
sudo -u www-data php occ config:system:set maintenance_window_start --type=integer --value=1
sudo -u www-data php occ maintenance:repair --include-expensive
sudo -u www-data php occ db:add-missing-indices
sudo -u www-data php occ background:cron
if [[ "$systemos" = 'debian' ]]; then
systemctl start apache2.service
fi
echo "--------------------------------------------------------------------------------------------------------"
echo " E2EE end 2 end encryption is not working like usual without, functions too limited .......2023.08 "
echo " Used serverside encryption for now, less secure but better than nothing ..... "
echo " A cloud VPS server is not really your host, its just someone else system,storage,and so on ......"
echo "--------------------------------------------------------------------------------------------------------"
echo ""
echo ""
echo -e "${GREEN} Your settings, and passwords, maybe take a copy ..... ${ENDCOLOR}"
echo ""
echo ""
echo "--------------------------------------------------------------------------------------------------------"
echo " Your apache https port : $httpsport"
echo "--------------------------------------------------------------------------------------------------------"
echo " Your mariaDB port : $dbport"
echo "--------------------------------------------------------------------------------------------------------"
echo " sql databasename : $databasename"
echo "--------------------------------------------------------------------------------------------------------"
echo " sql databaseuser : $databaseuser"
echo "--------------------------------------------------------------------------------------------------------"
echo " sql databaseuserpasswd : $databaseuserpasswd"
echo "--------------------------------------------------------------------------------------------------------"
echo " Your nextcloud data folder : $ncdatafolder"
echo "--------------------------------------------------------------------------------------------------------"
echo " Your nextcloud admin user : $nextroot"
echo "--------------------------------------------------------------------------------------------------------"
echo " Your nextcloud login password : $nextpass"
echo "--------------------------------------------------------------------------------------------------------"
echo " Now setup Nextcloud to your needs : https://10.$ipv4network.1:$httpsport"
echo "--------------------------------------------------------------------------------------------------------"
echo ""
echo ""