[How-To] Move Nextcloud Data Directory
Purpose
The purpose of this document is to show how to move the data directory of a nextcloud server.
Prerequisites
List of prerequisites:
- Root user or sudo user
- Nextcloud Server
Data Directory Move
Step 1: Shut Down Nextcloud Web Server
First, we need to stop Apache so nextcloud is not active. Do this with the following command:
sudo systemctl stop apache2
Step 2: Move Data Directory Contents
Next, we have to move the contents of the data directory. First, become root:
sudo su -l
Then, use the copy command to move everything from the original dir to the target dir for new data:
cp -r /var/www/nextcloud/data /mnt/nc-data/
Step 3: Change Data Directory in Nextcloud Config
Next, change the data directory in the nextcloud config:
sudo nano /var/www/nextcloud/config/config.php
'datadirectory' => '/mnt/nc-data/data',
Around line 15, you should see like above, change this to the new data directory.
Step 4: Change Owner Permissions
Next, we need to update www-data to be the owner of the new data dir:
sudo chown -R www-data:www-data /mnt/nc-data/data
Step 5: Start Nextcloud Web Server
Finally, start the nextcloud web server for the first time with the new data directory:
sudo systemctl start apache2
Once the web server has started, try to browse to the URL. If everything is good, you'll get to login page. If it says something about a file not existing, you've not done things in order. After getting past login, go to admin settings to check for errors. It will likely complain about the cronjob not being able to run. Just run it manually once like this then it should be good going forward:
sudo -u www-data php -f /var/www/nextcloud/cron.php
Once everything is good to go with no errors and you have backups, remove the old data directory:
sudo rm -rf /var/www/nextcloud/data