Deploy Nextcloud on a VM

This guide describe the steps to deploy and configure a Nextcloud instance on a Ubuntu 20.04.

Create VM with Ubuntu 20.04

To create a VM on OpenStack, you can follow this guide: https://cloud.garr.it/compute/quick-vm/

Note

When you are in the “Select Boot Source” step, choice Ubuntu 20.04 image.

Create Security Group to open SSH, HTTP and HTTPS ports.

Attach a Nextcloud data volume to VM

Now we want to create and attach a secondary volume to VM for Nextcloud data. To create and attach a new volume you can follow this steps:

  • login to the OpenStack dashboard: https://dashboard.cloud.garr.it

  • create a secondary volume: from the left menù “Volumes -> Volumes -> Create Volume”

Note

When you create a volume you must select the “Capacity” type.

  • attach the new volume to VM: from volume menù, click on “Manage Attachments” and select the VM

Now, mount volume on VM:

  • SSH access to the VM:

    $ ssh -i .ssh/<ssh-key> ubuntu@<ip-vm>
    
  • root user:

    $ sudo su -
    
  • check the /dev/vdb exist:

    $ fdisk -l
    
  • create partition, after command follow this step -> “gpt - new (enter) - write (yes) - quit”:

    $ cfdisk /dev/vdb
    
  • check partition:

    $ fdisk -l
    
  • format partition:

    $ mkfs.ext4 /dev/vdb1
    
  • create Nextcloud diectory:

    $ mkdir /media/nextcloud
    
  • mount partition on Nextcloud directory:

    $ mount /dev/vdb1 /media/nextcloud
    
  • check mount:

    $ df -h
    
  • save the informations needed to mount partiotion -> add this row “/dev/vdb1 /media/nextcloud ext4 defaults 0 0”:

    $ vi /etc/fstab
    
  • reboot VM:

    $ reboot
    

Nextcloud installation

  • SSH access to the VM:

    $ ssh -i .ssh/<ssh-key> ubuntu@<ip-vm>
    
  • root user:

    $ sudo su -
    
  • installation:

    $ snap install nextcloud
    
  • check installation:

    $ snap get nextcloud php
    $ snap services nextcloud
    $ snap connect nextcloud:removable-media
    

Note

It is possible to maintain the ‘php.memory-limit’ value to 512M.

  • additional useful commands to Nextcloud installation info:

    $ snap changes nextcloud
    $ snap info nextcloud
    $ snap connections nextcloud
    

Create admin user

You can create the administrator user from the Nextcloud web interface. By entering the public IP of the VM, the web interface is accessed and the admin credentials can be configured. In this case you are not using a secure connection, it is better to configure the admin user via CLI as follows:

  • configure admin user:

    $ nextcloud.manual-install admin <password>
    
  • check trusted domain:

    $ nextcloud.occ config:system:get trusted_domains
    
  • add trusted domain:

    $ nextcloud.occ config:system:set trusted_domains 1 --value=<domain>
    

Change Nextcloud data directory

  • Stop Nextcloud service:

    $ snap stop nextcloud
    
  • edit Nextcloud config and change the ‘datadirectory’ value with ‘/media/nextcloud/data’:

    $ vim /var/snap/nextcloud/current/nextcloud/config/config.php
    
  • copy data in the new directory:

    $ mv /var/snap/nextcloud/common/nextcloud/data /media/nextcloud
    
  • start Nextcloud service:

    $ snap start nextcloud
    

Create DNS record

Now it is necessary to create a DNS record ‘A’ to reach the service using a DNS name and to configure the certificate. The DNS name must be the same as previously added in the trusted domain.

Create certificate with Let’s Encrypt

  • Create certificate by Nextcloud:

    $ ufw allow 80,443/tcp
    
    $ nextcloud.enable-https lets-encrypt
    

Now you can access to Nextcloud from your browser.

Install and configure server mail

  • Change VM hostname with your domain:

    $ vi /etc/hostname
    
  • in “/etc/hosts” add a row with VM private IP and hostname (<private-ip> <hostname>):

    $ vi /etc/hosts
    
  • update:

    $ apt update
    
  • install mailutils, when requerid select ‘internet-site’ and configure complite domain:

    $ apt install mailutils
    
  • edit postfix configuration, edit ‘inet_interfaces’ from ‘all’ to ‘loopback-only’ and ‘myhostname’ with your domain:

    $ vi /etc/postfix/main.cf
    
  • restart postfix:

    $ service postfix restart
    

Now you can run a test to verify the correct functioning of the mail server:

$ echo "This email confirms that Postfix is working" | mail -s "Testing Posfix" <your-email>

Configure mail server in Nextcloud

Access to Nextcloud from web interface and from the upper right menù go to “Personal settings -> Basic settings”. Configures the server settings as follows:

Send mode: SMTP
Encryption: None
From address: noreply @ <domain>
Authentication method: None
Server address: localhost
Port: 25

To test the email send function, go to ‘Personal info’ (left menù) and add your email. Now return to the email settings and click on “Send email”.