Nextcloud 27.1.3 on Ubuntu 22.04

I've been running Nextcloud at home for almost a decade now. It's been invaluable and I don't really think we could easily live without it at this point. The time had come to migrate from my fairly old server to a new one backed by our internal network storage. Here's the story (complete with gotchas & tweaks) that got it running under Ubuntu 22.04.

Not a Snap

After seeing that Nextcloud was presented as part of the Ubuntu install as a "snap" install - I figured, despite some misgivings about automatically installed stuff, to give it a try.

(TL;DR - don't do it!)

At first this appeared to be fantastic. The moment the Ubuntu installer completed - a web browser pointed at the server displayed the new Nextcloud service. This seemed too good to be true... and it was.

Snap, like so many other automagical installers didn't install Nextcloud or it's dependencies in a normal way, but instead burred deep under /var/snap/nextcloud including php, Apache, and miriaDB in a container like format. Which is fine if you don't need to make any changes. But, since Nextcloud seems to need quite a lot of changes to actually function - quickly proved to be entirely untenable. The straw that broke the camel's back in this case was a slew of apparmor profiles that came with the snap and prevented any outbound web connectivity for Nextcloud (more on the outbound web access later) but try as I might, I couldn't find them because, of course, they were not in the standard location. 

At this point I gave up with the snap and started over with a manual install. 

Take 2 (if you want something done right...)

Installing Nextcloud manually following the instructions from the Nextcloud site was straightforward. I had a server up and running in about 15 mins and could hit it with a web browser. 

Here is what that looked like:

sudo apt update && sudo apt upgrade
sudo apt install apache2 mariadb-server libapache2-mod-php php-gd php-mysql php-curl php-mbstring php-intl php-gmp php-bcmath php-xml php-zip
#optional - some security concerns
sudo apt install php-imagick imagemagick

then get the database up and running:

sudo mysql

CREATE USER 'nextcloud'@'localhost' IDENTIFIED BY '<<password>>';
CREATE DATABASE IF NOT EXISTS nextcloud CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
GRANT ALL PRIVILEGES ON nextcloud.* TO 'nextcloud'@'localhost';

And finally download, unpack and move the Nextcloud php code into place:

tar -xjvf latest.tar.bz2

sudo cp -r nextcloud /var/www
sudo chown -R www-data:www-data /var/www/html

Some of the instructions suggest installing this in a sub folder /var/www/nextcloud

I would recommend against this unless you actually need that because you have a lot more configuration to get through if you do it that way.

I diverge from the instructions at this point to add later steps in ahead of time and save some bouncing around.

We need to set up the Nextcloud specific options for apache2:

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

In that file goes the following:

Alias /nextcloud "/var/www/html/"

<Directory /var/www/html/>
 Require all granted
 AllowOverride All
 Options FollowSymLinks MultiViews

 <IfModule mod_dav.c>
   Dav off
 <IfModule mod_headers.c>
     Header always set Strict-Transport-Security "max-age=15552000; includeSubDomains"

Then enable it, followed by a bunch of the other modules you'll need:

sudo a2ensite nextcloud.conf
sudo a2enmod rewrite
sudo a2enmod headers
sudo a2enmod env
sudo a2enmod dir
sudo a2enmod mime
sudo a2enmod ssl
sudo a2ensite default-ssl

Finally reload apache2 

sudo systemctl reload apache2

If you're planning to mount some external folder into /var/www/html/data to serve as your primary file storage for Nextcloud - you should do that now. (Be sure to transfer the files in the provided data folder in the new mount)

At this point you can do to your browser and complete the install there. 

You're not done yet!

Heading over to the admin section of Nextcloud at this point will give you a ton of errors, issues, and other warnings that need to be sorted out. I'm not sure why they don't start off with some of these in the install script docs but here are all the tweaks I needed to add:


Until you do this Nextcloud won't be able to connect outside local host and basically can not function. Why this is not in the config file by default I have no clue!

sudo vi /var/www/html/config/config.php

and add:

'allow_local_remote_servers' => true,

also add (or your country code as applicable):

'default_phone_region' => 'CA',
'htaccess.RewriteBase' => '/',

#Since I run through a reverse proxy I will need these to name the host for it's actual public URL later too and establish trust:
'overwrite.cli.url' => '',
'overwritehost' => '',
'trusted_proxies' => array( 0 => <<ip1>>, 1 => <<ip2>> etc...),


Fix PHP settings:

Set the  memory_limit to 512M via (it will be there with a lower value already):
sudo vi /etc/php/8.1/apache2/php.ini

Nextcloud started complaining about my opcache being too small after the install - but while you're in the php.ini, may as well do this now. Uncomment the following line and double the cache size (default is 8) time will tell if that is a large enough setting.



Setup apcu cache:

sudo apt-get install php-apcu


sudo vi /var/www/html/config/config.php

and add

'memcache.local' => '\OC\Memcache\APCu',

and update for that change.

sudo systemctl restart apache2


Improve caching with Redis

This is optional, but why not really since it is so easy:

sudo apt-get install redis-server php-redis

then again

sudo vi /var/www/html/config/config.php

and add:
'memcache.locking' => '\OC\Memcache\Redis',
'memcache.distributed' => '\OC\Memcache\Redis',
'redis' => [
  'host'     => '/var/run/redis/redis-server.sock',
  'port'     => 0,

You need to enable the redis-server.sock socket listener:

sudo vi /etc/redis/redis.conf

#============= Un-comment the 2 lines below:
unixsocket /var/run/redis/redis-server.sock
unixsocketperm 770
#the line above is the umask for perms of the .sock - change to 770 from 700

Finally add the www-data user to the redis group

sudo usermod -a -G redis www-data


Add the required cron for the www-data user:

sudo crontab -e -u www-data

and add
*/5  *  *  *  * php -f /var/www/html/cron.php --define apc.enable_cli=1

The last bit enables the apcu cache for php cli which it won't be by default, causing the cron to fail.


One final note. If you are migrating from an old Nextcloud like me. DO NOT point the desktop client's connection with the new nextcloud to the same folder in your local PC as the old one. Unless you want to delete everything there cough PSA: Backups rock!