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