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