The goal of this article is to use Nextcloud as a local data storage system and publish that system to the internet via VPS so that Nextcloud can be accessed locally from anywhere.
Prepare
I assume you already have 1 VPS working and have 1 domain drive.example.com pointed to VPS I use cloudflare’s service to manage domains, as well as to be able to use https for my domain You Need an old laptop to run Nextcloud on docker
You need to know the basics of docker to be able to continue this article
Step 1: Install Nextcloud on your Laptop
Install Nextcloud on your laptop using docker and docker-compose, this is a basic file to run Nextcloud on docker, note that you should change the password for MYSQL_ROOT_PASSWORD and MYSQL_PASSWORD when using on production environment
docker-compose.yml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | <span class="token key atrule">version</span> <span class="token punctuation">:</span> <span class="token string">'3.7'</span> <span class="token key atrule">services</span> <span class="token punctuation">:</span> <span class="token key atrule">db</span> <span class="token punctuation">:</span> <span class="token key atrule">image</span> <span class="token punctuation">:</span> mariadb <span class="token key atrule">restart</span> <span class="token punctuation">:</span> always <span class="token key atrule">environment</span> <span class="token punctuation">:</span> <span class="token punctuation">-</span> MYSQL_ROOT_PASSWORD=nextcloud <span class="token comment"># should change this password when push to Production</span> <span class="token punctuation">-</span> MYSQL_DATABASE=nextcloud <span class="token punctuation">-</span> MYSQL_USER=nextcloud <span class="token punctuation">-</span> MYSQL_PASSWORD=nextcloud <span class="token key atrule">volumes</span> <span class="token punctuation">:</span> <span class="token punctuation">-</span> db <span class="token punctuation">:</span> /var/lib/mysql <span class="token key atrule">networks</span> <span class="token punctuation">:</span> <span class="token punctuation">-</span> nextcloud <span class="token key atrule">app</span> <span class="token punctuation">:</span> <span class="token key atrule">image</span> <span class="token punctuation">:</span> nextcloud <span class="token key atrule">restart</span> <span class="token punctuation">:</span> always <span class="token key atrule">ports</span> <span class="token punctuation">:</span> <span class="token punctuation">-</span> 8080 <span class="token punctuation">:</span> <span class="token number">80</span> <span class="token key atrule">depends_on</span> <span class="token punctuation">:</span> <span class="token punctuation">-</span> db <span class="token key atrule">volumes</span> <span class="token punctuation">:</span> <span class="token punctuation">-</span> nextcloud <span class="token punctuation">:</span> /var/www/html <span class="token key atrule">networks</span> <span class="token punctuation">:</span> <span class="token punctuation">-</span> nextcloud <span class="token key atrule">environment</span> <span class="token punctuation">:</span> <span class="token punctuation">-</span> MYSQL_HOST=db <span class="token punctuation">-</span> MYSQL_DATABASE=nextcloud <span class="token punctuation">-</span> MYSQL_USER=nextcloud <span class="token punctuation">-</span> MYSQL_PASSWORD=nextcloud <span class="token punctuation">-</span> MYSQL_ROOT_PASSWORD=nextcloud |
When the initialization is complete, you can access Nextcloud with the address http://localhost:8080 and log in with the admin account and password that you created when installing Nextcloud
Once this step is done, you can use Nextcloud to store your data on your laptop, but you can only access Nextcloud locally, if you want to access Nextcloud locally from anywhere then you need to publish Nextcloud locally to the internet we will do in the next step
Step 2: Link Local Nextcloud with VPS using P2P network
I use Zerotier to create a P2P network between my laptop and VPS, then you can access Nextcloud at VPS using http://<IP of LAPTOP>:8080
You go to https://my.zerotier.com to create an account and create a network, assuming your network ID is 1234567890abcdef
On VPS, you need to install Zerotier and join network 1234567890abcdef
, similarly on laptop, you need to install Zerotier and join network 1234567890abcdef
After joining, you can access Nextcloud on VPS with http://<IP of LAPTOP>:8080
IP of LAPTOP can be viewed at https://my.zerotier.com or you can use Zerotier One to view IP of regular LAPTOP, to avoid IP conflict, you should avoid using IP range 192.168.xx(LOCAL ) and 10.xxx (VPS) you can use IP range 172.16.xx
When your VPS and laptop are online, zerotier will automatically create a P2P network between these two devices, and give each node 1 IP in the range 172.16.xx
Assuming the laptop’s IP is 172.16.0.123, when you access http://172.16.0.123:8080 , you will access Nextcloud on the laptop
I choose Zerotier to create a P2P network between a laptop and a VPS because it’s easy and free, in addition, it supports many different operating systems and can be used on many different devices up to 25 devices (free version). )
Step 3: Publish Nextcloud locally to the internet
After step 2 you can access nextcloud on VPS with the address http://172.16.0.123:8080 , but if you want to access nextcloud by domain, you must use a reverse proxy to point the domain to Nextcloud http address ://172.16.0.123:8080 and configure SSL for the domain
You can use nginx-proxy-manager to do reverse proxy and configure SSL for domain, below is sample file to configure nginx-proxy-manager using docker
docker-compose.yml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | <span class="token key atrule">version</span> <span class="token punctuation">:</span> <span class="token string">'3.7'</span> <span class="token key atrule">services</span> <span class="token punctuation">:</span> <span class="token key atrule">reverse-proxy</span> <span class="token punctuation">:</span> <span class="token key atrule">image</span> <span class="token punctuation">:</span> nginx <span class="token punctuation">-</span> proxy <span class="token punctuation">-</span> manager <span class="token key atrule">restart</span> <span class="token punctuation">:</span> always <span class="token key atrule">ports</span> <span class="token punctuation">:</span> <span class="token punctuation">-</span> <span class="token datetime number">80:80</span> <span class="token punctuation">-</span> <span class="token datetime number">81:81</span> <span class="token punctuation">-</span> 443 <span class="token punctuation">:</span> <span class="token number">443</span> <span class="token key atrule">volumes</span> <span class="token punctuation">:</span> <span class="token punctuation">-</span> ./data <span class="token punctuation">:</span> /data <span class="token punctuation">-</span> ./letsencrypt <span class="token punctuation">:</span> /etc/letsencrypt <span class="token key atrule">environment</span> <span class="token punctuation">:</span> <span class="token punctuation">-</span> TZ=Asia/Ho_Chi_Minh <span class="token punctuation">-</span> PGID=1000 <span class="token punctuation">-</span> PUID=1000 <span class="token punctuation">-</span> DISABLE_IPV6=true <span class="token key atrule">network-mode</span> <span class="token punctuation">:</span> <span class="token string">"host"</span> <span class="token comment"># quan trọng: để có thể truy cập vào Nextcloud ở VPS bằng địa chỉ http://<IP của LAPTOP>:8080</span> |
After the configuration is complete, you can access nginx-proxy-manager at http://<IP of VPS>:81 to configure the proxy to point the domain to your Nextcloud
Complete System Flow
The subsection you can refer to
Configure SSL for the domain
I use cloudflare to manage domains and configure SSL for domains, you can use cloudflare or another service to manage domains and configure SSL for domains
You can configure SSL for the domain using cloudflare proxy, note that it is recommended to use full SSL mode (you generate SSL certificate in cloudflare and download and install it on VPS)
Things to do when publishing Nextcloud locally to the internet
- add Trusted Domains to your Nextcloud
- config force https in your Nextcloud in the file config.php key overprotocol => ‘https’, you can read more here https://docs.nextcloud.com/server/latest/admin_manual/configuration_server/reverse_proxy_configuration.html
- Some apps you need to install to make your Nextcloud work better such as: TOTP (2-factor authentication), pdf viewer, raw preview…
- cron.php may not run if you don’t configure cronjob for your Nextcloud, you can read more here https://docs.nextcloud.com/server/latest/admin_manual/configuration_server/background_jobs_configuration.html