Is there a safe and reliable way to move data directory out of web root?

I have been struggling with this issue all week and finally came across this very useful thread. Thanks to all contributors. However, after going through the various items listed above in robertb’s posting of Sep '16 I still had the mentioned error :

Data directory (/etc/nextclouddata/nextcloud/data/) is invalid. Please check that the data directory contains a file “.ocdata” in its root.

I had learnt in other attempts that the nextcloud.conf has also to be modified by adding the new data directory path as follows (using robertb’s data directory as an example) :

php_admin_value open_basedir “/srv/http/:/dev/urandom:/tmp/:/usr/share/pear/:/usr/share/webapps/nextcloud/:/etc/webapps/:/etc/nextclouddata/nextcloud/data/”

So I tried adding my path and all is working again with a new directory path.

Hope this comment might help those using Nextcloud 11.

Note : I am running on Archlinux so the apache user is “http” and not “www-data”

nextcloud 11.0.2-1
php 7.1.3-1
apache 2.4.25-1
mariadb 10.1.22-1
archlinux 4.10.6-1

Hi, I have the latest version of Nextcloud running on Raspbian. I tried to change the path of the data directory. I moved all /mnt/dietpi_userdata/nextcloud_data files to /mnt/myexternaldrive/nextcloud, then do chown -R www-data:www-data, changed settings in config.php and in oc_storage in the database.

I still get Data directory (/mnt/myexternaldrive/nextcloud) is invalid. Please check that the data directory contains a file “.ocdata” in its root.

I can’t find any nextcloud.conf. What may I do?

Thank you!

Unfortunately I’m a total rookie when it comes to this. I got very lucky with some good advice.

Did you run the following?

chown -R www-data:www-data /mnt/myexternaldrive/nextcoud/data

If I had to guess, it sounds like your path to your data file is specified incorrectly. But again, my experience is minimal.


when re-attaching the external hard drive/usb, make sure you plug it in on the same port.

i had it configured in the fstab and when i plugged it into another port i couldn’t see a thing inside the HDD.

i hope this works for you… :slight_smile:

I have successfully configured nextcloud so that my data-folder is located on a external USB HDD.

Personally, I reinstalled nextcloud because moving the data-folder after installation is not officially supported.

I formatted my HDD as NTFS so the data would be accessible from other OSs and pasted the following last in /etc/fstab (remember to insert correct UUID):
UUID="[INSERT UUID]" /mnt/ekstern ntfs-3g defaults,auto,uid=33,gid=33,umask=077 0 0

Hope this helps.

I posted this on another thread a while back, but thought it could assist you.

Though it may not be supported as others have pointed out in many threads, I have yet to have an issues changing data directorys; I have changed the data directory many times on many different OwnCloud & NextCloud hosts just fine without any issues. These are the steps I take to move the data directory: (This only works if you have no data to move, as if this is a fresh install)

Run the following command in the Linux shell:

o sudo service apache2 stop

o sudo mkdir /media/user/your_hard_drive/nextcloud_data “or whatever you want to call it?” Remember the /media is your mount point or external hard drive.

o sudo chown -R www-data:www-data /media/user/your_hard_drive/nextcloud_data/

o sudo chmod -R 777 /media/user/your_hard_drive/nextcloud_data/

o sudo chown –R www-data:www-data /media

o sudo nano /var/www/html/nextcloud/config/config.php

o Once you are in the config.php you need to change your data directory. Save file and exit.

o Sudo service apache2 start

Hope this helps you out.


I have moved my data folder using the steps described by @wclang and at first sight everything seemed to be fine. However after a restart of my system I received an Internal Server Error right after the login, see “

One step that is missing but mentioned by the previous posts is to update the database table oc_storages. Also you should activate the maintenance mode inside config.php before you apply the changes.

One additional thing I had to do was to delete all the contents from the table oc_filecache. Afterwards I simply restarted Apache, deactivated maintenance mode and when I logged in to nextcloud, everything was working fine again.

For future reference folks, you can also just symlink from /var/www/data (or wherever you set it up initially) to your new location after moving everything. I’m personally a fan of sudo rsync -avP as it preserves all files, permissions, ownership and shows progress as it goes, then deleting the original when I’ve confirmed everything (including hidden files) has been copied over.

That way there’s no need to update the DB and you won’t be told that’s unsupported.


I would add :
6: check the read/write permissions of the new folder (if created and not only moved, as suggested by @robertb )

I should of stated in my post that my process is for fresh builds only without any data in it, if you have data then following “sirea” additional steps will be required. Thanks for pointing that out “sirea” :slight_smile:

note th[quote=“JasonBayton, post:4, topic:3642”]
Make sure no cron jobs are running
Stop apache
Move /data to the new location
Create a symlink from the original location to the new location
Ensure permissions are still correct
Restart apache

Note that you have to configure your webserver to respect that symlink, I believe not all do :smiley:

1 Like

Edited that in :slight_smile:

1 Like

I added a new 1TB virtual drive and moved the data directory in NextCloud 12.0.0 by doing the following:

Partition and mount new 1TB drive (standard disclaimer - if you don’t know what this does, don’t do it, you will likely lose data)

sudo mkfs.ext4 -G 4096 /dev/sdb
sudo blkid /dev/sdb
/dev/sdb: UUID=“copy ID listed here” TYPE="ext4"
sudo mkdir /mnt/data
sudo nano etc/fstab

/mnt/data was on /dev/sdb

UUID=paste_UUID_here /mnt/data ext4 defaults 0 2

sudo mount -a
sudo chmod -Rf 777 /mnt/data

Now for the data move to fresh disk:
sudo systemctl stop apache2
sudo rsync -avP /var/www/nextcloud-data /mnt/data/
sudo nano /var/www/nextcloud/config/config.php
’datadirectory’ => ‘/mnt/data/nextcloud-data/’,
sudo shutdown -r now

Seemed to work just fine.

this steps worked for me too. could it really be that easy?

You missed the database edit. See my solution above:

Not quite.

Thanks JaysonBayton. You are correct, I missed the database edit.
Fooled myself because everything continued to work, but of course the new data location wasn’t being used. Probably would have been a long time until I realized it. Thanks again.

sudo systemctl stop apache2
sudo rsync -avP /var/www/nextcloud-data /mnt/data/
sudo nano /var/www/nextcloud/config/config.php
’datadirectory’ => ‘/mnt/data/nextcloud-data/’,

Edit database nextcloud
Database: nextcloud
Row: change row data from local::/var/www/nextcloud-data/ to local::/mnt/data/nextcloud-data/
sudo systemctl stop MySQL
sudo su
mv /var/www/nextcloud-data/ /var/www/nextcloud-data-old/
sudo shutdown -r now

A bit off topic: I used HeidiSQL with plink SSH Tunnel to manage MariaDB and do the nextcloud database edit as I’m connecting from Windows - a few things to know to connect to MariaDB, I’ll share if anyone is interested


Without the database edit the new disk is not being used.

Redo rsync, edit db, rename original data folder to be sure - you can delete it later.

Maybe we can move this to howto, as still questions about that appear. Or ah, I will collect all information here and write some howto the next hour. About symlink, which seems to be an easy alternative solution: How the enable symlink support for webservers or be sure that they’re supported?
€: Okay for apache it’s simply:

    <Directory />
            Options +FollowSymlinks

as given in the recommended nextcloud.conf.

Is there something that needs to be done for nginx?

€: Howto done: Howto: Change / Move data directory after installation
Input/Fixes welcome, and I will add the steps for mysql later and perhaps steps for nginx and other databases.


But be aware, symlinks are handled like they are in your webroot.
There is no difference between:

  1. /var/www/nextcloud/data/yourSecretFile
  2. /var/www/nextcloud/[symlink to /your/new/nextcloud/datadirectory]/yourSecretFile

There is no need to move your /data directory out of your webroot, if you symlink it.

Though that doesn’t matter if your move is based on storage requirements rather than security.