Upgrading NextCloud from version 10 to 14

Contents

PHP infighting: 5.4.40 vs 5.6.0 vs 7.1 vs 7.2

Most of last week was spent building a process to set up my home file server penguin on CentOS 7. It’s a considerable amount of work because not only am I completely setting up the server from scratch, I’m aslo doing a thorough documentation of the process, in the hopes of largely automating it when it comes time to do this again when CentOS 8 is released.

One of the services on penguin is NextCloud, written in PHP. PHP moves ahead fairly quickly, and people writing software in PHP have a tendency to use the new features. Software that was released two years ago would, of course, run on a version of PHP from two years ago.

But that same software released today won’t run on that two year old version of PHP. The problem is CentOS 5 still runs that two year old PHP (specifically, PHP 5.4.40.)

When I installed the latest version of NextCloud (version 14) on the new server I’m building, it ran fine on PHP 7. It also wanted to migrate the database … but the database was from NextCloud 10. The process works only when migrating from NextCloud 13.

All right, I thought, all I have to do is put NextCloud 11 on to the new server and run the migration there. But that didn’t work, because NextCloud version 11 refused to run on PHP 7.2! The PHP language must have moved forward a lot in the transition from PHP 5 to PHP 7, while at the same time not maintaining compatibility with version 5.

So the next attempt was to put NextCloud 11 on to my current server and run the migration there. Still no go! This time NextCloud said it couldn’t run on PHP 5.4.40; it wants PHP 5.6.0. Guess what … I can’t get PHP 5.6 for CentOS 5.

I noticed, however, that NextCloud 11 specifically said it would not run on PHP 7.2, which implies it will run on PHP 7.1. PHP 7.2 was installed on the new penguin as part of the Pi-hole ad blocker.

So my first attempt at mitigation was assume that both Pi-hole and NextCloud 11 would run on PHP 7.1, and patched the Pi-hole installer to use PHP 7.1 instead of 7.2. That worked.

Migrating from version 10 to 14

Next up was to create a script that upgraded NextCloud to version 12, then 13, and finally 14.

RM_LIST=""
set -o pipefail
for VERSION in 11 12 13 14
do
    cd /var/www
    # Have we migrated this version already?
    ls -d nextcloud-${VERSION}* &>/dev/null && continue

    # Unpack the new version
    PREV_NC_DN="$(ls -d nextcloud* | tail -n1)"
    echo "> Version $((VERSION-1)) to $VERSION"
    echo "  - Unpack nextcloud-$VERSION.tar.bz2"
    tar xjf $DUMPMYDATA/mnt/sparrow/var/tmp/nextcloud-$VERSION.tar.bz2
    chown -R apache:users nextcloud
    NC_VERSION="$(awk -F"'" '/^\$OC_VersionString/{ print $2 }' nextcloud/version.php)"

    # Update the 'nextcloud' symlink
    mv nextcloud nextcloud-$NC_VERSION
    rm -f nextcloud
    ln -s nextcloud-$NC_VERSION nextcloud

    # Move the data files and update the configuration
    cd nextcloud-$NC_VERSION
    cp -a ../$PREV_NC_DN/config/config.php config
    mv ../$PREV_NC_DN/data .
    mv ../$PREV_NC_DN/apps/calendar ../$PREV_NC_DN/apps/contacts apps

    # Migrate to the new version
    echo "  - Run the migration"
    [ $VERSION -lt 14 ] && NO_APP_DISABLE="--no-app-disable" || NO_APP_DISABLE=""
    sudo -u apache stdbuf -oL -eL php occ upgrade -v $NO_APP_DISABLE 2>&1 | sed 's/^/  /'

    echo "  - Version $VERSION installed"
    RM_LIST="${RM_LIST}${PREV_NC_DN} "
done

Increase PHP memory limit

NextCloud recommends increasing memory_limit fron 128M to 512M:

sed --in-place 's/\(memory_limit\) =.*/\1=512M/' /etc/php.ini

Install the php-opcache module

NextCloud recommended the installation of the php-opcache module with the folllowing configuration options in /etc/php.d/10-opcache.ini:

opcache.enable=1
opcache.enable_cli=1                     Changed from default
opcache.interned_strings_buffer=8
opcache.max_accelerated_files=10000      Recommended, but default is 4000
opcache.memory_consumption=128
opcache.save_comments=1                  Changed from default
opcache.revalidate_freq=1                Changed from default

Code to set the options (optimized for readability):

sed --inplace 's/^;\(opcache.enable_cli\)=./\1=1/
    s/^;\(opcache.save_comments\)=./\1=1/
    s/^;\(opcache.revalidate_freq\)=./\1=1/' /etc/php.d/10.opcache.ini

Add indexes to table

NextCloud also recommended adding indexes to three tables:

  • Missing index “share_with_index” in table “oc_share”.
  • Missing index “parent_index” in table “oc_share”.
  • Missing index “fs_mtime” in table “oc_filecache”.

That was an easy job from the command line:

sudo -u apache php occ db:add-missing-indices

Upgrade the Calendar and Contacts apps

Then NextCloud complained about ancient versions of the calendar and contacts apps. This could not be fixed from the command line, so as part of setting up and configuraing the new penguin I need to do the following:

  • Log in to NextCloud as admin
  • Go to Admin → Apps
  • In Disabled Apps, delete the calendar and contacts apps
  • In App Bundles, install and enable the calendar and contacts apps
  • In Administration → Overview, rescan the code base.