Installing Nextcloud on WSL2

Nextcloud requires Apache server, PHP and MariaDB to run. You can install the required packages using the Linux package manager in WSL2. This guide assumes that you have Ubuntu installed on WSL2 but the steps are the same for other distros, only the installation commands will differ.

  1. Install the required LAMP packages mentioned at Nextcloud installation docs. The required packages can be installed with:

    1
    2
    3
    4
    
    sudo apt update
    sudo apt install apache2 mariadb-server libapache2-mod-php7.4
    sudo apt install php7.4-gd php7.4-mysql php7.4-curl php7.4-mbstring php7.4-intl
    sudo apt install php7.4-gmp php7.4-bcmath php-imagick php7.4-xml php7.4-zip
    
  2. Start Apache and MariaDB (MySQL) with:

    1
    2
    
    sudo service apache2 start
    sudo /etc/init.d/mysql start
    
  3. Now that you have MariaDB running, create a database and a database user. In the example code below replace username and nextcloud with your preferred database username and database name respectively.

    1
    2
    3
    4
    
    CREATE USER 'username'@'localhost' IDENTIFIED BY 'password';
    CREATE DATABASE IF NOT EXISTS nextcloud CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
    GRANT ALL PRIVILEGES ON nextcloud.* TO 'username'@'localhost';
    FLUSH PRIVILEGES;
    
  4. Download the Nextcloud Server files from the Nextcloud Install page

  5. Copy to the downloaded files to /var/www/nc. You can change the folder name nc to your preferred name. This will be the folder in which Nextcloud is installed.

  6. After copying the files, change owner of the folder to Apache user with chown -R www-data:www-data nc

  7. Create an Apache configuration file for the Nextcloud website to run. Create a file called nextcloud.conf in the /etc/apache2/sites-available/ folder with the following contents. In the config below, the Alias /nextcloud can be changed to a different name at which you want to access the site. With the alias in the below example, you can access the site at http://localhost/nextcloud. The /var/www/nc is the folder containing the Nextcloud files. This should be the same as the folder you created in Step 5 above.

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    
    Alias /nextcloud "/var/www/nc/"
    <Directory /var/www/nextcloud/>
    Require all granted
    AllowOverride All
    Options FollowSymLinks MultiViews
    
    <IfModule mod_dav.c>
        Dav off
    </IfModule>
    </Directory>
    

    For location of the Apache config files in other distros see the Nextcloud docs

  8. Enable nextcloud.conf by running the command below. This will essentially copy the .conf file to /etc/apache2/sites-enabled.

    1
    
    sudo a2ensite nextcloud.conf
    
  9. Enable other required apache modules

    1
    2
    3
    4
    5
    
    sudo a2enmod rewrite
    sudo a2enmod headers
    sudo a2enmod env
    sudo a2enmod dir
    sudo a2enmod mime
    
  10. You have now set up all the requirements for Nextcloud server. You can proceed to install. You will need the IP address of WSL2. You can get this with the command ip addr. Use the IP address mentioned at eth0. Usually, this IP will start with 192.x.x.x or 172.x.x.x. To confirm if you have the right address, access the IP that you see by navigating to http://192.x.x.x (or http://172.x.x.x) in your browser. If you see the Apache start page, then you’re at the right place.

  11. Now, in your browser, go to the location at which you’ve copied the Nextcloud files in Step 5. In this example, it will be at http://192.x.x.x/nc. You will see a Nextcloud install page where you can enter details (Step 3) to proceed with installation.

    • Enter a new admin user for Nextcloud and the database details from Step 3.
    • Take care in picking the data directory. You’ll have to reinstall Nextcloud if you pick the wrong folder. Be sure that the folder you pick contains enough space.
    • Change the owner of data directory to www-data
    • If you enabled apache’s SSL as mentioned in the Nextcloud docs link above, then trying to setup using http://192.x.x.x/nc will fail. You need to use https://192.x.x.x/nc
    • After entering the details and proceeding with installation, you might have an error saying “Server not found” at the “Installing recommended apps” stage. You can safely ignore this and navigate to the Nextcloud homepage (http://192.x.x.x/nc) and you should be able to login.
    • If you pick a data directory outside WSL2, you will most likely get an error that “Data directory is not writeable. Change permissions to 0770” when accessing Nextcloud. This is because www-data can’t own folders outside WSL2, i.e., it can’t own folders in Windows. To fix this, add 'check_data_directory_permissions' => false, to /var/www/nc/config/config.php
  12. Your Nextcloud is now installed and available at http://192.x.x.x/nc. You can login using the username & password you had entered in Step 11.

  13. You can access Nextcloud from any device connected to your local network using the http://wsl2-ip/nextcloud-foldername (e.g., http://192.x.x.x/nc).

  14. Your WSL2 IP needs to be added to the trusted_domains in Nextcloud’s config file to be able to access the site. After initial install, this IP should normally be added already. If you restart your PC, you WSL2 IP is likely to change. To ensure that the site is accessible after restart, set the config such that trusted domains include IPs starting with 192 or 172. To do this, edit Nextcloud’s config at /var/www/nc/config/config.php

    1
    2
    3
    4
    5
    6
    7
    
    sudo nano /var/www/nc/config/config.php
    # Edit the file to include the following lines:
    'trusted_domains' =>
    array (
        0 => '172.*.*.*',
        1 => '192.*.*.*.',
    ),
    

Access Nextcloud anywhere with Tailscale

Nextcloud is now accessible from your local network. To access your Nextcloud externally, we can use Tailscale which lets you create your own private VPN and provides a static IP. You can get install instructions for Tailscale here. Any device connected to your Tailscale VPN can access the other devices on your VPN using the IP.

Once you’ve Tailscale installed and running, get your WSL2’s Tailscale IP from the Tailscale admin area. Add this IP to the trusted_domains section of your Nextcloud’s config.php. If you’ve setup MagicDNS in Tailscale, you can also add your WSL2 instance’s hostname to the Nextcloud config file. Your config file should contain lines similar to below:

1
2
3
4
5
6
7
'trusted_domains' =>
    array (
        0 => '172.*.*.*',
        1 => '192.*.*.*.',
        2 => '100.1.2.3', // Replace this with your WSL2's Tailscale IP
        3 => 'my-wsl-hostname' // Replace this with your WSL2's Tailscale hostname
    ),

Now you can access your Nextcloud at http://100.1.2.3 or at http://my-wsl-hostname from any device that is connected to your Tailscale VPN.

Troubleshooting

  • If you’re unable to access Nextcloud, ensure that Apache & MySQL are running.
  • If you’re unable to access Nextcloud at the Tailscale IP, check that Tailscale is up using tailscale status. Remember that Tailscale running on WSL2 doesn’t mean that Windows is connected to Tailscale. Use the Tailscale desktop client for Windows to check that you’re connected to Tailscale VPN.
  • After logging into Nextcloud, you get a curl error when trying to access Nextcloud Apps page. This is usually due to DNS resolution errors where you Nextcloud install is unable to the Nextcloud Apps server. To solve:

Keeping changes up-to-date

If you’re accessing your files using a Nextcloud client on another devices (e.g., desktop client or Android app), the changes done to your files or new files added may not be immediately reflected when accessed from the client apps. To ensure that the changes are picked-up by Nextcloud, run:

1
2
3
sudo -u www-data php /var/www/nc/occ files:scan adminuser
# Replace '/var/www/nc' with your Nextcloud install location
# Replace 'adminuser' with your Nextcloud admin's username

Putting it all together

If you’ve installed Tailscale on WSL2 along with Nextcloud, follow these steps to have everything up and running.

1
2
3
4
5
6
7
8
9
# In WSL2, start Tailscale with
sudo tailscaled
sudo tailscale up
# Start Apache for Nextcloud with
sudo service apache2 start
sudo /etc/init.d/mysql start
# Connect to Tailscale from Windows, so you can access Nextcloud using Tailscale IP or device-name
# Access Nextcloud at http://wsl2-tailscale-hostname/nextcloud-foldername


Related Posts: