From 743f12f91b18e330a14423f45b69cad05f451c1a Mon Sep 17 00:00:00 2001 From: nachoparker Date: Sun, 12 Feb 2017 20:12:48 +0100 Subject: [PATCH] First commit, Raspbian 8 with Nextcloud 11 --- README.md | 26 ++++ install-image.sh | 73 +++++++++++ install-nextcloud.sh | 285 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 384 insertions(+) create mode 100755 install-image.sh create mode 100755 install-nextcloud.sh diff --git a/README.md b/README.md index 14f1b53c..1f20e978 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,28 @@ # nextcloud-raspbian-generator Automatically generate Raspbian Images with Nextcloud installed and configured using QEMU + +## Features + +* Raspbian 8 Jessie +* Nextcloud 11.0.1 +* Apache 2.4.25, with HTTP2 enabled +* PHP 7.0 +* MariaDB 10 +* Automatic redirection to HTTPS +* ACPU PHP cache +* PHP Zend OPcache enabled with file cache +* HSTS +* Cron jobs for Nextcloud +* Sane configuration defaults + +## Usage + +``` +git clone https://github.com/nachoparker/nextcloud-raspbian-generator.git +cd nextcloud-raspbian-generator +./install-image.sh 192.168.0.145 # change to your QEMU raspbian IP +``` + +See details and instructions at + +https://ownyourbits.com/2017/02/13/nextcloud-ready-raspberry-pi-image/ diff --git a/install-image.sh b/install-image.sh new file mode 100755 index 00000000..b1dd05ad --- /dev/null +++ b/install-image.sh @@ -0,0 +1,73 @@ +#!/bin/bash + +# Nextcloud installation on QEMU emulated Raspbian image +# Tested with 2017-01-11-raspbian-jessie.img (and lite) +# +# Copyleft 2017 by Ignacio Nunez Hernanz +# GPL licensed (see end of file) * Use at your own risk! +# +# Usage: +# ./install-image.sh # Use the IP of your running QEMU Raspbian image +# +# Notes: +# Set DOWNLOAD=0 if you have already downloaded an image. Rename it to nextcloudpi.img + +IP=$1 # First argument is the QEMU Raspbian IP address +DOWNLOAD=1 # Download the latest image +#IMG=raspbian_latest +IMG=raspbian_lite_latest + +[[ "$IP" == "" ]] && { echo "usage: ./install-image.sh "; exit; } + +SSH="ssh -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o ServerAliveInterval=5 -o ConnectTimeout=1 -o LogLevel=quiet" +IMGFILE="NextCloudPi_$( date "+%m-%d-%y" ).img" + +if [[ "$DOWNLOAD" == "1" ]]; then + wget https://downloads.raspberrypi.org/$IMG -O $IMG.zip && \ + unzip $IMG.zip && \ + mv *-raspbian-*.img $IMGFILE && \ + qemu-img resize $IMGFILE +1G +fi + +test -d qemu-raspbian-network || git clone https://github.com/nachoparker/qemu-raspbian-network.git +sed -i '30s/NO_NETWORK=1/NO_NETWORK=0/' qemu-raspbian-network/qemu-pi.sh + +NUM_REBOOTS=$( grep -c reboot install-nextcloud.sh ) +while [[ $NUM_REBOOTS != -1 ]]; do + echo "Starting QEMU" + cd qemu-raspbian-network + sudo ./qemu-pi.sh ../$IMGFILE & + cd - + + sleep 10 + echo "Waiting for SSH to be up" + while true; do + sshpass -praspberry $SSH pi@$IP ls &>/dev/null && break + sleep 1 + done + + sleep 90 + echo "Launching installation" + cat install-nextcloud.sh | sshpass -praspberry $SSH pi@$IP + wait + NUM_REBOOTS=$(( NUM_REBOOTS-1 )) +done +echo "$IMGFILE generated successfully" + +# License +# +# This script is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This script is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this script; if not, write to the +# Free Software Foundation, Inc., 59 Temple Place, Suite 330, +# Boston, MA 02111-1307 USA + diff --git a/install-nextcloud.sh b/install-nextcloud.sh new file mode 100755 index 00000000..0cd8e90b --- /dev/null +++ b/install-nextcloud.sh @@ -0,0 +1,285 @@ +#!/bin/bash + +# Nextcloud installation on Raspbian through SSH +# Tested with 2017-01-11-raspbian-jessie.img (and lite) +# +# Copyleft 2017 by Ignacio Nunez Hernanz +# GPL licensed (see end of file) * Use at your own risk! +# +# Usage: +# cat install-nextcloud.sh | sshpass -praspberry ssh pi@$IP +# +# Notes: +# Upon each necessary restart, the system will cut the SSH session, therefore +# it is required to save the state of the installation. See variable $STATE_FILE +# It will be necessary to invoke this a number of times for a complete installation + +sudo su + +VER=11.0.1 +ADMINUSER=admin +DBADMIN=ncadmin +DBPASSWD=ownyourbits +MAX_FILESIZE=1G +STATE_FILE=/home/pi/.installation_state + +set -x +set -e + +test -f $STATE_FILE && STATE=$( cat $STATE_FILE 2>/dev/null ) +if [ "$STATE" == "" ]; then + + # RESIZE IMAGE + ########################################## + + SECTOR=$( fdisk -l /dev/sda | grep Linux | awk '{ print $2 }' ) + set +e + echo -e "d\n2\nn\np\n2\n$SECTOR\n\nw\n" | fdisk /dev/sda + set -e + + echo 0 > $STATE_FILE + reboot +elif [ "$STATE" == "0" ]; then + + # UPDATE EVERYTHING + ########################################## + resize2fs /dev/sda2 + + apt-get update + apt-get upgrade -y + apt-get dist-upgrade -y + apt-get autoremove + + echo 1 > $STATE_FILE + reboot +elif [ "$STATE" == "1" ]; then + + # GET STRETCH SOURCES FOR HTTP2 AND PHP7 + ########################################## + + echo "deb http://mirrordirector.raspbian.org/raspbian/ stretch main contrib non-free rpi" >> /etc/apt/sources.list +cat > /etc/apt/preferences </etc/apache2/conf-available/http2.conf <> /etc/apache2/apache2.conf < + Header always set Strict-Transport-Security "max-age=15768000; includeSubDomains; preload" + +EOF + + cat > /etc/php/7.0/mods-available/apcu.ini < /etc/php/7.0/mods-available/opcache.ini < /etc/apache2/sites-available/nextcloud.conf < + Options +FollowSymlinks + AllowOverride All + + + Dav off + + + SetEnv HOME /var/www/nextcloud + SetEnv HTTP_HOME /var/www/nextcloud + +EOF + a2ensite nextcloud + + mysql -u root -p$DBPASSWD < '\''\\OC\\Memcache\\APCu'\'',\n);=' /var/www/nextcloud/config/config.php + + sed -i "s/post_max_size=.*/post_max_size=$MAX_FILESIZE/" /var/www/nextcloud/.user.ini + sed -i "s/post_max_size=.*/upload_max_filesize=$MAX_FILESIZE/" /var/www/nextcloud/.user.ini + sed -i "s/post_max_size=.*/post_max_size=$MAX_FILESIZE/" /var/www/nextcloud/.htaccess + sed -i "s/post_max_size=.*/upload_max_filesize=$MAX_FILESIZE/" /var/www/nextcloud/.htaccess + +cat >> /var/www/nextcloud/.htaccess < + RewriteEngine On + RewriteCond %{HTTPS} !=on + RewriteRule ^/?(.*) https://%{SERVER_NAME}/nextcloud/$1 [R,L] + +EOF + + echo "*/15 * * * * php -f /var/www/nextcloud/cron.php" > /tmp/crontab_http + crontab -u www-data /tmp/crontab_http + rm /tmp/crontab_http + + cat > /usr/local/bin/nextcloud-domain.sh <<'EOF' +#!/bin/bash +IFACE=$( ip r | grep "default via" | awk '{ print $5 }' ) +IP=$( ip a | grep "global $IFACE" | grep -oP '\d{1,3}(.\d{1,3}){3}' | head -1 ) +cd /var/www/nextcloud +sudo -u www-data php occ config:system:set trusted_domains 1 --value=$IP +EOF + + mkdir -p /usr/lib/systemd/system + cat > /usr/lib/systemd/system/nextcloud-domain.service <<'EOF' +[Unit] +Description=Register Current IP as Nextcloud trusted domain +Requires=network.target +After=mysql.service + +[Service] +ExecStart=/bin/bash /usr/local/bin/nextcloud-domain.sh + +[Install] +WantedBy=multi-user.target +EOF + systemctl enable nextcloud-domain + + # CLEANUP + ########################################## + + apt-get autoremove -y + apt-get clean + rm /var/lib/apt/lists/* -r + + rm $STATE_FILE + halt +fi + +# License +# +# This script is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This script is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this script; if not, write to the +# Free Software Foundation, Inc., 59 Temple Place, Suite 330, +# Boston, MA 02111-1307 USA +