How to migrate Nextcloud to a new Server with Ubuntu 16.04 LTS

Today I want to show you how to move a Nextcloud instance to a new VPS. Reasons may be that our one runs out of space and the VPS can not be increased by the provider.

We should be aware that depending on the TTL in the DNS a downtime for our Nextcloud instance arises. If possible reduce the TTL to the minimum (depending on the DNS provider) and for larger amounts of data you can do a first synchronization one day before. In any case, I recommend testing the procedure once and familiarizing yourself with this manual. Afterwards the live migration can be carried out.

Step 1: Preparation of the new server

A prerequisite, of course, is a new VPS with more storage space and shell access, which we prepare for Nextcloud. For Nextcloud 13 and php7.2-fpm with Apache2 and mysql, we can do the following things.

sudo apt install -y software-properties-common
sudo apt install -y python-software-properties
sudo add-apt-repository -y ppa:ondrej/php
sudo add-apt-repository -y ppa:ondrej/apache2
sudo add-apt-repository -y ppa:certbot/certbot
sudo apt update
sudo apt install -y lamp-server^
sudo apt install -y libapache2-mod-php7.2 php7.2-cli php7.2-common php7.2-mbstring php7.2-gd php7.2-intl php7.2-xml php7.2-mysql php7.2-zip php7.2-dev php7.2-curl php7.2-fpm php-dompdf php-apcu redis-server php-redis unzip nano
sudo a2dismod php7.2 mpm_prefork
sudo a2enmod proxy_fcgi setenvif mpm_event
sudo a2enconf php7.2-fpm
sudo apt install -y python-certbot-apache

we secure our mysql-installation:

sudo mysql_secure_installation

and adjust apache2-conf:

sudo nano /etc/apache2/apache2.conf

change the following code:

<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>

to

<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>

an don’t forget the php.ini:

sudo nano /etc/php/7.2/fpm/php.ini

paste the following parameters:

opcache.enable=1
opcache.enable_cli=1
opcache.interned_strings_buffer=8
opcache.max_accelerated_files=10000
opcache.memory_consumption=128
opcache.save_comments=1
opcache.revalidate_freq=1

If php-APCu is in use, then the php.ini must be extended accordingly, see also here.

To synchronize, we’ll use rsync later, if not installed, then just install it:

sudo apt install rsync

To use rsync with sudo, we need to adjust the /etc/sudoers on the destination server:

Check path of rsync:

which rsync

then:

sudo visudo

and at the very bottom, add the following as the last line (adjust the path):

username  ALL=NOPASSWD:/usr/bin/rsync

Save file.

Step 2: Migration of Nextcloud data, certificates, and Apache2 vhosts

Live migration only: Before we begin, we should adapt the DNS record (A and possibly AAAA) to the new server IP..

We start with the smaller stuff and sync the certificates and Apache2 vHosts to the new server.

Synchronize certificates (adapt paths and new hostname / IP, sudo user should also exist on new server):

sudo rsync -azv --rsync-path="sudo rsync" --progress /etc/letsencrypt/live/nextcloud.your-domain.tld/ username@hostname:/etc/letsencrypt/live/nextcloud.your-domain.tld/

Synchronize Apache2-vHosts:

sudo rsync -azv --rsync-path="sudo rsync" --progress /etc/apache2/sites-available/001-nextcloud* username@hostname:/etc/apache2/sites-available/

Live migration only: Now we activate the maintenance mode in Nextcloud:

sudo -u www-data php /var/www/html/nextcloud/occ maintenance:mode --on

and continue with the data:

sudo rsync -azv --rsync-path="sudo rsync" --progress /var/www/html/nextcloud/ username@hostname:/var/www/html/nextcloud

Step 3: Migrate Database

On the source server, we dump the Nextcloud database with:

mysqldump -u root -pdein-passwort -h localhost --verbose nextcloud > /tmp/nextcloud.sql

(right: no space behind -p)

and copy the dump-file with scp to the new server:

scp /tmp/nextcloud.sql username@hostname:/tmp/

Before importing the database, we create en empty one:

mysql -u root -p
create database nextcloud;
exit

we import the database to the new host:

mysql -u root -pyour-password nextcloud </tmp/nextcloud.sql

and we create the database-user:

sudo mysql -u root -p
create user nextcloud@localhost identified by 'YOUR_PASSWORD';
grant all privileges on nextcloud.* to nextcloud@localhost;
flush privileges;
exit;

If we do not know the old password for the mysql user, we can look in the config.php.

cat /var/www/html/nextcloud/config/config.php | grep dbpassword

Step 4: Activate Apache2-vHosts and Test

The following command enables the two Nextcloud vHosts (http/https):

sudo a2ensite 001-nextcloud.conf
sudo a2ensite 001-nextcloud-le-ssl.conf
sudo systemctl restart apache2

live migration only deactivate maintenance mode in Nextcloud:

sudo -u www-data php /var/www/html/nextcloud/occ maintenance:mode --off

andf create cronjob for user www-data cron-job:

sudo crontab -u www-data -e

insert the line there:

*/15 * * * * php -f /var/www/html/nextcloud/cron.php

We can test the Nextcloud instance by entering the new IP address in our local hosts file on our workstation:

Linux:

/etc/hosts

Windows:

C:\Windows\System32\drivers\etc\hosts

Again, check the log files and if there are no errors, then either live migration or wait until new DNS record was distributed.
The LetsEncrypt certificate will then be renewed with:

sudo certbot --apache

If the login button does not work after migration or testing, it may be enough to comment the line with „instanceid“ in config.php.

That’s it for today and good luck with „Nextclouding“ 🙂

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert