[Tutorial] Nextcloud on WSL2 (Windows 10 20H2)

Hi Community!

I did spent days now researching the web on how to run WSL2 with Nextcloud and make it accessible for others on my network.
Also I was looking for an way to use my Windows Share as an default Data drive.
Finally I made it and wanted to post my performed steps here for others to save time.
Since this account is brand new I have no trust level and cannot post in the “How To” section.
I am not an Linux expert nor good at coding so if you find any mistakes please inform me and I will try to update the post as fast as I can.
Another issue for new users is that i can only post 4 Links, so at the end of this post you find some links where I removed the “h” from https at the beginning of the link :wink:

I am not responsible for any damage or data loss! You are performing the following steps at your own risk!

How to enable WSL2:
First open an Windows PowerShell as Administrator on your machine.
Enable WSL & Virtual Machine Feature with the following commands.

dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart

dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart

Reboot Windows
Download and run the WSL Update for Windows:

https://wslstorestorage.blob.core.windows.net/wslblob/wsl_update_x64.msi

Open an PowerShell as Administrator and set WSL2 as default Version.

wsl --set-default-version 2

Now you can open up your Windows Store and search for the Linux Distribution you desire (I went with Ubuntu 20.04 LTS for this guide).
For ease of use you can also install the Windows Terminal from the Store.
After you have installed Ubuntu, launch it from the store or from the installed Apps in your start menu.
Set your Username and password as presented by the terminal.

How to setup Nextcloud (incl. LAMP Stack):
Open up the Windows Terminal and add a new Tab with the dropdown for Ubuntu.
First update your installation.

sudo apt-get update
sudo apt-get upgrade

Install the unzip package as it does not come with the WSL Version of Ubuntu.

sudo apt-get install unzip

Switching path to make sure everything we touch during mount process is not bussy.
After that unmount all drives and mount the drive you want again with metadata to be able to proceed afterwards.
Make sure that under Windows the folder you want to mount exists, in my case its the entire D: drive.
Check this also for Ubuntu, if the mountpoint is not created simply run sudo mkdir /mnt/yourfolder to create it.
If you do not set the metadata option for your mounting procedure you may see the "Your directory can be read by others … " error later.

cd /mnt
sudo umount -a
sudo mount -t drvfs D: /mnt/d -o metadata

Now install and start apache.

sudo apt install -y apache2 apache2-utils
sudo service apache2 start

Install and start mariadb (mysql).

sudo apt install mariadb-server mariadb-client
sudo service mysql start

Secure the mysql installation by answering the questions prompted after running the command below.
Typically you want to answer all of this questions with Y except the one for changing the root password.

sudo mysql_secure_installation

Now install PHP.

sudo apt install php7.4 libapache2-mod-php7.4 php7.4-mysql php-common php7.4-cli php7.4-common php7.4-json php7.4-opcache php7.4-readline

Run the PHP modules and restart apache

sudo a2enmod php7.4
sudo service apache2 restart

Now download and unpack Nextcloud.
I set my download path to /tmp and install path to /var/www which I suggest.
Check if there is an newer Version of Nextcloud and adapt the link / command.

wget -P /tmp https://download.nextcloud.com/server/releases/nextcloud-20.0.4.zip
sudo unzip /tmp/nextcloud-20.0.4.zip -d /var/www

Give your www-data user (UserID 33) permissions

sudo chown www-data:www-data /var/www/nextcloud -R

Now startup mysql and create the database and databaseuser.
You may want to change the names and password in the command to your needs.

sudo mysql
create database nextcloud;
create user ncuser@localhost identified by ‘ncpassword’;
grant all privileges on nextcloud.* to ncuser@localhost identified by ‘ncpassword’;
flush privileges;
exit;

Create the nextcloud configuration file

sudo nano /etc/apache2/sites-available/nextcloud.conf

Paste the following entire block into the editor.
Make sure that if you later want nextcloud to respond to other URL than “localhost” to change the value at “ServerName” (for now i leave localhost)
Press CTRL + O to write the changes (accept by pressing enter)
Press CTRL + X to exit

<VirtualHost *:80>
DocumentRoot “/var/www/nextcloud”
ServerName localhost

   ErrorLog ${APACHE_LOG_DIR}/nextcloud.error
   CustomLog ${APACHE_LOG_DIR}/nextcloud.access combined

   <Directory /var/www/nextcloud/>
       Require all granted
       Options FollowSymlinks MultiViews
       AllowOverride All

      <IfModule mod_dav.c>
          Dav off
      </IfModule>

   SetEnv HOME /var/www/nextcloud
   SetEnv HTTP_HOME /var/www/nextcloud
   Satisfy Any

  </Directory>

Enable the configuration and restart apache

sudo a2ensite nextcloud.conf
sudo a2enmod rewrite headers env dir mime setenvif ssl
sudo service apache2 restart

Install PHP Addons

sudo apt install php-imagick php7.4-common php7.4-mysql php7.4-fpm php7.4-gd php7.4-json php7.4-curl php7.4-zip php7.4-xml php7.4-mbstring php7.4-bz2 php7.4-intl php7.4-bcmath php7.4-gmp

Reload apache for changes to take effect

sudo service apache2 reload

Lastly give your www-data user permissions on the Data share to not run into any issues afterwards.
Be aware that after doing so you should not change permissions on the nextcloud folder anymore from windows.
At this point only Linux / Nextcloud is supposed to handle permissions.
All Windows Users that had access at the time doing this will keep their access and can read / write files in all directories.
So maybe change your permission inheritance on windows side and set your permissions as you like before performing this last step!

sudo chown www-data:www-data /mnt/d/nextcloud -R
sudo chmod -R 0770 /mnt/d/nextcloud

At this point you should be able to reach Nextcloud from an Browser on your local machine by typing http://localhost
You need to finish the installation in the interface provided - make sure to enter the correct data path as well as the correct database credentials set before!

How to enable network access:
Install ifconfig at your Ubuntu terminal first.

sudo apt install net-tools

Open an Notepad in Windows and post the following block.
Save the file as .ps1 (PowerShell Skript).
This skript looks up your Linux IP Address from WSL2 together with the Ports you entered (in this case only Port 80 http) and makes it accessible through the network.

$remoteport = bash.exe -c “ifconfig eth0 | grep 'inet '”
$found = $remoteport -match ‘\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}’;

if( $found ){
$remoteport = $matches[0];
} else{
echo “The Script Exited, the ip address of WSL 2 cannot be found”;
exit;
}

#[Ports]

#All the ports you want to forward separated by coma
$ports=@(80);

#[Static ip]
#You can change the addr to your ip config to listen to a specific address
$addr=‘0.0.0.0’;
$ports_a = $ports -join “,”;

#Remove Firewall Exception Rules
iex "Remove-NetFireWallRule -DisplayName ‘WSL 2 Firewall Unlock’ ";

#adding Exception Rules for inbound and outbound Rules
iex “New-NetFireWallRule -DisplayName ‘WSL 2 Firewall Unlock’ -Direction Outbound -LocalPort $ports_a -Action Allow -Protocol TCP”;
iex “New-NetFireWallRule -DisplayName ‘WSL 2 Firewall Unlock’ -Direction Inbound -LocalPort $ports_a -Action Allow -Protocol TCP”;

for( $i = 0; $i -lt $ports.length; $i++ ){
$port = $ports[$i];
iex “netsh interface portproxy delete v4tov4 listenport=$port listenaddress=$addr”;
iex “netsh interface portproxy add v4tov4 listenport=$port listenaddress=$addr connectport=$port connectaddress=$remoteport”;
}

Before you can run the skript you may need to change some policies on your machine (only do this if you get an execution error under windows).
Open an PowerShell as Administrator and run the command.
This allows PowerShell Skripts that were generated locally to be ran.

Set-ExecutionPolicy RemoteSigned

Now you can execute the script.
If you finished with the script and want to change the policy back to default enter the following command.

Set-ExecutionPolicy Default

Now you need to make sure that in the conifg.php and nextcloud.conf the appropriate hostname is given or you will run into errors.
Usually it is the hostname of your Windows Machine you want to put there.

Search for ‘trusted_domains’ => array ( 0 => and put there the domain nextcloud should respond to separated by ,
Press CTRL + O to write the changes (accept by pressing enter)
Press CTRL + X to exit

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

Check if the “ServerName” is set to the same value that you did put into the config.php before
Press CTRL + O to write the changes (accept by pressing enter)
Press CTRL + X to exit

sudo nano /etc/apache2/sites-available/nextcloud.conf

Now your Nextcloud should be available through any Browser on your network by going to http://%pcname% were %pcname% is the name of your computer you did setup nextcloud.
If you want to access the installation from the Web you may need to check official documentation on how to portforward etc.

If thats not the case try to reload and restart your apache service.

sudo service apache2 reload
sudo service apache2 restart

How to reset Nextcloud:
If you want to quickly reset your setup (because you made mistakes like me during the setup) you can perform the following steps.
Be aware that this deletes important entries and cannot be undone!
We do not touch your data and it still should be accessible through windows anyway.
If you want to create an useraccount that existed already before make sure that you either rename or delete (backup files before deleting if you want to keep them) the folder of this user.
Else you will get an error during user creation.
For more details on the commands used read the entire guide as we used almost all of them already!

If you have issues managing files from the Windows side after cleaning up your Ubuntu or Nextcloud installation you need to check the security settings in Windows.
I found it helpful to completly reset ownership, and reset all permissions on the folders and files!

sudo rm -R /var/www/nextcloud
sudo mysql
drop database nextcloud;
create database nextcloud;
grant all privileges on nextcloud.* to ncuser@localhost identified by ‘ncpassword’;
flush privileges;
exit;
wget -P /tmp https://download.nextcloud.com/server/releases/nextcloud-20.0.4.zip
sudo unzip /tmp/nextcloud-20.0.4.zip -d /var/www
sudo chown www-data:www-data /var/www/nextcloud -R
sudo service apache2 reload

Useful stuff:
If you are putting files from within Windows into the Datadirectory of your Nextcloud user they may not appear in nextcloud.
You can run the following command to make nextcloud scan all direcories and detect changes

sudo -u www-data php occ files:scan --all

If you are doing this on an virtual machine like VirtualBox you can use cifs in the fstab to mount your Windows Datadrive with the cifsacl option.
This works and has the same effect as drvfs metadata in WSL - “Data directory can be read by others…” error is bypassed this way.
Be aware that the same limitations as described above apply!
If you want your drives to be automatically mounted in Ubuntu you need to edit the /etc/fstab file.
Before you do this make sure that you actually need it - in WSL2 it worked for me without having to edit it!
Check out the documentation
ttps://wiki.ubuntu.com/MountWindowsSharesPermanently

Here are some other guides I found to be very helpful:
ttps://www.linuxbabe.com/ubuntu/install-lamp-stack-ubuntu-20-04-server-desktop
ttps://www.linuxbabe.com/ubuntu/install-nextcloud-ubuntu-20-04-apache-lamp-stack
ttps://github.com/microsoft/WSL/issues/4150
ttps://docs.microsoft.com/en-us/windows/wsl/install-win10
ttps://docs.microsoft.com/en-us/windows/wsl/install-manual

Experimental PHP Workaround
If you really really want to manage permissions for your nextcloud files from within windows there is an way a colleague of mine found.
You need to edit the corresponding PHP Page to the error “Directory readable by other users… change the permission to 0770…”.
This way you can surpress the Page and still be able to work with nextcloud even though Linux permission may be not correct.
Be aware that this can be an security concern.
However it works for my colleague and he is serving nextcloud this way since a few years now to his family and at home he uses Windows Fileshare to get access to the nextcloud stuff.
Permission is managed in Windows as already mentioned.

Hope this helps some other people out there!

Merry Christmas and stay healthy!

4 Likes

Thank you so much for this. I was able to get nextcloud running on my Windows machine perfectly thanks to these simple instructions. It didn’t take me long to work out the port forwarding and make it available online, either.

However, I’m having trouble getting SSL configured correctly. I generated a certificate using sslforfree.com and ZeroSSL, attempting to follow this guide to install the certificate.

Along the way I had to copy a text file into /var/www/nextcloud/.well-known/pki-validation/ so that the domain validation could be performed, and I copied the certificate.crt and ca_bundle.crt into my /etc/ssl/ folder, along with my private.key into /etc/ssl/private/

I then duplicated my nextcloud.conf like so:

sudo cp /etc/apache2/sites-available/nextcloud.conf /etc/apache2/sites-available/nextcloud-ssl.conf

And modified it to use port 443 and enable the SSL engine, so nextcloud-ssl.conf looks like this:

<VirtualHost *:443>
DocumentRoot "/var/www/nextcloud"
ServerName myserver.domain

   ErrorLog ${APACHE_LOG_DIR}/nextcloud.error
   CustomLog ${APACHE_LOG_DIR}/nextcloud.access combined

   SSLEngine on
   SSLCertificateFile "/etc/ssl/certificate.crt"
   SSLCertificateKeyFile "/etc/ssl/private/private.key"
   SSLCertificateChainFile "/etc/ssl/ca_bundle.crt"

   <Directory /var/www/nextcloud/>
       Require all granted
       Options FollowSymlinks MultiViews
       AllowOverride All

      <IfModule mod_dav.c>
          Dav off
      </IfModule>

   SetEnv HOME /var/www/nextcloud
   SetEnv HTTP_HOME /var/www/nextcloud
   Satisfy Any

  </Directory>
</VirtualHost>

But it hasn’t worked, even after reloading and restarting the service.
HTTP still works fine, but HTTPS won’t connect.
I would appreciate any advice

3 Likes

Hi, first of all thank you very much for this tuto which works very well.
Just one point, in your Screen at the level of the conf file of apache2 it misses the closing of the tag at the end :
Nice day !

I get to

and it gives me the errors
umount: /dev: target is busy.
umount: /: target is busy.

What should I do differently?