nextcloudpi

NextCloudPi gets remote updates, logs to RAM, easy WiFi and more

The latest release of NextCloudPi is out!

The most noticeable change for users is remote updates.

As new versions come out, we will have the option to upgrade our private cloud directly from the internet.

This means that from now on it will be easier for users to get the latest goodies without having to start from scratch.

Also, given that the most common configuration involves the use of an external USB drive, there is a number of new options to achieve an better setup.

Remote updates

This is the key structural change for this release.

NextCloudPi will look in github for updates to NextCloudPi and warn the user upon login.

The user can easily upgrade through nextcloudpi-config or using the new command

sudo ncp-update

pi@raspberrypi:~ $ sudo ncp-update
Downloading updates
Performing updates
Installing nc-database
Installing nc-ramlogs
Installing nc-swapfile
Installing nc-wifi
Get:1 http://mirrordirector.raspbian.org jessie InRelease [14.9 kB]
Get:2 http://mirrordirector.raspbian.org stretch InRelease [15.0 kB]
Get:3 http://archive.raspberrypi.org jessie InRelease [22.9 kB]
Get:4 http://mirrordirector.raspbian.org jessie/main armhf Packages [8,981 kB]
Get:5 http://archive.raspberrypi.org jessie/main armhf Packages [145 kB]
Get:6 http://mirrordirector.raspbian.org jessie/contrib armhf Packages [37.5 kB]
Get:7 http://mirrordirector.raspbian.org jessie/non-free armhf Packages [70.3 kB]
Get:8 http://mirrordirector.raspbian.org jessie/rpi armhf Packages [1,356 B]
Get:9 http://mirrordirector.raspbian.org stretch/main armhf Packages [11.7 MB]

Easy WiFi

NextCloudPi comes ready to connect through WiFi!

Either configure your access point through

sudo nextcloudpi-config

or

sudo wicd-curses

RAM logs

This setting moves logs to RAM, which has the following consequences

  • Better performance: as logging to SD is way slower.
  • Better SD card life: as wear produced by constantly writing logging info to the SD is greately reduced.
  • It consumes a little bit more RAM: ~30 MB in my 30 day uptime Raspberry Pi.
  • Logs are lost upon reboot.
  • Any new package that logs to a subdirectory inside /var/log will require the folder to be created in every boot.

For instance, in Apache a new pre-execution command is added

ExecStartPre=/bin/mkdir -p /var/log/apache2

If that is too advanced for you, do not use this feature.

If you want to try it and get in trouble, it can be deactivated through nextcloudpi-config.

Configure swap file

As many people will have a permanent USB attached to the Raspberry Pi, we can have a big swap file in there.

First, configure the USB drive to be mounted on boot in /etc/fstab. Then, configure through nextcloudpi-config.

You can verify the results with

swapon -s

Move database

As many people will have a permanent USB attached to the Raspberry Pi, we can have the database in there.

This will improve our SD card life for the same reasons as above, and likewise our data will be more safe, as USB drives are less prone to corruption than an SD card in a Raspberry Pi.

hint: Never unplug your Raspberry Pi unless it is halted with ‘sudo halt’. It is a recipe for SD card corruption.

Code

#!/bin/bash

# Nextcloud installation on Raspbian 
# Tested with 2017-03-02-raspbian-jessie-lite.img
#
# Copyleft 2017 by Ignacio Nunez Hernanz <nacho _a_t_ ownyourbits _d_o_t_ com>
# GPL licensed (see end of file) * Use at your own risk!
#
# Usage:
# 
#   ./installer.sh no-ip.sh <IP> (<img>)
#
# See installer.sh instructions for details
#
# 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
#
# More at https://ownyourbits.com/2017/02/13/nextcloud-ready-raspberry-pi-image/
#

VER=11.0.1
ADMINUSER_=admin
DBADMIN_=ncadmin
DBPASSWD_=ownyourbits
MAXFILESIZE_=768M
MAXTRANSFERTIME_=3600
OPCACHEDIR=/var/www/nextcloud/data/.opcache
CONFDIR=/usr/local/etc/nextcloudpi-config.d/
STATE_FILE=/home/pi/.installation_state
APTINSTALL="apt-get install -y --no-install-recommends"


install()
{
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 }' )
  echo -e "d\n2\nn\np\n2\n$SECTOR\n\nw\n" | fdisk /dev/sda || true

  echo 0 > $STATE_FILE 
  nohup reboot &>/dev/null &
elif [ "$STATE" == "0" ]; then

  # UPDATE EVERYTHING
  ##########################################
  resize2fs /dev/sda2

  apt-get update
  apt-get upgrade 
  apt-get dist-upgrade 
  $APTINSTALL rpi-update 
  echo -e "y\n" | rpi-update

  echo 1 > $STATE_FILE 
  nohup reboot &>/dev/null &
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 <<EOF
Package: *
Pin: release n=jessie
Pin-Priority: 600
EOF
  apt-get update

  # INSTALL FROM STRETCH
  ##########################################

  $APTINSTALL -t stretch apache2
  $APTINSTALL -t stretch php7.0 php7.0-curl php7.0-gd php7.0-fpm php7.0-cli php7.0-opcache php7.0-mbstring php7.0-xml php7.0-zip 
  $APTINSTALL php7.0-APC 
  $APTINSTALL libxml2-dev php-zip php-dom php-xmlwriter php-xmlreader php-gd php-curl php-mbstring 

  debconf-set-selections <<< "mariadb-server-5.5 mysql-server/root_password password $DBPASSWD_"
  debconf-set-selections <<< "mariadb-server-5.5 mysql-server/root_password_again password $DBPASSWD_"
  $APTINSTALL mariadb-server php7.0-mysql 

  # CONFIGURE APACHE AND PHP7
  ##########################################

  cat >/etc/apache2/conf-available/http2.conf <<EOF
Protocols h2 h2c http/1.1

H2Push          on
H2PushPriority  *                       after
H2PushPriority  text/css                before
H2PushPriority  image/jpeg              after   32
H2PushPriority  image/png               after   32
H2PushPriority  application/javascript  interleaved

SSLProtocol all -SSLv2 -SSLv3
SSLHonorCipherOrder on
SSLCipherSuite 'EECDH+ECDSA+AESGCM EECDH+aRSA+AESGCM EECDH+ECDSA+SHA384 EECDH+ECDSA+SHA256 EECDH+aRSA+SHA384 EECDH+aRSA+SHA256 EECDH+aRSA+RC4 EECDH EDH+aRSA !RC4 !aNULL !eNULL !LOW !3DES !MD5 !EXP !PSK !SRP !DSS'
EOF

  cat >> /etc/apache2/apache2.conf <<EOF
<IfModule mod_headers.c>
  Header always set Strict-Transport-Security "max-age=15768000; includeSubDomains; preload"
</IfModule>
EOF

  cat > /etc/php/7.0/mods-available/apcu.ini <<EOF
extension=apcu.so
apc.enable_cli=0
apc.shm_size=256M
apc.ttl=7200
apc.gc_ttl=3600
apc.entries_hint=4096
apc.slam_defense=1
apc.serializer=igbinary
EOF

  cat > /etc/php/7.0/mods-available/opcache.ini <<EOF
zend_extension=opcache.so
opcache.file_cache=$OPCACHEDIR;
opcache.fast_shutdown=1
EOF

  a2enmod http2
  a2enconf http2 
  a2enmod proxy_fcgi setenvif
  a2enconf php7.0-fpm
  a2enmod rewrite
  a2enmod headers
  a2enmod env
  a2enmod dir
  a2enmod mime
  a2enmod ssl

  echo 2 > $STATE_FILE 
  nohup reboot &>/dev/null &

elif [ "$STATE" == "2" ]; then
  # INSTALL NEXTCLOUD
  ##########################################

  cd /var/www/
  wget https://download.nextcloud.com/server/releases/nextcloud-$VER.tar.bz2 -O nextcloud.tar.bz2
  tar -xvf nextcloud.tar.bz2
  rm nextcloud.tar.bz2

  ocpath='/var/www/nextcloud'
  htuser='www-data'
  htgroup='www-data'
  rootuser='root'

  printf "Creating possible missing Directories\n"
  mkdir -p $ocpath/data
  mkdir -p $ocpath/updater
  mkdir -p $OPCACHEDIR

  printf "chmod Files and Directories\n"
  find ${ocpath}/ -type f -print0 | xargs -0 chmod 0640
  find ${ocpath}/ -type d -print0 | xargs -0 chmod 0750

  printf "chown Directories\n"
  # recommended defaults do not play well with updater app
  # re-check this with every new version
  #chown -R ${rootuser}:${htgroup} ${ocpath}/
  chown -R ${htuser}:${htgroup} ${ocpath}/
  chown -R ${htuser}:${htgroup} ${ocpath}/apps/
  chown -R ${htuser}:${htgroup} ${ocpath}/config/
  chown -R ${htuser}:${htgroup} ${ocpath}/data/
  chown -R ${htuser}:${htgroup} ${ocpath}/themes/
  chown -R ${htuser}:${htgroup} ${ocpath}/updater/
  chown -R ${htuser}:${htgroup} $OPCACHEDIR

  chmod +x ${ocpath}/occ

  printf "chmod/chown .htaccess\n"
  if [ -f ${ocpath}/.htaccess ]; then
    # breaks updater, see above
    #chmod 0644 ${ocpath}/.htaccess
    chmod 0664 ${ocpath}/.htaccess
    chown ${rootuser}:${htgroup} ${ocpath}/.htaccess
  fi
  if [ -f ${ocpath}/data/.htaccess ]; then
    chmod 0644 ${ocpath}/data/.htaccess
    chown ${rootuser}:${htgroup} ${ocpath}/data/.htaccess
  fi

cat > /etc/apache2/sites-available/000-default.conf <<'EOF'
<VirtualHost _default_:80>
  DocumentRoot /var/www/nextcloud
  <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{HTTPS} !=on
    RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L]
  </IfModule>
</VirtualHost>
EOF

cat > /etc/apache2/sites-available/nextcloud.conf <<'EOF'
<IfModule mod_ssl.c>
  <VirtualHost _default_:443>
    DocumentRoot /var/www/nextcloud
    CustomLog /var/www/nextcloud/data/access.log combined
    ErrorLog /var/www/nextcloud/data/error.log
    SSLEngine on
    SSLCertificateFile      /etc/ssl/certs/ssl-cert-snakeoil.pem
    SSLCertificateKeyFile /etc/ssl/private/ssl-cert-snakeoil.key
  </VirtualHost>
  <Directory /var/www/nextcloud/>
    Options +FollowSymlinks
    AllowOverride All
    <IfModule mod_dav.c>
      Dav off
    </IfModule>
  </Directory>
</IfModule>
EOF
  a2ensite nextcloud

  mysql -u root -p$DBPASSWD_ <<EOF
CREATE DATABASE nextcloud;
CREATE USER '$DBADMIN_'@'localhost' IDENTIFIED BY '$DBPASSWD_';
GRANT ALL PRIVILEGES ON nextcloud.* TO $DBADMIN_@localhost;
EXIT
EOF

  # NEXTCLOUDPI-CONFIG
  ##########################################

  $APTINSTALL dialog
  mkdir -p $CONFDIR
  sed -i '/Change User Password/i"0 NextCloudPi Configuration" "Configuration of NextCloudPi" \\\\'  /usr/bin/raspi-config
  sed -i '/1\\\\ \*) do_change_pass ;;/i0\\\\ *) nextcloudpi-config ;;'                              /usr/bin/raspi-config

  # NEXTCLOUDPI MOTD
  ##########################################
  mkdir /etc/update-motd.d
  rm /etc/motd
  ln -s /var/run/motd /etc/motd

  cat > /etc/update-motd.d/10logo <<EOF
#!/bin/sh
echo
cat /usr/local/etc/ncp-ascii.txt
EOF

  cat > /etc/update-motd.d/20updates <<'EOF'
#!/bin/bash
/usr/local/bin/ncp-check-updates
EOF
  chmod a+x /etc/update-motd.d/*

  # NEXTCLOUDPI UPDATES
  ##########################################
  $APTINSTALL git

  cat > /usr/local/bin/ncp-check-updates <<'EOF'
#!/bin/bash

NEED_UPDATE=false
VERFILE=/var/run/.ncp-latest-version

if test -f $VERFILE && grep -qP "v\d+\.\d+\.\d+" $VERFILE; then

  MAJOR=$( grep -oP "\d+\.\d+\.\d+" $VERFILE | cut -d. -f1 )
  MINOR=$( grep -oP "\d+\.\d+\.\d+" $VERFILE | cut -d. -f2 )
  PATCH=$( grep -oP "\d+\.\d+\.\d+" $VERFILE | cut -d. -f3 )

  MAJ=$( grep -oP "\d+\.\d+\.\d+" /usr/local/etc/ncp-version | cut -d. -f1 )
  MIN=$( grep -oP "\d+\.\d+\.\d+" /usr/local/etc/ncp-version | cut -d. -f2 )
  PAT=$( grep -oP "\d+\.\d+\.\d+" /usr/local/etc/ncp-version | cut -d. -f3 )

  if [[ $MAJOR > $MAJ ]]; then
    NEED_UPDATE=true
  elif [[ $MAJOR == $MAJ ]] && [[ $MINOR > $MIN ]]; then
    NEED_UPDATE=true
  elif [[ $MAJOR == $MAJ ]] && [[ $MINOR == $MIN ]] && [[ $PATCH > $PAT ]]; then
    NEED_UPDATE=true
  fi
fi

if $NEED_UPDATE; then
  echo -e "\nNextCloudPi \e[1m$( cat $VERFILE )\e[0m available!!"
  echo -e "update through 'nextcloudpi-config' or type 'sudo ncp-update'" 
else
  echo -e "\nNextCloudPi \e[1m$( cat /usr/local/etc/ncp-version )\e[0m is up to date"
fi
EOF
  chmod a+x /usr/local/bin/ncp-check-updates


  cat > /etc/cron.daily/ncp-check-version <<EOF
#!/bin/sh
/usr/local/bin/ncp-check-version
EOF
  chmod a+x /etc/cron.daily/ncp-check-version


  cat > /usr/local/bin/ncp-check-version <<'EOF'
#!/bin/bash
[ $(id -u) -ne 0 ] && exit 1
git clone -q --depth 1 https://github.com/nachoparker/nextcloud-raspbian-generator.git /tmp/ncp-check-tmp
cd /tmp/ncp-check-tmp
git describe --always --tags > /var/run/.ncp-latest-version
cd /
rm -rf /tmp/ncp-check-tmp
EOF
  chmod a+x /usr/local/bin/ncp-check-version


  cat > /usr/local/bin/ncp-update <<'EOF'
#!/bin/bash
echo -e "Downloading updates"
rm -rf /tmp/ncp-update-tmp
git clone -q --depth 1 https://github.com/nachoparker/nextcloud-raspbian-generator.git /tmp/ncp-update-tmp
cd /tmp/ncp-update-tmp

echo -e "Performing updates"
./update.sh

VER=$( git describe --always --tags )
echo $VER > /usr/local/etc/ncp-version
echo $VER > /var/run/.ncp-latest-version

cd /
rm -rf /tmp/ncp-update-tmp

echo -e "NextCloudPi updated to version \e[1m$VER\e[0m"
EOF
  chmod a+x /usr/local/bin/ncp-update

  /usr/local/bin/ncp-update
fi
}

configure()
{ 
  [ "$STATE" != "2" ] && return
  cd /var/www/nextcloud/

  sudo -u www-data php occ maintenance:install --database \
  "mysql" --database-name "nextcloud"  --database-user "$DBADMIN_" --database-pass \
  "$DBPASSWD_" --admin-user "$ADMINUSER_" --admin-pass "$DBPASSWD_" 

  sudo -u www-data php occ background:cron

  sed -i '$s|^.*$|  '\''memcache.local'\'' => '\''\\\\OC\\\\Memcache\\\\APCu'\'',\\n);|' /var/www/nextcloud/config/config.php

  sed -i "s/post_max_size=.*/post_max_size=$MAXFILESIZE_/"             /var/www/nextcloud/.user.ini 
  sed -i "s/upload_max_filesize=.*/upload_max_filesize=$MAXFILESIZE_/" /var/www/nextcloud/.user.ini 
  sed -i "s/memory_limit=.*/memory_limit=$MAXFILESIZE_/"               /var/www/nextcloud/.user.ini 

  # slow transfers will be killed after this time
  cat >> /var/www/nextcloud/.user.ini <<< "max_execution_time=$MAXTRANSFERTIME_"

  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()   
{ 
  [ "$STATE" != "2" ] && return
  apt-get autoremove
  apt-get clean
  rm /var/lib/apt/lists/* -r
  rm -f /home/pi/.bash_history

  systemctl disable ssh
  rm $STATE_FILE 
  nohup halt &>/dev/null &
}

# 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

github

Author: nachoparker

Humbly sharing things that I find useful [ github dockerhub ]

21 Comments on “NextCloudPi gets remote updates, logs to RAM, easy WiFi and more

  1. Thanks for making these posts, and continuing to provide terrific updates. I am following your progress closely, as I just bought pi3 + kit to make a local network nextcloud server w/ USB drive. Perfect fit for your image.

    If you put up a donation link I would happily throw some support your way as a sign of gratitude! You are doing important work as I only discovered owncloud/nextcloud via your blog posts . I hope for a future where nextcloud and other likeminded products are mainstream and easy enough for the normal user to get working. You are totally making that happen with your images and posts !

  2. I did a fresh install due to the new features, updates!!
    – I’ve enabled everything except dnsmasq, modsecurity and nc-wifi –Ran ncp-update and Nextcloud is running like a champ! 😀

    Did; curl -sSL https://install.pi-hole.net | bash
    — went to /etc/lighttpd/lighttpd.conf and changed the port to 8080
    — reboot

    This didn’t work like the previous version and i can’t access the nextcloud https on wan/lan or the http://8080 pihole admin page on lan.

    Could something other then lighttpd cause a conflict?

  3. Nacho, thank you for the rpi nc image! Had it up and running in no time. You make it so easy! In the process my knowledge of linux, cli and nc has grown exponentially. What you are doing not only with your rpi image dev but also generally (cf yr latest post) is amazing. Congratulations and a _huge_ thank you!

  4. I don’t understand. I have 2 problems.

    Wifi is not finding any nets, i’m connecting to the pi via putty, connected to ethernet, but it does not enable the wifi module i think.
    Another problem is trusted domains, i try to add trusted domains, but it does not add it to config.php, and if i try to add it manually it crashes everything.

    Also, yesterday i rebooted the pi with sudo reboot now, it would never boot again, and i had to reflash the SD.

    Seems promising, but the wifi bug is weird, the rest i think maybe is possible to figure out.

    1. What version are you using? you can check typing `ncp-check-updates`. If you are not in v0.7.1 update with `sudo ncp-update`

      Then, try to connect through `sudo nextcloudpi-config`, not through `sudo wicd-curses`

      NextCloudPi configures trusted domains for you when you configure dnsmasq. How are you trying to do it? In any case, the standard procedure should work if you insist on doing it manually ( `sudo -u www-data php occ config:system:set trusted_domains 2 –value=mydomain` , or editing config.php)

      In any case, crashes are related to hardware problems, most likely corrupt/uncompatible SD cards, or cheap power sources. Nothing in userspace software can crash your system. That is unrelated to NextCloudPi.

      1. Can i have ethernet plugged in when finding wifi?

        I dont have a screen connected to it, so i have not tested with ethernet unplugged, but i’ve successfully setup wifi before in raspbian with the wpa-supplicant config., maybe i can use this here too, but i thought that your config was kind of cool

        I am on the latest, 0.7.1

        Ok, i have not tried dnsmasq yet, but i would like to try to get wifi to work first.

        1. Hello,

          It seems like you found a bug. Indeed, if you boot with a wired interface connected no wifi comes up.

          I registered the issue https://github.com/nachoparker/nextcloud-raspbian-generator/issues/6

          Please, in the future report any issues that you find over here

          https://github.com/nachoparker/nextcloud-raspbian-generator/issues

          It is more appropriate to keep track of them and mark them as resolved.

          If you do not want to wait for the solution, you can make wifi work by

          – press `P` (preferences)
          – in `wireless interface` type `wlan0`.
          – hit `F10` to save the change
          – hit `R` (refresh) to rescan for wireless networks

          Thank you for your feedback

  5. Great work. Spent a few hours last night figuring out fstab and now have my data directory on a mounted cifs share all encrypted very nice! One question though – if the data directory is elsewhere, is nc-update safe? Or will it default certain settings back? Also is there a way to turn off the nextcloud brute force protection or whitefish an IP?

  6. Hello Nachoparker

    I just stumbled upon your webiste. You did what I was always hoping for that someone would do. Thank you for the Raspberry Pi image for nextcloud.

    Thank you so much!

  7. Hello Nachoparker, great job, but I’m newbie and I need some advice: after moving the database is no longer the nextcloud login page, what have I done? How can I fix this thing? Thanks again

    1. Another thing when I move the data directory does not see the disk capacity, but only 2Gb, where does the display change? Thanks

      1. Hi, I used nextcloudpi-config to move everything but failed, but first I saw where usb disk was mounted (/media/pi/hdd name/folder). so, the disk was moved, but never update the capacity in nextcloud, the database failed.

Leave a Reply

Your email address will not be published. Required fields are marked *