Hosting Nextcloud

Steps to host your own Nextcloud service.

The Task

The Goals

  • Manual deploy, i.e. no Docker or Snap.
  • Support for online previewing for pictures, videos and documents.
  • Nginx for reverse proxy

The Steps

1. Install Nextcloud

  1. Install dependencies.

     sudo apt install apache2 mariadb-server libapache2-mod-php7.2
     sudo apt install php7.2-gd php7.2-json php7.2-mysql php7.2-curl php7.2-mbstring
     sudo apt install php7.2-intl php-imagick php7.2-xml php7.2-zip
    

    Note

    • miradb can be replaced with mysql.
  2. Download nextcloud-15.0.5.zip from official website and unzip it.
  3. Copy site files to webserver’s data root: sudo cp -r nextcloud /var/www.
  4. Change owner / executor to user www-data.
  5. Create database nextcloud and database user nextclouduser@localhost, and grant access.
  6. Set-up default Apache server, in order to test reverse proxy (config file copied from here).
    • Here I bound service to localhost:80/nextcloud.

2. Install Nginx

  1. sudo apt install nginx
  2. cat /etc/passwd | grep www-data to make sure we have an HTTP user.
    • Here it’s www-data under Ubuntu.
  3. sudo service nginx start
  4. Open a browser tab and visit 127.0.0.1 to check if Nginx is running properly.
    • If there exists a previous installation of other web servers, say the Apache server we’ve just installed via sudo apt install, good chance is that an index.html already exists in /var/www/html/. However, for the default Nginx configuration, the HelloWorld page is loaded in the following order …

            index index.html index.htm index.nginx-debian.html;
      

      So don’t panic when you are greeted by Apache :smile_cat: !

    • Also, do remember to change the default port configuration, so it doesn’t collide with the existing Apache server!

3. [Optional] Configure Nginx for Nextcloud

Nginx will be used for reverse proxy at the end of the day, therefore a dedicate Nginx-powered Nextcloud service is unnecessary! I just did this for fun :smile_cat: !

  1. Create configuration file /etc/nginx/conf.d/nextcloud.conf.
  2. sudo apt install php7.2-fpm
    • Required for Nginx to run PHP files, or 502 Bad Gateway.
  3. sudo systemctl start php7.2-fpm.service
  4. sudo systemctl reload nginx.service

At this point, both localhost:80/nextcloud (via Apache) and localhost:9001 (via Nginx) can reach our Nextcloud service.

4. Configure Nginx for Reverse Proxy

  1. Replace /etc/nginx/conf.d/nextcloud.conf with following lines

     upstream localserver1 {
         server 127.0.0.1:80;    # our local nextcloud service
         # you may have more than just one `server` statement in
         # this block, which enables Nginx to dynamically switch
         # between upstreams based on load.
     }
    
     server {
         listen 9001;
         server_name _;
    
         # redirect every subdir to our Apache-powered Nextcloud
         location / {
             proxy_pass http://localserver1/nextcloud/;
             proxy_redirect off;
             proxy_set_header Host $host;
             proxy_set_header X-Real-IP $remote_addr;
             proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
         }
     }
    

    Note

    • The proxy_set_header lines are required, or Nextcloud will identify the Nginx proxy, disallowing access.
  2. Visit localhost:9001.

    • After Nginx caching for sometime, you will see browser’s URL stops at localhost/nextcloud/index.php/xxxx, which indicates our Nginx reverse proxy is working as expected.

5. Final Touches

  1. Open browser tab and visit Nextcloud service.
  2. Walk through Nextcloud initialization step in its fancy WebApp.
  3. Enjoy.

References