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