Once Nextcloud is installed on the host the next task is to activate the database and administration user and then work through the initial configuration checklist for things like email notifications, some PHP tuning and then making the server accessible to external users via HAproxy.
The Nextcloud installation was covered in Test Nextcloud 26 Installation. As a recap this is a new Nextcloud 26.0.0 server running on Ubuntu 20.04 LTS, Apache 2, MariaDB 10.6 and PHP 8.1.
Contents
This is all based on the official configuration guide which can be found here:
https://docs.nextcloud.com/server/26/admin_manual/configuration_server/index.html
The first part of the configuration is done by connecting directly to the host to avoid any complications with the proxy. Then the proxy was brought into play using an internal TLS certificate which works perfectly well for us as we all have my internal CA Root certificate installed. The last step was to upgrade the proxy to use a Lets Encrypt certificate since this system will be used by others.
Initial Connection
We did a quick connection check at the end of the installation process, now it's time to run the installer to create the database and prepare Nextcloud for use. Open a browser and connect to the server, making sure to specify this is an HTTP connection, e.g. http://testcloud.public.example.com

This initial admin user can have any name. Given this will be an internet facing site, don't use something obvious like admin and pick a long password as there's no point in making it too easy for the drive-by hacking attempts that are inevitable. Although there are some things we can build in to help with that particular problem too.
We want it to use the /srv/nextcloud/data directory we created for documents and other data and it helpfully finds and suggests this itself which is a nice touch.
The last thing to configure is the database using the settings from when we created it and which you have written down somewhere safe. When you're ready click the Install button and it'll load the database content and after a few seconds offer to install some recommended applications.

I always skip this, in this case it's because:
- They don't really know what they want to use beyond group folders
- I'm definitely not going to be setting up a mail service for them
- If they decide Talk is useful I'll do that as a sandbox first and might well run it on a dedicated server.
And that's it. We now have a running Nextcloud server. In practice there's some initial configuration required before I start letting the users in.
System Configuration
Nextcloud provides a handy list of the most important configuration items. This is updates as you add applications so it's worth checking regularly, especially while you're getting things established.
Apart from the Administration Settings menu there are two key locations for making configuration changes:
- The config.php file in the /srv/nextcloud/www/config directory. Changes made here take effect as soon as you save.
- The FPM php.ini file in the /etc/php/8.1/fpm directory. Changes made here take effect after restarting the php8.1-fpm service
- The CLI php.ini file in the /etc/php/8.1/cli directory. Changes made here take effect the next time calls the CLI after you save.
The locations shown are defaults for the install I've done, you may find the active php.ini file is in a different location if you've done your own thing.
Administrator Profile
But the first thing to do is to setup your Administrator profile. I suggest you keep this as an admin and separate from your regular user on the system that you'll use for doing regular user things.
At the top of the page is the Nextcloud Menu (this pic is taken after I'd named the initial user). If you click on the Settings icon in the top right corner of the screen - the one with a green dot and with "TA" on it in this example - the first item on the menu that pops up will be View Profile. Click on it and you can set the full name of your admin user and you should also provide at least one email address too. The rest of the settings are optional.
My advice is to keep this admin user secure - we'll protect it with two-factor authentication too - and create other users for routine administration.
Security & Setup Warnings
Go back to the Settings menu and this time pick Administration settings from the menu. This will open at the Overview screen which will have a long list of warnings and recommendations. Some of them I ignore as I really don't care about php-imagick not having SVG support or that gmp and bcmath are not enabled or that I'm not setting up HSTS.
The big red warning about HTTP can also be ignored for now since that'll be configured later. After some time in operation you'll likely see additional warnings appear, such as PHP OPcache tuning.
Notification mail server
Nextcloud uses email notification for things like creating new user, as an option for password resets, alerts of patches and updates etc so getting connected to a mail server is an important task.
Go to Administration > Basic Settings > Email Server
The only supported method is SMTP and the connection settings will depend on your mail setup. This is a send-only connection so it'll be polite to make the sender address include noreply or similar, e.g
Check the email address you added to your administrator profile to verify it working and claim a first victory.
Since Nextcloud is in a DMZ along with a proxy that also handles access to a mail server, I figured rather than opening up a firewall port for direct access I'll route mail sending through the existing SMTPS proxy which I know works. It took me longer than I'd like to admit to get this working, thinking it was some sort of TLS handshake problem as I use internal certificates for the mail server, and there's precious little clue from Nextcloud apart from "computer says no" if it has a problem.
The actual problem was I had used the IP address of the proxy as the mail server address and immediately got blacklisted by fail2ban as the proxy was configured to only permit the correct hostname for the mail server - all part of the functioning of a TLS connection. Adding the fully-qualified hostname of the mail server as the proxy wanted along with the IP address of the proxy to the hosts file of the Nextcloud server meant I could use mail server hostname in the for the Server address and a quick fail2ban-client unban command and it worked nicely. Fail2ban is a very useful tool, but it repeatedly pished me off during this exercise.
PHP Memory Limit
The default 128 MB PHP memory limit is too low for Nextcloud. Edit the FPM php.ini file and change the memory_limit parameter to 512 MB. You'll find it around line 436.
; Maximum amount of memory a script may consume
; https://php.net/memory-limit
;memory_limit = 128M
memory_limit = 512M
You will need to be root to edit the file, restart PHP with systemctl restart php8.1-fpm to make it take effect. The warning should disappear from Nextcloud after you refresh the page.
Default Phone Region
This helps with working out phone numbers which may be relevant if Contact lists are activated. Edit the config.php and add default_phone_region setting to the end of the settings
[other stuff]
'default_phone_region' => 'GB',
);
Save the file and it'll take immediate effect. Don't quit as we've got more to add here
Configure a Memory Cache
PHP can use a cache to speed up script processing. Since we're on a single server the APCu cache is sufficient and it's also installed by default so we nee to do is activate ate it in config.php by adding a memcache.local entry
[other stuff]
'default_phone_region' => 'GB',
'memcache.local' => '\\OC\\Memcache\\APCu',
);
Save the file and it'll take immediate effect. After a while we'll start to get warnings about cache size so we'll come back to this again for tuning.
Background Jobs
Nextcloud needs to trigger internal processes and provides three different ways to doing this, defaulting to using normal request processing to trigger actions. Since we're using a dedicated server we can use the cron method, or given that it's 2023 we can use the vastly superior and much loved systemd instead. Flame bait? Moi?
This needs root access as we need to create two unit configuration files in /etc/systemd/system, set their permissions to 644 root:root and reload the config. We then need to update the PHP CLI config before activating the units. The first file is nextcloud-cron.service
[Unit] Description=Nextcloud cron.php job [Service] User=www-data ExecStart=/usr/bin/php -f /srv/nextcloud/www/cron.php KillMode=process
The second file is nextcloud-cron.timer
[Unit] Description=Run Nextcloud cron.php every 5 minutes [Timer] OnBootSec=5min OnUnitActiveSec=5min Unit=nextcloud-cron.service [Install] WantedBy=timers.target
Reload the systemd configuration.
systemctl daemon-reload
We also need to enable APCu cache in the PHP CLI too otherwise jobs might fail. Edit the CLI php.ini file and add the following near the start
;;;;;;;;;;;;;;;;;;; ; Nextcloud Config ; Enable APCu for cron.php triggered by nextcloud-cron.timer ; apc.enable_cli = 1
Save the file and we can now activate and start our systemd units.
systemctl enable nextcloud-cron.service
systemctl enable nextcloud-cron.timer
systemctl start nextcloud-cron.timer
systemctl status nextcloud-cron.service
systemctl status nextcloud-cron.timer
Assuming you've got everything right and you didn't get any errors from the enable or start command you should see the following from the status:
Nextcloud-cron.service should be loaded but inactive (dead) since less than 5 minutes ago and triggered by nextcloud-cron.timer
Nextcloud-cron.timer shold be loaded, active (waiting) and trigger should have less than 5 minutes left until it triggers nextcloud-cron.service
root@host:/srv/nextcloud/www/config# systemctl status nextcloud-cron.service
● nextcloud-cron.service - Nextcloud cron.php job
Loaded: loaded (/etc/systemd/system/nextcloud-cron.service; static; vendor preset: enabled)
Active: inactive (dead) since Tue 2023-04-18 23:28:58 BST; 2min 49s ago
TriggeredBy: ● nextcloud-cron.timer
Process: 90836 ExecStart=/usr/bin/php -f /srv/nextcloud/www/cron.php (code=exited, status=0/SUCCESS)
Main PID: 90836 (code=exited, status=0/SUCCESS)
Apr 18 23:28:58 host systemd[1]: Started Nextcloud cron.php job.
Apr 18 23:28:58 host systemd[1]: nextcloud-cron.service: Succeeded.
root@host:/srv/nextcloud/www/config# systemctl status nextcloud-cron.timer
● nextcloud-cron.timer - Run Nextcloud cron.php every 5 minutes
Loaded: loaded (/etc/systemd/system/nextcloud-cron.timer; enabled; vendor preset: enabled)
Active: active (waiting) since Tue 2023-04-11 20:27:46 BST; 1 weeks 0 days ago
Trigger: Tue 2023-04-18 23:33:58 BST; 2min 5s left
Triggers: ● nextcloud-cron.service
Apr 11 20:27:46 host systemd[1]: Started Run Nextcloud cron.php every 5 minutes.
The 1 weeks ago on the Active line for the timer refers to when the timer unit was started, not when it last triggered. Yes, it takes me a little while to catch up the documentation.
If you check back on Nextcloud Administration > Basic Settings : Background Jobs should show it's now using Cron as well as when it last ran. You might need to refresh the page if you had it open while doing configuring the service.
External Access - Phase 1
Note: As you've probably noticed, the hostnames in this documentation all refer to example.com as this is an established good practice and that name is officially reserved for use in documentation anyway. It also means you're unlikely to cause any problems to others if you accidentally copy/paste direct into your own configuration. Obviously in real life I used a different domain name.
The simple way to enable external access when you're hosting on a GeekLab server, and assuming you have more than the simplest home router is to just set up forwarding from TCP port 443 from the public IP the router to TCP port 443 on your target server. This being a GeekLab and I already have some public services it means it's not quite so simple.
As I mentioned in the Mail Server section above, and as shown in Nextcloud for Beaver Scout activity planning I have a proxy that handles all external access and forwards requests to the appropriate server. This lets me have many different sites, each with their own name and potentially on many different hosts, all appearing to be on a single public IP address. It also adds a little bit of complexity and in the case of Nextcloud some specific configuration to make it work via a proxy.
And just to add to the fun, internal users accessing Nextcloud will be doing so over internal IP addresses so I also need to have separate DNS configuration for internal and external users, also known as split horizon DNS so everyone can use the same hostnames regardless of where they access from.
Despite the complications this actually gives us a lot of future proofing. We will create a new logical hostname for our test Nextcloud instance, which I'll call nc.test.example.com here, and the proxy and DNS configurations will make sure that requests always reach our test Nextcloud installation regardless of the actual server name it has now, or any future upgrades and replacements.
Nextcloud Configuration for Proxy
Nextcloud does some clever stuff to generate the internal URLs that link all the pages, buttons and images together, and it does the mostly from the URL you use to connect to it. It also knows the hostname it's installed on by asking the OS. We're now going to start messing with this and using a different hostname to what it was installed on, and not accessing directly either. While it still mostly gets it right at first, we need to give it some hints to get it right all the time.
We need to make two more edits to config.php. Add the logical hostname to trusted_domain, which is an array variable so add the 1=> too
'trusted_domains' =>
array (
0 => 'testcloud.public.example.com',
1 => 'nc.test.example.com',
),
We also need to permit access from the proxy by adding a trusted_proxies entry too, also structured as an array so you need the arrows and specify it as the actual IP address of the interface on the proxy that will connect to Nextcloud.
'trusted_proxies' =>
array (
0 => '192.168.4.8',
),
We'll need to make a further update once we switch to HTTPS but cannot do that yet. Save the file so it take effect, and it's worth doing a quick connection check to the login page to be sure you're not broken anything yet.
Proxy Configuration
With the benefit of hindsight this can be done in one step, but since this is an account of the process I went through I'll detail each step as it makes it easier to understand and test.
The first step is to add the chosen logical hostname to internal DNS. I have DNS capability on the router itself and also on an internal Windows/Samba domain. I decided to use the router to provide the internal view since it also controls the public subnet address allocation and I already have the a LAN DNS record for the Nextcloud host too. It's just a case of adding a CNAME to it.
To prove this is working, simply open a browser using the logical hostname e.g. http://nc.test.example.com and as soon as you get the Nextcloud login prompt you've won.
The next step is to make this work on the proxy. And to be safe, I really mean make it work on a test version of the proxy first so as not to disturb other sites I have on there. It took less than 10 minutes to start the test proxy VM and bring it up to date and to recreate the LAN DNS config for it in the router so well worth doing.
Ubuntu 20.04 LTS provides HAproxy 2.0.29 which isn't the latest, but it's sufficient. The HAproxy configuration is split into a front-end which decides if a request is permitted based on things like hostname and path and a backend which sends it whichever host(s) it needs to get to.
Condensing down to just the configuration for this site only, we can configure for HTTP quite simply with
frontend http
bind *:80
mode http
option forwardfor
http-request capture hdr(host) len 64
acl host_nctest hdr(host) -i nc.test.example.com
use_backend http-nctest if host_nctest
backend http-nctest
# Nextcloud test server
server nttp-nextcloud-test1 192.168.1.42:80 check
This configuration is for an HTTP connection only and capture the HTTP Host Header which specifies which host the browser wants to connect to. It then checks if the requested host header is nc.test.example.com, and if it is then use the http-nctest backend to connect to the host using the IP address 192.168.1.42 on port 80 and to check it's healthy.
If you configured the status monitor in HAproxy and open that in another brower window you should be able to see the frontend and backend connections..

If we then remove the CNAME from the NCtest profile on the router and add it as a CNAME on a LAN/DNS entry for the proxy instead we should still get a connection to Nextcloud.

Again at this point just getting the login screen means you've won, although logging in an making sure things look OK is a good idea just to be sure.
There are two potential issues if this doesn't work at first:
- If your browser / OS caches the address of the CNAME from the first test it will still try connecting directly. Check with a ping nc.test.example.com to see if it's resolving to the IP of the proxy or directly to the Nextcloud. On a Linux Mint desktop you can force it to flush the cache with resolvectl flush-caches so it'll pick up the new address.nc
- Make sure you've permitted access from your internal subnet to the proxy host on TCP port 80 too - both on your router filewall and on any iptables / application firewall on your proxy host.
External access test - HTTP
At this point I wasn't permitting any external HTTP access so it was easy to do a quick connection check without disrupting anything. I created a temporary DNS A record using the same alias domain name on the router (although using the real domain, not example.com) and with the address set to the external IP of the router. Since this is just temporary TTL is set to 300 seconds.
The router configuration is in two parts, a NAT port mapping and an associated firewall rule, both permitting access to the HAproxy interface in the public subnet.
On the router navigate to NAT > Open Ports and create a new profile. We want to allow access from any source and the destination is the HAproxy host and we just allow TCP and set both start and end to port 80.

The firewall config is very similar, go to Firewall > Filter Setup and pick the appropriate filter set. I have one dedicated to incoming connections to help organise things. The direction needs to be set to WAN -> LAN/DMZ/RT/VPN, with a source address of Any and the Destination IP set to the HAproxy host, and this must be the same address as specified in the NAT rule. I have previously defined port 80/tcp as a service under Object Setting > Service Type Object, otherwise Service Type needs to be set to Protocol = TCP, Source Port = 1 ~ 65535 and Destination Port = 80 ~ 80. The Application : Filter needs to be set to Pass Immediately to permit the connections.
The Draytek firewall is stateful and always permits related traffic such as replies without any other config.

Testing this requires something connected to the internet via some other method, the easy solution is to put a mobile phone into mobile data mode and try to access the site on the alias address, being careful to specify it's http://nc.test.example.com (or preferably the real address you're using).
As soon as the browser shows the login page that proves success. As this is an unencrypted connection we definitely do not want to attempt a login.
Now that we've proved it works, delete the NAT port and firewall rule from the router as not only are we finished with them, I'll need to use port 80 again later. I also deleted the public DNS record too as it was just for testing.
Internal Proxy with HTTPS termination
To enable HTTPS connection you need to install certificates on the servers. As all the servers and devices are under my control, and to a much lesser (and declining) extent the users are too, I've been able to use an internal TLS root certificate authority and insist my users install our root certificate on their devices.
The Internal CA setup will be written up at some point, but in brief I use the XCA v1 application to generate certificates since it's much easier than mucking about with OpenSSL. As is best practice, I have a two tier configuration for TLS certificates, which means I have the Root Certificate Authority which generates the internal Root Certificate and is also used to sign intermediate Certificate Authorities which actually issue the certificates to servers and devices. As this is a test system, I use the certificats from my Test intermediate.
Generating the certificate only takes a few minutes. Since this is entirely internal, a test system and a temporary certificate I can cheat a little and create both the public and private keys in XCA. For the certificate, the fully-qualified domain name is used for both the Subject CN field and also the Subject Alternate Name DNS field, and signed by my Test Intermediate CA key.
The private key is exported to a .PEM file without a password, and the full public certificate chain is also exported as a .PEM file as well and then verified by opening the certs on my desktop and confirming they are indeed an RSA private key, and the certificate file contains the root, intermediate and host certificate together in a single file with the correct hostname.
HAproxy wants all of this in a single file though. So from a command prompt, cd into whichever directory you've placed the files and join them with a cat command, substituting the nctest-chain.pem and nctest-key.pem for the names you chose when exporting.
cat nctest-chain.pem nctest-key.pem > nctest.pem
The nctest.pem file is then uploaded to the HAproxy host and placed in the /etc/ssl/haproxy directory with permissions set to 640 root:root since it contains the private key and shouldn't be readable by anyone else. Also make sure you delete all other downloaded copies of the key files too.
Finally we need to return to the HAproxy configuration and add a new section to the /etc/haproxy/haproxy.cfg file.
frontend HTTPS
# Terminates TLS - finds required cert(s) in the haproxy dir.
bind *:443 ssl crt /etc/ssl/haproxy/
mode http
option forwardfor
http-request capture hdr(host) len 64
acl host_nctest hdr(host) -i nc.test.example.com
use_backend http-nctest if host_nctest
The key line part here is the bind line. We're telling HAproxy to listen on the standard HTTPS port 443 and that it's a ssl connection (yes, they're using the old name for what is now TLS) and to search the /etc/ssl/haproxy directory for certificates and keys to accept the connection. At this point it's no longer a HTTPS connection so we can handle this in exactly the same way as the HTTP connection above by looking for the host header and using that to decide which backend to use.
backend http-nctest
# Nextcloud test server
server nttp-nextcloud-test1 192.168.1.42:80 check
There's no change to the backend connection at all as this will use HTTP, at least untill something complains.
Restart the HAproxy service with systemctl restart haproxy and then connect to the logical hostname as an HTTPS connection e.g. https://nc.test.example.com and you should now get the Nextcloud login page with a nice little padlock icon next to it.
Although I didn't realise at the time, Nextcloud was probably still generating internal URLs using HTTP instead of HTTPS, but the proxy still had the HTTP frontend defined so it handled it just fine. That soon changed...
External Proxy with HTTPS termination - internal CA
I can't redirect from incoming HTTPS connections from the live proxy for testing since it's in use for other services, but on the basis that I already know that it's handling HTTPS traffic all I need to do is move the configuration to it. And since it's a GeekLab with no SLA if it breaks then my users will have to bring me beer to fix it. I mean, I will apologise profusely and promise to be more careful next time, especially if I want my domestic bliss to continue.
We can use the same certificate file so we just need to copy that to /etc/ssl/haproxy on the live proxy and set the permissions as before: 640 root:root
Of course we'll make a backup of the existing /etc/haproxy/haproxy.cfg file before editing it, just in case something bad happens.
- The test Nextcloud server is a new backend so copy the entire backend configuation to the live proxy config.
- For the frontend we just want to copy the acl host_nctest and use_backend http_nctest lines into the existing frontend HTTP section
Restart HAproxy with systemctl restart haproxy and then check it's OK - it'll help to open the status page in a browser too.
We now need to make this live for both internal and then external access. Starting with internal access all I had to do was go back into the router LAN/DNS profile for the test proxy and delete the CNAME for nc.test.example.com and then go to the profile for the live proxy and add the CNAME record there. After a quick DNS cache flush and a ping nc.test.example.com to make sure it was resolving to the live proxy now open a browser to https://nc.test.example.com expecting to see the Nextcloud login and all I got was errors and then timeout messages from the browser as yet again fail2ban bit me.
Remember the section above about configuring Nextcloud to work with a proxy? There's one final step needed, and that's to force Nextcloud itself to use HTTPS and the logical hostname for all the internal URLs it generates. Edit the config.php file and add the following overrides:
[other stuff]
'overwriteprotocol' => 'https',
'overwritehost' => 'nc.test.example.com',
);
After a fail2ban-client unban [my IP] and with Nextcloud now making sure all URLs are to the correct host and using HTTPS it worked perfectly.
To enable external access I again need a to create DNS records in the public DNS, this time being permanent. My broadband connection has a static IP address which makes means I don't have to muck about with dynamic DNS updates, but as I can see there being a few servers being configured for external access so time to get a bit more creative.
Hostname TTL Type Data
geeklab 3600 A 192.2.0.17
nc.test 3600 CNAME geeklab
This is not a full bind config, but an extract from the Mythic Beasts DNS console. The geeklab could be anything, it exists just to provide an Address record for the public IP of my router. If that ever changes, I now have just one record to update. The nc.test is a CNAME or alias and points to the geeklab record so any requests for nc.test.example.com will get the geeklab address. When I add the production system I just have to add extra CNAME record for that.
As an aside, this also lets me lay some traps with fail2ban too - anyone tying to connect to services using the IP address or geeklab.example.com will get an instant ban since I won't have real hosts directly on there. I could even seed a few trap CNAME records to do the same too - try to access anything on them and you also get banned.
With the public DNS now updated as well I can check access with the mobile and prove I get a connection, and now it's using HTTPS it's safe to login and do a proper test. Providing I'm stood by the windows and holding the phone just right.
External Access with LetsEncrypt Certificate
Using my internal CA is great for our own use, but this server will be used by others who might struggle with importing a "user" root CA certificate or even refuse outright. I don't really want to get into buying TLS certificates either, especially for a test server that might be abandoned after a few weeks. This is where LetsEncrypt are so useful as I can have a public CA certificate that's accepted by all browsers, and it's free too.
To make this work with HAproxy requires the installation of certbot and some scripting to make the certificates work with HAproxy. Since this is not directly related to Nextcloud I'll walk though that in HAproxy with LetsEncrypt certificate instead.
Enable Admin Applications
I've enabled a couple of admin applications for now, and as part of the testing I'll be evaluating a few more like 2-factor authentication, announcement center, a Deck board for config activity - and I'm sure it'll be useful for their activity planning too. I also have ClamAV integration on my to-do list as well since they're looking to exchange documents
Brute Force Protection
This app has very little configuration. If a user makes an excessive number of password attempts it introduces a delay before responding to slow things down. Assuming that fail2ban doesn't pick anything from the logs and ban them.
Terms of Service
This lets you display a message to all users when they first log in, and again on any changes if you wish. I've set it to give a very brief summary of what the test system is to be used for, a reminder there's no SLA so if it breaks they'll have to wait until I get a free moment and also to keep within their guidelines and GDPR with respect to not using real personal data here.
Next Steps
Now we're ready to let the users into have a first look at Nextcloud and start looking at what they'd like to turn activate and more importantly start thinking about ways to use it effectively. It also gives me a chance to have a little play too - in fact it looks like I'm going to have to run up a sandbox instance now as to work out how to make Nextcloud Office play nicely with the proxy.
Next section: Functional Configuration (to be continued....)