Moving Nextcloud to another server

For example, I will transfer Nextcloud from one Linux server to another and change the domain.

First of all, we prepare a new server, for example, as I described in this article:
Install Nextcloud in Ubuntu

sudo apt-get install apache2 mysql-server php libapache2-mod-php php-json php-mysql php-bz2 php-curl php-gd php-imagick php-intl php-mbstring php-xml php-zip php-memcache php-bcmath php-gmp

sudo a2enmod rewrite
sudo a2enmod headers
sudo a2enmod env
sudo a2enmod dir
sudo a2enmod mime
sudo a2enmod setenvif
sudo a2enmod ssl
sudo service apache2 restart

Install an SSL certificate for the domain, for example from Let’s Encrypt.
Let’s set up the web server.

Let’s create a database and import a backup copy of the data into it:

mysql -u root -p
CREATE USER 'nextcloud'@'localhost' IDENTIFIED BY '';
GRANT ALL PRIVILEGES ON nextcloud.* TO nextcloud@localhost;
flush privileges;
gzip -d nextcloud.sql.gz
mysql -D nextcloud < nextcloud.sql

Copy the Nextcloud files (and the data directory if it is placed separately):

rsync -avh -e "ssh -p 222" --delete /var/www/cloud/
rsync -avh -e "ssh -p 222" --delete /srv/hdd/nextcloud/data/

If there are a lot of files, then I recommend running the copy process in screen so that you can disconnect from ssh (Ctrl+a d to disconnect from the terminal, -r to connect):

screen -S nextcloud
screen -r nextcloud

If necessary, change the domain name in the configuration /config/config.php

'trusted_domains' =>
  array (
   0 => '',

If apache2 is installed, then most likely OPcache cached the old domain name, so we will restart apache2 to update:

service apache2 restart

Now it remains to enter the Nextcloud web interface and, if necessary, update to the latest version (if after the update you need to add indexes in the database, then add them):

cd /var/www/nextcloud
sudo -u www-data php occ db:add-missing-indices

During Nextcloud migration, you can enable maintenance mode:

sudo -u www-data php occ maintenance:mode --on

See also my articles:
Nextcloud. Migrating the DATA directory

Leave a comment

Leave a Reply