3026af75d46586ae31951db02a9a75d6dadbe767
[distro-setup] / nextcloud-setup
1 #!/bin/bash
2
3 set -e; . /usr/local/lib/bash-bear; set +e
4
5
6 pre="${0##*/}:"
7 m() { printf "$pre %s\n" "$*"; "$@"; }
8 e() { printf "$pre %s\n" "$*"; }
9 err() { printf "$pre %s\n" "$*" >&2; exit 1; }
10 i() { # install file
11 local tmp tmpdir dest="$1"
12 local base="${dest##*/}"
13 local dir="${dest%/*}"
14 if [[ $dir != "$base" ]]; then
15 # dest has a directory component
16 mkdir -p "$dir"
17 fi
18 ir=false # i result
19 tmpdir=$(mktemp -d)
20 cat >$tmpdir/"$base"
21 tmp=$(rsync -ic $tmpdir/"$base" "$dest")
22 if [[ $tmp ]]; then
23 printf "%s\n" "$tmp"
24 # shellcheck disable=SC2034
25 ir=true
26 if [[ $dest == /etc/systemd/system/* ]]; then
27 touch /var/local/mail-setup-reload
28 reload=true
29 fi
30 fi
31 rm -rf $tmpdir
32 }
33 setini() {
34 key="$1" value="$2" section="$3"
35 file="/etc/radicale/config"
36 sed -ri "/ *\[$section\]/,/^ *\[[^]]+\]/{/^\s*${key}[[:space:]=]/d};/ *\[$section\]/a $key = $value" "$file"
37 }
38 soff () {
39 for service; do
40 # ignore services that dont exist
41 if systemctl cat $service &>/dev/null; then
42 m systemctl disable --now $service
43 fi
44 done
45 }
46 sre() {
47 for service; do
48 m systemctl restart $service
49 m systemctl enable $service;
50 done
51 }
52
53
54 ncdir=/var/www/ncfsf
55 myncdir=/root/ncfsf
56 ncbase=${ncdir##*/}
57 mkdir $myncdir
58 domain=boardfiles.fsf.org
59
60 apt-get -y install php-zip apache2 php-fpm
61
62 fpm=$(dpkg-query -s php-fpm | sed -nr 's/^Depends:.* (php[^ ]*-fpm)( .*|$)/\1/p') # eg: php7.4-fpm
63 phpver=$(dpkg-query -s php-fpm | sed -nr 's/^Depends:.* php([^ ]*)-fpm( .*|$)/\1/p')
64 m a2enconf $fpm
65 # 3 useless guides on php fpm fcgi debian 10 later, i figure out from reading
66 # /etc/apache2/conf-enabled/php7.3-fpm.conf
67 # However, on t11,
68 # ERROR: Module php8.1 does not exist. just allow it to fail
69 m a2dismod php$phpver ||:
70
71 # php with fpm doesnt work without this
72 m a2enmod proxy_fcgi
73
74
75
76 m web-conf - apache2 $domain <<EOF
77 ### begin nextcloud settings
78 Alias /nextcloud "$ncdir/"
79 <Directory $ncdir/>
80 Require all granted
81 AllowOverride All
82 Options FollowSymLinks MultiViews
83
84 <IfModule mod_dav.c>
85 Dav off
86 </IfModule>
87
88 </Directory>
89
90 # based on install checker, links to
91 # https://docs.nextcloud.com/server/19/admin_manual/issues/general_troubleshooting.html#service-discovery
92 # their example was a bit wrong, I figured it out by adding
93 # LogLevel warn rewrite:trace5
94 # then watching the apache logs
95
96 RewriteEngine on
97 RewriteRule ^/\.well-known/host-meta /nextcloud/public.php?service=host-meta [QSA,L]
98 RewriteRule ^/\.well-known/host-meta\.json /nextcloud/public.php?service=host-meta-json [QSA,L]
99 RewriteRule ^/\.well-known/webfinger /nextcloud/public.php?service=webfinger [QSA,L]
100 ### end nextcloud settings
101 EOF
102
103
104 i /etc/php/$phpver/cli/conf.d/30-local.ini <<'EOF'
105 apc.enable_cli = 1
106 EOF
107
108
109 i /etc/php/$phpver/fpm/conf.d/30-local.ini <<'EOF'
110 date.timezone = "America/New_York"
111 # for nextcloud
112 upload_max_filesize = 2000M
113 post_max_size = 2000M
114 # install checker, nextcloud/settings/admin/overview
115 memory_limit = 512M
116 EOF
117
118 m systemctl restart $fpm
119
120 # some of these are based on errors later on.
121 m apt-get -y install php-curl php-bz2 php-gmp php-bcmath php-imagick php-apcu php-mbstring php-xml php-gd sqlite3 php-sqlite3
122
123 # https://docs.nextcloud.com/server/19/admin_manual/installation/source_installation.html
124 cat >/etc/php/$phpver/fpm/pool.d/localwww.conf <<'EOF'
125 [www]
126 clear_env = no
127 EOF
128
129
130
131 nextcloud_admin_pass=casHiosidZyFraycs
132
133 m cd /var/www
134 if [[ ! -e $ncdir/index.php ]]; then
135 # if we wanted to only install a specific version, use something like
136 # file=latest-22.zip
137 file=latest.zip
138 m wget -nv -N https://download.nextcloud.com/server/releases/$file
139 m rm -rf nextcloud
140 m unzip -q $file
141 m rm -f $file
142 m chown -R www-data.www-data nextcloud
143 m mv nextcloud $ncdir
144 fi
145
146 if [[ ! -e $myncdir/done-install ]]; then
147 m cd $ncdir
148 m sudo -u www-data php occ maintenance:install --database sqlite --admin-user iank --admin-pass $nextcloud_admin_pass
149 m touch $myncdir/done-install
150 fi
151
152 # note, strange this happend where updater did not increment the version var,
153 # mine was stuck on 20. I manually updated it.
154 m cd $ncdir/config
155 if [[ ! -e $myncdir/config.php-orig ]]; then
156 m cp -a config.php $myncdir/config.php-orig
157 fi
158 cat $myncdir/config.php-orig - >$myncdir/tmp.php <<EOF
159 # https://docs.nextcloud.com/server/19/admin_manual/configuration_server/email_configuration.html
160 \$CONFIG["mail_smtpmode"] = "sendmail";
161 \$CONFIG["mail_smtphost"] = "127.0.0.1";
162 \$CONFIG["mail_smtpport"] = 25;
163 \$CONFIG["mail_smtptimeout"] = 10;
164 \$CONFIG["mail_smtpsecure"] = "";
165 \$CONFIG["mail_smtpauth"] = false;
166 \$CONFIG["mail_smtpauthtype"] = "LOGIN";
167 \$CONFIG["mail_smtpname"] = "";
168 \$CONFIG["mail_smtppassword"] = "";
169 \$CONFIG["mail_domain"] = "$domain";
170
171
172 # based on installer check
173 # https://docs.nextcloud.com/server/19/admin_manual/configuration_server/caching_configuration.html
174 \$CONFIG['memcache.local'] = '\OC\Memcache\APCu';
175
176 \$CONFIG['overwrite.cli.url'] = 'https://$domain/nextcloud';
177 \$CONFIG['htaccess.RewriteBase'] = '/nextcloud';
178 \$CONFIG['trusted_domains'] = array (
179 0 => '$domain',
180 );
181 #\$CONFIG[''] = '';
182 fwrite(STDOUT, "<?php\n\\\$CONFIG = ");
183 var_export(\$CONFIG);
184 fwrite(STDOUT, ";\n");
185 EOF
186 e running php $myncdir/tmp.php
187 # note: we leave it around place for debugging
188 php $myncdir/tmp.php >config.php
189 cd $ncdir
190 m sudo -u www-data php occ maintenance:update:htaccess
191 i /etc/systemd/system/$ncbase.service <<EOF
192 [Unit]
193 Description=ncup $ncbase
194 After=multi-user.target
195
196 [Service]
197 Type=oneshot
198 ExecStart=/usr/local/bin/ncup $ncbase
199 User=www-data
200 IOSchedulingClass=idle
201 CPUSchedulingPolicy=idle
202 EOF
203 i /etc/systemd/system/$ncbase.timer <<EOF
204 [Unit]
205 Description=ncup $ncbase timer
206
207 [Timer]
208 OnCalendar=Daily
209
210 [Install]
211 WantedBy=timers.target
212 EOF
213 systemctl enable --now $ncbase.timer
214 i /usr/local/bin/ncup <<'EOFOUTER'
215 #!/bin/bash
216
217 set -e; . /usr/local/lib/bash-bear; set +e
218
219 m() { printf "%s\n" "$*"; "$@"; }
220 err-cleanup() {
221 echo failed nextcloud update for $ncbase >&2
222 # -odf or else systemd will kill the background delivery process
223 # and the message will sit in the queue until the next queue run.
224 exim -odf -t <<EOF
225 To: alerts@iankelling.org
226 From: www-data@$(hostname -f)
227 Subject: failed nextcloud update for $ncbase
228
229 For logs, run: jr -u $ncbase
230 EOF
231 }
232
233 if [[ $(id -u -n) != www-data ]]; then
234 echo error: running as wrong user: $(id -u -n), expected www-data
235 exit 1
236 fi
237
238 if [[ ! $1 ]]; then
239 echo error: expected an arg, nextcloud relative base dir
240 exit 1
241 fi
242
243 ncbase=$1
244 cd /var/www/$ncbase
245 # https://docs.nextcloud.com/server/22/admin_manual/maintenance/update.html?highlight=updater+phar
246 m php /var/www/$ncbase/updater/updater.phar -n
247 EOFOUTER
248 chmod +x /usr/local/bin/ncup
249
250 mkdir -p /var/www/cron-errors
251 chown www-data.www-data /var/www/cron-errors
252 i /etc/cron.d/$ncbase <<EOF
253 PATH=/usr/sbin:/sbin:/usr/bin:/bin:/usr/local/bin
254 SHELL=/bin/bash
255 # https://docs.nextcloud.com/server/20/admin_manual/configuration_server/background_jobs_configuration.html
256 */5 * * * * www-data php -f $ncdir/cron.php --define apc.enable_cli=1 |& log-once nccron
257 EOF
258
259 if $reload; then
260 m systemctl daemon-reload
261 fi