-
Notifications
You must be signed in to change notification settings - Fork 63
/
owncloudpie_setup.sh
executable file
·470 lines (390 loc) · 16.7 KB
/
owncloudpie_setup.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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
#!/bin/bash
# OwncloudPie - Shell script for installing and updating Owncloud on the Raspberry Pi.
#
# (c) Copyright 2012 Florian Müller ([email protected])
#
# OwncloudPie homepage: https://github.com/petrockblog/OwncloudPie
#
# Permission to use, copy, modify and distribute OwncloudPie in both binary and
# source form, for non-commercial purposes, is hereby granted without fee,
# providing that this license information and copyright notice appear with
# all copies and any derived work.
#
# This software is provided 'as-is', without any express or implied
# warranty. In no event shall the authors be held liable for any damages
# arising from the use of this software.
#
# OwncloudPie is freeware for PERSONAL USE only. Commercial users should
# seek permission of the copyright holders first. Commercial use includes
# charging money for OwncloudPie or software derived from OwncloudPie.
#
# The copyright holders request that bug fixes and improvements to the code
# should be forwarded to them so everyone can benefit from the modifications
# in future versions.
#
# Raspberry Pi is a trademark of the Raspberry Pi Foundation.
#
function printMsg()
{
echo -e "\n= = = = = = = = = = = = = = = = = = = = =\n$1\n= = = = = = = = = = = = = = = = = = = = =\n"
}
# arg 1: key, arg 2: value, arg 3: file
function ensureKeyValue()
{
if [[ -z $(egrep -i ";? *$1 = [0-9]*[M]?" $3) ]]; then
# add key-value pair
echo "$1 = $2" >> $3
else
# replace existing key-value pair
toreplace=`egrep -i ";? *$1 = [0-9]*[M]?" $3`
sed $3 -i -e "s|$toreplace|$1 = $2|g"
fi
}
# arg 1: key, arg 2: value, arg 3: file
# make sure that a key-value pair is set in file
# key=value
function ensureKeyValueShort()
{
if [[ -z $(egrep -i "#? *$1\s?=\s?""?[+|-]?[0-9]*[a-z]*"""? $3) ]]; then
# add key-value pair
echo "$1=""$2""" >> $3
else
# replace existing key-value pair
toreplace=`egrep -i "#? *$1\s?=\s?""?[+|-]?[0-9]*[a-z]*"""? $3`
sed $3 -i -e "s|$toreplace|$1=""$2""|g"
fi
}
function checkNeededPackages()
{
doexit=0
type -P git &>/dev/null && echo "Found git command." || { echo "Did not find git. Try 'sudo apt-get install -y git' first."; doexit=1; }
type -P dialog &>/dev/null && echo "Found dialog command." || { echo "Did not find dialog. Try 'sudo apt-get install -y dialog' first."; doexit=1; }
if [[ doexit -eq 1 ]]; then
exit 1
fi
}
function downloadLatestOwncloudRelease()
{
clear
if [[ ! -d /var/www/html/owncloud ]]; then
mkdir -p /var/www/html/owncloud
chown -R www-data:www-data /var/www
fi
printMsg "Updating to latest Owncloud release."
# download and extract the latest release of Owncloud
latestrelease="http://download.owncloud.org/community/owncloud-latest.tar.bz2"
wget "$latestrelease"
if [[ $? -gt 0 ]]; then
wget -O changelog http://owncloud.org/changelog/
latestrelease=$(cat changelog | grep "Download:" | head -n 2 | tail -n 1 | grep -o '<a href=['"'"'"][^"'"'"']*['"'"'"]' | sed -e 's/^<a href=["'"'"']//' -e 's/["'"'"']$//')
wget "$latestrelease"
rm changelog
fi
tar -xjf "$(basename $latestrelease)"
rm "$(basename $latestrelease)"
}
function writeServerConfig()
{
cat > /etc/nginx/sites-available/default << _EOF_
# owncloud
server {
listen 80;
server_name $__servername;
return 301 https://\$server_name\$request_uri; # enforce https
}
# owncloud (ssl/tls)
server {
listen 443 ssl;
server_name $__servername;
ssl_certificate /etc/nginx/cert.pem;
ssl_certificate_key /etc/nginx/cert.key;
root /var/www/html;
index index.php;
client_max_body_size 4000M; # set maximum upload size
fastcgi_buffers 64 4K;
add_header Strict-Transport-Security "max-age=15552000; includeSubDomains";
# deny direct access
location ~ ^/owncloud/(data|config|\.ht|db_structure\.xml|README) {
deny all;
}
# default try order
location / {
try_files \$uri \$uri/ index.php;
}
# owncloud WebDAV
location @webdav {
fastcgi_split_path_info ^(.+\.php)(/.*)$;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME \$document_root\$fastcgi_script_name;
fastcgi_param HTTPS on;
include fastcgi_params;
}
# enable php
location ~ ^(?<script_name>.+?\.php)(?<path_info>/.*)?$ {
try_files \$script_name = 404;
include fastcgi_params;
fastcgi_param PATH_INFO \$path_info;
fastcgi_param HTTPS on;
fastcgi_pass 127.0.0.1:9000;
fastcgi_read_timeout 900s; # 15 minutes
fastcgi_param SCRIPT_FILENAME \$document_root\$fastcgi_script_name;
}
}
_EOF_
}
function main_setservername()
{
cmd=(dialog --backtitle "PetRockBlock.com - OwncloudPie Setup." --inputbox "Please enter the URL of your Owncloud server." 22 76 $__servername)
choices=$("${cmd[@]}" 2>&1 >/dev/tty)
if [ "$choices" != "" ]; then
__servername=$choices
if [[ -f /etc/nginx/sites-available/default ]]; then
sed /etc/nginx/sites-available/default -r -e "s|server_name .*|server_name $__servername;|g"
fi
else
break
fi
}
function installCertificateNginx()
{
dialog --backtitle "PetRockBlock.com - OwncloudPie Setup." --msgbox "We are now going to create a self-signed certificate. While you could simply press ENTER when you are asked for country name etc. or enter whatever you want, it might be beneficial to have the web servers host name in the common name field of the certificate." 20 60
openssl req $@ -new -x509 -days 365 -nodes -out /etc/nginx/cert.pem -keyout /etc/nginx/cert.key
chmod 600 /etc/nginx/cert.pem
chmod 600 /etc/nginx/cert.key
}
function main_newinstall_nginx()
{
clear
# make sure we use the newest packages
apt-get update
apt-get upgrade -y
locale-gen en_US.UTF-8
# make sure that the group www-data exists
groupadd www-data
usermod -a -G www-data www-data
# install all needed packages, e.g., Nginx, PHP, SQLite, MariaDB
apt-get install -y nginx sendmail sendmail-bin openssl ssl-cert php5-cli php5-sqlite php5-gd php5-curl \
php5-common php5-cgi sqlite php-pear php-apc git-core \
autoconf automake autotools-dev curl libapr1 libtool curl libcurl4-openssl-dev \
php-xml-parser php5 php5-dev php5-gd php5-fpm php5-mysql memcached php5-memcache varnish dphys-swapfile bzip2 mariadb-server
apt-get autoremove -y
# set memory split to 240 MB RAM and 16 MB video
ensureKeyValueShort "gpu_mem" "16" "/boot/config.txt"
# generate self-signed certificate that is valid for one year
installCertificateNginx
writeServerConfig
sed /etc/php5/fpm/pool.d/www.conf -i -e "s|listen = /var/run/php5-fpm.sock|listen = 127.0.0.1:9000|g"
sed /etc/php5/fpm/pool.d/www.conf -i -e "s|\;env\[PATH\]|env\[PATH\]|g"
ensureKeyValue "upload_max_filesize" "2000M" "/etc/php5/fpm/php.ini"
ensureKeyValue "post_max_size" "2000M" "/etc/php5/fpm/php.ini"
ensureKeyValue "default_charset" "UTF-8" "/etc/php5/fpm/php.ini"
ensureKeyValue "upload_tmp_dir" "/srv/http/owncloud/data" "/etc/php5/fpm/php.ini"
mkdir -p /srv/http/owncloud/data
chown www-data:www-data /srv/http/owncloud/data
sed /etc/nginx/sites-available/default -i -e "s|client_max_body_size [0-9]*[M]?;|client_max_body_size 2000M;|g"
/etc/init.d/php5-fpm restart
/etc/init.d/nginx restart
# set ARM frequency to 800 MHz (or use the raspi-config tool to set clock speed)
ensureKeyValueShort "arm_freq" "800" "/boot/config.txt"
ensureKeyValueShort "sdram_freq" "400" "/boot/config.txt"
ensureKeyValueShort "core_freq" "250" "/boot/config.txt"
# resize swap file to 512 MB
ensureKeyValueShort "CONF_SWAPSIZE" "512" "/etc/dphys-swapfile"
dphys-swapfile setup
dphys-swapfile swapon
mkdir -p /var/www/html/owncloud
downloadLatestOwncloudRelease
mv owncloud/ /var/www/html/
# change group and owner of all /var/www files recursively to www-data
chown -R www-data:www-data /var/www
# enable US UTF-8 locale
sudo sed -i -e "s/# en_US.UTF-8 UTF-8/en_US.UTF-8 UTF-8/g" /etc/locale.gen
# finish the script
myipaddress=$(hostname -I | tr -d ' ')
dialog --backtitle "PetRockBlock.com - OwncloudPie Setup." --msgbox "If everything went right, Owncloud should now be available at the URL https://$myipaddress/owncloud. You have to finish the setup by visiting that site." 20 60
}
function installCertificateApache()
{
dialog --backtitle "PetRockBlock.com - OwncloudPie Setup." --msgbox "We are now going to create a self-signed certificate. While you could simply press ENTER when you are asked for country name etc. or enter whatever you want, it might be beneficial to have the web servers host name in the common name field of the certificate." 20 60
clear
openssl req $@ -new -x509 -days 365 -nodes -out /etc/apache2/apache.pem -keyout /etc/apache2/apache.key
chmod 600 /etc/apache2/apache.pem
chmod 600 /etc/apache2/apache.key
}
function main_newinstall_apache()
{
clear
# make sure we use the newest packages
apt-get update
apt-get upgrade -y
locale-gen en_US.UTF-8
# make sure that the group www-data exists
groupadd www-data
usermod -a -G www-data www-data
# install all needed packages, e.g., Apache, PHP, SQLite
apt-get install -y apache2 openssl sendmail sendmail-bin ssl-cert libapache2-mod-php5 php5-cli php5-sqlite php5-mysql php5-gd php5-curl php5-common php5-cgi sqlite php-pear php-apc git-core ca-certificates dphys-swapfile bzip2 mariadb-server
# Change RAM settings 16 MB video RAM
ensureKeyValueShort "gpu_mem" "16" "/boot/config.txt"
# generate self-signed certificate that is valid for one year
installCertificateApache
# enable Apache modules (as explained at http://owncloud.org/support/install/, Section 2.3)
a2enmod ssl
a2enmod rewrite
a2enmod headers
# disable unneccessary (for Owncloud) module(s)
a2dismod cgi
a2dismod authz_groupfile
# configure Apache to use self-signed certificate
mv /etc/apache2/sites-available/default-ssl /etc/apache2/sites-available/default-ssl.bak
sed 's|/etc/ssl/certs/ssl-cert-snakeoil.pem|/etc/apache2/apache.pem|g' /etc/apache2/sites-available/default-ssl.bak > tmp
sed -i 's|AllowOverride None|AllowOverride All|g' tmp
sed -i 's|/etc/ssl/private/ssl-cert-snakeoil.key|/etc/apache2/apache.key|g' tmp
mv tmp /etc/apache2/sites-available/default-ssl
# limit number of parallel Apache processes
mv /etc/apache2/apache2.conf /etc/apache2/apache2.conf.bak
sed 's|StartServers 5|StartServers 2|g;s|MinSpareServers 5|MinSpareServers 2|g;s|MaxSpareServers 10|MaxSpareServers 3|g' /etc/apache2/apache2.conf.bak > tmp
mv tmp /etc/apache2/apache2.conf
# set ARM frequency to 800 MHz (attention: should be safe, but do this at your own risk)
ensureKeyValueShort "arm_freq" "800" "/boot/config.txt"
ensureKeyValueShort "sdram_freq" "450" "/boot/config.txt"
ensureKeyValueShort "core_freq" "350" "/boot/config.txt"
# resize swap file to 512 MB
ensureKeyValueShort "CONF_SWAPSIZE" "512" "/etc/dphys-swapfile"
dphys-swapfile setup
dphys-swapfile swapon
# enable SSL site
a2ensite default-ssl
mkdir -p /var/www/html/owncloud
downloadLatestOwncloudRelease
cp -r owncloud/* /var/www/html/owncloud/
rm -rf owncloud
# change group and owner of all /var/www files recursively to www-data
chown -R www-data:www-data /var/www
# restart Apache service
/etc/init.d/apache2 reload
# enable US UTF-8 locale
sudo sed -i -e "s/# en_US.UTF-8 UTF-8/en_US.UTF-8 UTF-8/g" /etc/locale.gen
# finish the script
myipaddress=$(hostname -I | tr -d ' ')
dialog --backtitle "PetRockBlock.com - OwncloudPie Setup." --msgbox "If everything went right, Owncloud should now be available at the URL https://$myipaddress/owncloud. You have to finish the setup by visiting that site. Before that, we are going to reboot the Raspberry." 20 60
reboot
}
function create_db()
{
__rootpass="root"
__dbuser="ocuser"
__dbpass="ocpass"
cmd=(dialog --backtitle "PetRockBlock.com - OwncloudPie Setup." --inputbox "Please enter the password of the root db user you selected earlier" 22 76 $__rootpass)
choices=$("${cmd[@]}" 2>&1 >/dev/tty)
if [ "$choices" != "" ]; then
__rootpass=$choices
fi
cmd=(dialog --backtitle "PetRockBlock.com - OwncloudPie Setup." --inputbox "Please enter the username of the Owncloud db user." 22 76 $__dbuser)
choices=$("${cmd[@]}" 2>&1 >/dev/tty)
if [ "$choices" != "" ]; then
__dbuser=$choices
fi
cmd=(dialog --backtitle "PetRockBlock.com - OwncloudPie Setup." --inputbox "Please enter the password of the Owncloud db user." 22 76 $__dbpass)
choices=$("${cmd[@]}" 2>&1 >/dev/tty)
if [ "$choices" != "" ]; then
__dbpass=$choices
fi
mysql -u root -p"$__rootpass" -e "DROP DATABASE ocdb;"
mysql -u root -p"$__rootpass" -e "CREATE DATABASE ocdb CHARACTER SET utf8 COLLATE utf8_general_ci;"
mysql -u root -p"$__rootpass" -e "GRANT ALL PRIVILEGES ON ocdb.* TO $__dbuser@localhost IDENTIFIED BY '$__dbpass';"
mysql -u root -p"$__rootpass" -e "FLUSH PRIVILEGES;"
}
function mount_external()
{
apt-get install -y ntfs-3g
apt-get autoremove -y
mkdir /media/owncloud
cat /etc/fstab > temp_stab
uuid=$(blkid /dev/sda1 | grep -Po 'UUID="\K.*?(?=")')
uid=$(id -u www-data)
gid=$(id -g www-data)
echo "UUID=$uuid /media/owncloud auto nofail,uid=$uid,gid=$gid,umask=0027,dmask=0027,noatime 0 0" >> temp_stab
mv temp_stab /etc/fstab
dialog --backtitle "PetRockBlock.com - OwncloudPie Setup." --msgbox "If everything went right, drive in /dev/sda1 is mounted to /media/owncloud. For the changes to be applied, we are going to reboot the Raspberry." 20 60
reboot
}
function main_update()
{
downloadLatestOwncloudRelease
cp -r owncloud/* /var/www/html/owncloud/
rm -rf owncloud
chown -R www-data:www-data /var/www
dialog --backtitle "PetRockBlock.com - OwncloudPie Setup." --msgbox "Finished upgrading Owncloud instance." 20 60
}
function main_updatescript()
{
scriptdir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
pushd $scriptdir
if [[ ! -d .git ]]; then
dialog --backtitle "PetRockBlock.com - OwncloudPie Setup." --msgbox "Cannot find direcotry '.git'. Please clone the OwncloudPie script via 'git clone git://github.com/petrockblog/OwncloudPie.git'" 20 60
popd
return
fi
git pull
popd
dialog --backtitle "PetRockBlock.com - OwncloudPie Setup." --msgbox "Fetched the latest version of the OwncloudPie script. You need to restart the script." 20 60
}
function main_uninstall()
{
dialog --title " Uninstall OwncloudPie " --clear \
--yesno "Do you really want to UNINSTALL OwncloudPie?" 22 76
case $? in
0)
apt-get remove -y nginx sendmail sendmail-bin openssl ssl-cert php5-cli php5-sqlite php5-gd php5-curl \
apache2 php5-common php5-cgi sqlite php-pear php-apc git-core \
autoconf automake autotools-dev curl libapr1 libtool curl libcurl4-openssl-dev \
php-xml-parser php5 php5-dev php5-gd php5-fpm php5-mysql memcached php5-memcache varnish dphys-swapfile bzip2 mariadb-server ntfs-3g
apt-get -y autoremove
;;
*)
;;
esac
}
# here starts the main script
checkNeededPackages
if [[ -f /etc/nginx/sites-available/default ]]; then
__servername=$(egrep -m 1 "server_name " /etc/nginx/sites-available/default | sed "s| ||g")
__servername=${__servername:11:0-1}
else
__servername="url.ofmyserver.com"
fi
if [ $(id -u) -ne 0 ]; then
printf "Script must be run as root. Try 'sudo ./OwncloudPie_setup'\n"
exit 1
fi
while true; do
cmd=(dialog --backtitle "PetRockBlock.com - OwncloudPie Setup." --menu "You MUST set the server URL (e.g., 192.168.0.10 or myaddress.dyndns.org) before starting one of the installation routines. Choose task:" 22 76 16)
options=(1 "Set server URL ($__servername)"
2 "New installation, NGiNX based"
3 "Generate new SSL certificate for NGiNX"
4 "New installation, Apache based"
5 "Generate new SSL certificate for Apache"
6 "Mount external drive (in /dev/sda1)"
7 "Create MySQL database for OwnCloud"
8 "Update existing Owncloud installation"
9 "Update OwncloudPie script"
10 "Uninstall OwncloudPie")
choice=$("${cmd[@]}" "${options[@]}" 2>&1 >/dev/tty)
if [ "$choice" != "" ]; then
case $choice in
1) main_setservername ;;
2) main_newinstall_nginx ;;
3) installCertificateNginx ;;
4) main_newinstall_apache ;;
5) installCertificateApache ;;
6) mount_external ;;
7) create_db ;;
8) main_update ;;
9) main_updatescript ;;
10) main_uninstall ;;
esac
else
break
fi
done
clear