How to Install Nextcloud on a Synology the Easy Way

Begin by installing a package called “Container Manager”. This should have created a new folder in your drive volume called ‘docker’. Create a new folder within this folder called “nextcloud”. Then create 7 new folders inside the nextcloud folder called “config”, “db”, “custom_apps”, “themes”, “html”, “redis”, and “data”. Now right click on the nextcloud folder that you have just created and click Properties. Go to the Permission tab then click Advanced options. From the drop-down menu choose “Make inherited permissions explicit“. Select Everyone then click the Edit tab. Check all Read and Write Permissions and click on Done. After you click Done check “Apply to this folder, sub-folders and files“ then click on Save. Now open up the “Container Manager” package and click on “Project”. Name it “nextcloud” and select “/docker/nextcloud” for the project path. Choose “Create docker-compose.yml” for source and add the following:

version: '3.9'
services:
  mariadb:
    image: mariadb:latest
    container_name: Nextcloud-DB
    security_opt:
      - no-new-privileges:true
    user: 1026:100
    command: --transaction-isolation=READ-COMMITTED --binlog-format=ROW --innodb-read-only-compressed=OFF
    volumes:
      - ./db:/var/lib/mysql:rw
      - ./db:/etc/mysql/conf.d:rw
    environment:
      - MYSQL_ROOT_PASSWORD=[PASSWORD]
      - MYSQL_PASSWORD=[PASSWORD]
      - MYSQL_DATABASE=nextcloud
      - MYSQL_USER=nextcloud
      - TZ=America/Los_Angeles
    restart: on-failure:5
      
  redis:
    image: redis:latest
    container_name: Nextcloud-REDIS
    hostname: nextcloudredis
    user: 1026:100
    healthcheck:
     test: ["CMD-SHELL", "redis-cli ping || exit 1"]
    volumes:
      - ./redis:/data:rw
    environment:
      TZ: America/Los_Angeles
    restart: on-failure:5
      
  nextcloud:
    image: nextcloud:latest
    container_name: Nextcloud
    ports:
      - 9333:80
    depends_on:
      mariadb:
       condition: service_started
      redis:
       condition: service_healthy
    environment:
      - REDIS_HOST=nextcloudredis
      - NEXTCLOUD_TRUSTED_DOMAINS=[DOMAIN] 10.0.1.11
      - TRUSTED_PROXIES=[DOMAIN] 10.0.1.11
      - OVERWRITEHOST=[DOMAIN]
      - OVERWRITEPROTOCOL=https
      - MYSQL_PASSWORD=[PASSWORD]
      - MYSQL_DATABASE=nextcloud
      - MYSQL_USER=nextcloud
      - MYSQL_HOST=mariadb
    healthcheck:
     test: curl -f http://localhost:80/ || exit 1
    volumes:
      - ./html:/var/www/html:rw
      - ./custom_apps:/var/www/html/custom_apps:rw
      - ./config:/var/www/html/config:rw
      - ./data:/var/www/html/data:rw
      - ./themes:/var/www/html/themes:rw
    restart: on-failure:5
    
  cron:
   image: nextcloud:apache
   container_name: Nextcloud-CRON
   restart: always
   volumes:
     - ./config:/var/www/html/config:rw
     - ./html:/var/www/html:rw
     - ./custom_apps:/var/www/html/custom_apps:rw
     - ./data:/var/www/html/data:rw
   entrypoint: /cron.sh
   depends_on:
    mariadb:
       condition: service_started
    redis:
       condition: service_started

Replace [PASSWORD] with unique strong passwords and [DOMAIN] with your own domain name. Continue to build the project. Once completed, you should be able to access the Nextcloud UI by typing “https://[SYNOLOGY_IP]:9333” in your web browser. Go ahead and create an admin user. Once installed, you will be logged in. If you are using LDAP you can enable the “LDAP user and group backend” app and configure it with uid=root,cn=users,dc=[SUBDOMAIN],dc=[DOMAIN],dc=[DOMAINSUFFIX] for the User DN. Now we just need to create a reverse proxy so that we don’t have to access Nextcloud with an IP address and port number. This can be done by going to Control Panel -> Login Portal -> Advanced in your Synology and clicking on the “Reverse Proxy” button. Enter your domain name for source and the IP address of your Synology for the destination. Be sure to specify port 9333 for the destination. Last, there is a file that needs to be modified. This might be fixed in a future version, but for now open up the .htaccess file in the html folder and look for the following lines:

RewriteRule ^\.well-known/carddav /remote.php/dav/ [R=301,L]
RewriteRule ^\.well-known/caldav /remote.php/dav/ [R=301,L]

Change it to the following replacing [DOMAIN] with your own domain name:

RewriteRule ^\.well-known/carddav https://[DOMAIN]/remote.php/dav/ [R=301,L]
RewriteRule ^\.well-known/caldav https://[DOMAIN]/remote.php/dav/ [R=301,L]

That’s it! Now you should be able to access Nextcloud with your domain name. If you need to run any commands as the web server user, go into Container Manager and click on ‘Container’, then right click on ‘Nextcloud’ and select ‘Open Terminal’. Then run the command like so:

# su - www-data -s /bin/bash -c 'php /var/www/html/occ db:add-missing-indices'

Reference

https://mariushosting.com/synology-how-to-install-nextcloud-using-docker/