NGINX Unit

NextCloud§

Note

This howto uses NextCloud 16, PHP 7.3, MariaDB, and apt; adjust it to your scenario as needed.

To install NextCloud if you haven’t already done so:

  1. Download and extract NextCloud files:

    $ cd /path/to/
    $ curl -O https://download.nextcloud.com/server/releases/nextcloud-16.0.4.tar.bz2
    $ tar xf nextcloud-16.0.4.tar.bz2
    

    This creates the /path/to/nextcloud directory.

  2. Change the directory ownership, supplying a username that will be used to configure and run Nextcloud:

    # chown -R www-data:www-data /path/to/nextcloud/
    
  3. Install and check NextCloud’s prerequisites:

    # apt install mariadb-server
    # apt install php7.3 php7.3-imagick php7.3-curl php7.3-gd php7.3-intl \
          php7.3-mbstring php7.3-mysql php7.3-xml php7.3-zip
    $ mysql --version
    $ php --version
    
  4. Set up the NextCloud database (note the sample credentials):

    # mysql -u root -p
    
        > CREATE DATABASE nextcloud;
        > CREATE USER 'nextuser'@'localhost' IDENTIFIED BY 'nextpass';
        > GRANT ALL PRIVILEGES ON nextcloud.* TO 'nextuser'@'localhost';
        > FLUSH PRIVILEGES;
    
  5. Finish the installation (here, the occ utility is used; note the credentials and the sudo -u username):

    $ cd /path/to/nextcloud/
    $ sudo -u www-data php occ maintenance:install --database "mysql" \
           --database-name "nextcloud" --database-user "nextuser"     \
           --database-pass "nextpass" --admin-user "admin" --admin-pass "adminpass"
    
          Nextcloud was successfully installed
    

    Note

    Verify the resulting settings in /path/to/nextcloud/config/config.php; in particular, check the trusted domains to ensure your installation will be accessible from within your network:

    'trusted_domains' =>
    array (
      0 => 'localhost',
      1 => '*.example.com',
    ),
    

NGINX and Unit§

To run NextCloud in Unit:

  1. Install NGINX. Currently, NGINX is required to serve static files.

  2. Install Unit with a PHP language module.

  3. Prepare Unit configuration for your update. To obtain an initial template, query the control API:

    # curl --unix-socket /path/to/control.unit.sock \
           http://localhost/config/ > config.json
    

    Note

    Control socket path may vary; run unitd --help or see Startup for details.

  4. Edit the file, adding an app and a listener to make NextCloud available:

    {
        "listeners": {
            "127.0.0.1:9000": {
                "pass": "applications/nextcloud"
            }
        },
    
        "applications": {
            "nextcloud": {
                "type": "php",
                "user": "www-data",
                "group": "www-data",
                "root": "/path/to/nextcloud/"
            }
        }
    }
    
  5. Upload the updated configuration:

    # curl -X PUT --data-binary @config.json --unix-socket \
           /path/to/control.unit.sock http://localhost/config
    
  6. Set up NGINX to serve static files and route PHP requests to Unit (adopted from NextCloud’s guide):

    upstream unit_nextcloud {
        server 127.0.0.1:9000;
    }
    
    server {
        listen 80;
        server_name nextcloud.example.com;
        root /path/to/nextcloud;
    
        location = /.well-known/carddav {
            return 301 $scheme://$host:$server_port/remote.php/dav;
        }
    
        location = /.well-known/caldav {
            return 301 $scheme://$host:$server_port/remote.php/dav;
        }
    
        location / {
            rewrite ^ /index.php$request_uri;
        }
    
        location ~ ^\/(?:build|tests|config|lib|3rdparty|templates|data)\/ {
            deny all;
        }
    
        location ~ ^\/(?:\.|autotest|occ|issue|indie|db_|console) {
            deny all;
        }
    
        location ~ ^\/(?:index|remote|public|cron|core\/ajax\/update|status|ocs\/v[12]|updater\/.+|oc[ms]-provider\/.+)\.php(?:$|\/) {
            proxy_pass http://unit_nextcloud;
            proxy_set_header Host $host;
        }
    
        location ~ ^\/(?:updater|oc[ms]-provider)(?:$|\/) {
            try_files $uri/ =404;
            index index.php;
        }
    
        location ~ \.(?:css|js|woff2?|svg|gif|map)$ {
            try_files $uri /index.php$request_uri;
        }
    
        location ~ \.(?:png|html|ttf|ico|jpg|jpeg|bcmap)$ {
            try_files $uri /index.php$request_uri;
        }
    }
    

    Note

    If you use the above config for your purposes, make sure to replace placeholders, such as /path/to/nextcloud/ in root. For details, refer to NGINX Admin Guide.

Finally, browse to your NextCloud site and complete the installation:

NextCloud in Unit - Home Screen