2023-05-23 06:49:50 +02:00
# Setup a CouchDB server
2021-11-18 11:15:23 +02:00
2023-07-04 01:52:48 +02:00
## Table of Contents
- [Configure ](#configure )
- [Run ](#run )
- [Docker CLI ](#docker-cli )
- [Docker Compose ](#docker-compose )
- [Access from a mobile device ](#access-from-a-mobile-device )
- [Testing from a mobile ](#testing-from-a-mobile )
- [Setting up your domain ](#setting-up-your-domain )
- [Reverse Proxies ](#reverse-proxies )
- [Traefik ](#traefik )
---
2023-05-23 06:49:50 +02:00
## Configure
2021-12-07 10:28:18 +02:00
2023-05-23 06:49:50 +02:00
The easiest way to set up a CouchDB instance is using the official [docker image ](https://hub.docker.com/_/couchdb ).
2021-12-07 10:28:18 +02:00
2023-07-04 00:15:00 +02:00
Some initial configuration is required. Create a `local.ini` to use Self-hosted LiveSync as follows ([CouchDB has to be version 3.2 or higher](https://docs.couchdb.org/en/latest/config/http.html#chttpd/enable_cors), if lower `enable_cors = true` has to be under section `[httpd]` ):
2021-12-07 10:28:18 +02:00
2023-05-23 06:49:50 +02:00
```ini
2021-12-07 10:28:18 +02:00
[couchdb]
single_node=true
2022-11-23 08:12:50 +02:00
max_document_size = 50000000
2021-12-07 10:28:18 +02:00
[chttpd]
require_valid_user = true
2022-11-23 08:12:50 +02:00
max_http_request_size = 4294967296
2023-07-04 00:15:00 +02:00
enable_cors = true
2021-12-07 10:28:18 +02:00
[chttpd_auth]
require_valid_user = true
authentication_redirect = /_utils/session.html
[httpd]
WWW-Authenticate = Basic realm="couchdb"
2023-07-04 00:15:00 +02:00
bind_address = 0.0.0.0
2021-12-07 10:28:18 +02:00
[cors]
2023-07-04 00:15:00 +02:00
origins = app://obsidian.md, capacitor://localhost, http://localhost
2021-12-07 10:28:18 +02:00
credentials = true
headers = accept, authorization, content-type, origin, referer
2023-07-04 00:15:00 +02:00
methods = GET,PUT,POST,HEAD,DELETE
2021-12-07 10:28:18 +02:00
max_age = 3600
```
2023-05-23 06:49:50 +02:00
## Run
### Docker CLI
You can launch CouchDB using your `local.ini` like this:
2021-12-07 10:28:18 +02:00
```
2022-09-12 18:36:50 +02:00
$ docker run --rm -it -e COUCHDB_USER=admin -e COUCHDB_PASSWORD=password -v /path/to/local.ini:/opt/couchdb/etc/local.ini -p 5984:5984 couchdb
2021-12-07 10:28:18 +02:00
```
2022-09-12 18:36:50 +02:00
*Remember to replace the path with the path to your local.ini*
2021-12-07 10:28:18 +02:00
2023-05-23 06:49:50 +02:00
Run in detached mode:
2021-12-07 10:28:18 +02:00
```
2022-09-12 18:36:50 +02:00
$ docker run -d --restart always -e COUCHDB_USER=admin -e COUCHDB_PASSWORD=password -v /path/to/local.ini:/opt/couchdb/etc/local.ini -p 5984:5984 couchdb
2021-12-07 10:28:18 +02:00
```
2022-09-12 18:36:50 +02:00
*Remember to replace the path with the path to your local.ini*
2021-12-07 10:28:18 +02:00
2023-05-23 06:49:50 +02:00
### Docker Compose
2023-07-04 00:15:00 +02:00
Create a directory, place your `local.ini` within it, and create a `docker-compose.yml` alongside it. Make sure to have write permissions for `local.ini` and the about to be created `data` folder after the container start. The directory structure should look similar to this:
2023-05-23 06:49:50 +02:00
```
obsidian-livesync
├── docker-compose.yml
└── local.ini
```
A good place to start for `docker-compose.yml` :
```yaml
version: "2.1"
services:
couchdb:
image: couchdb
container_name: obsidian-livesync
user: 1000:1000
environment:
- COUCHDB_USER=admin
- COUCHDB_PASSWORD=password
volumes:
- ./data:/opt/couchdb/data
- ./local.ini:/opt/couchdb/etc/local.ini
ports:
- 5984:5984
restart: unless-stopped
```
And finally launch the container
```
# -d will launch detached so the container runs in background
docker compose up -d
```
2022-12-22 11:02:12 +02:00
## Access from a mobile device
2021-12-07 10:28:18 +02:00
If you want to access Self-hosted LiveSync from mobile devices, you need a valid SSL certificate.
2022-12-22 11:02:12 +02:00
### Testing from a mobile
2023-05-23 06:49:50 +02:00
In the testing phase, [localhost.run ](https://localhost.run/ ) or something like services is very useful.
2021-12-07 10:28:18 +02:00
2023-05-23 06:49:50 +02:00
example using localhost.run:
2021-12-07 10:28:18 +02:00
```
$ ssh -R 80:localhost:5984 nokey@localhost.run
Warning: Permanently added the RSA host key for IP address '35.171.254.69' to the list of known hosts.
===============================================================================
Welcome to localhost.run!
Follow your favourite reverse tunnel at [https://twitter.com/localhost_run].
**You need a SSH key to access this service.**
If you get a permission denied follow Gitlab's most excellent howto:
https://docs.gitlab.com/ee/ssh/
*Only rsa and ed25519 keys are supported*
To set up and manage custom domains go to https://admin.localhost.run/
More details on custom domains (and how to enable subdomains of your custom
domain) at https://localhost.run/docs/custom-domains
To explore using localhost.run visit the documentation site:
https://localhost.run/docs/
===============================================================================
** your connection id is xxxxxxxxxxxxxxxxxxxxxxxxxxxx, please mention it if you send me a message about an issue. **
xxxxxxxx.localhost.run tunneled with tls termination, https://xxxxxxxx.localhost.run
Connection to localhost.run closed by remote host.
Connection to localhost.run closed.
```
https://xxxxxxxx.localhost.run is the temporary server address.
### Setting up your domain
Set the A record of your domain to point to your server, and host reverse proxy as you like.
Note: Mounting CouchDB on the top directory is not recommended.
Using Caddy is a handy way to serve the server with SSL automatically.
2023-07-04 01:52:48 +02:00
I have published [docker-compose.yml and ini files ](https://github.com/vrtmrz/self-hosted-livesync-server ) that launch Caddy and CouchDB at once. If you are using Traefik you can check the [Reverse Proxies ](#reverse-proxies ) section below.
2021-12-07 10:28:18 +02:00
2022-11-23 08:12:50 +02:00
And, be sure to check the server log and be careful of malicious access.
2023-07-04 01:52:48 +02:00
## Reverse Proxies
### Traefik
If you are using Traefik, this [docker-compose.yml ](https://github.com/vrtmrz/obsidian-livesync/blob/main/docker-compose.traefik.yml ) file (also pasted below) has all the right CORS parameters set. It assumes you have an external network called `proxy` .
```yaml
version: "2.1"
services:
couchdb:
image: couchdb:latest
container_name: obsidian-livesync
user: 1000:1000
environment:
- COUCHDB_USER=username
- COUCHDB_PASSWORD=password
volumes:
- ./data:/opt/couchdb/data
- ./local.ini:/opt/couchdb/etc/local.ini
# Ports not needed when already passed to Traefik
#ports:
# - 5984:5984
restart: unless-stopped
networks:
- proxy
labels:
- "traefik.enable=true"
# The Traefik Network
- "traefik.docker.network=proxy"
# Don't forget to replace 'obsidian-livesync.example.org' with your own domain
- "traefik.http.routers.obsidian-livesync.rule=Host(`obsidian-livesync.example.org`)"
# The 'websecure' entryPoint is basically your HTTPS entrypoint. Check the next code snippet if you are encountering problems only; you probably have a working traefik configuration if this is not your first container you are reverse proxying.
- "traefik.http.routers.obsidian-livesync.entrypoints=websecure"
- "traefik.http.routers.obsidian-livesync.service=obsidian-livesync"
- "traefik.http.services.obsidian-livesync.loadbalancer.server.port=5984"
- "traefik.http.routers.obsidian-livesync.tls=true"
# Replace the string 'letsencrypt' with your own certificate resolver
- "traefik.http.routers.obsidian-livesync.tls.certresolver=letsencrypt"
- "traefik.http.routers.obsidian-livesync.middlewares=obsidiancors"
# The part needed for CORS to work on Traefik 2.x starts here
- "traefik.http.middlewares.obsidiancors.headers.accesscontrolallowmethods=GET,PUT,POST,HEAD,DELETE"
- "traefik.http.middlewares.obsidiancors.headers.accesscontrolallowheaders=accept,authorization,content-type,origin,referer"
- "traefik.http.middlewares.obsidiancors.headers.accesscontrolalloworiginlist=app://obsidian.md,capacitor://localhost,http://localhost"
- "traefik.http.middlewares.obsidiancors.headers.accesscontrolmaxage=3600"
- "traefik.http.middlewares.obsidiancors.headers.addvaryheader=true"
- "traefik.http.middlewares.obsidiancors.headers.accessControlAllowCredentials=true"
networks:
proxy:
external: true
```
Partial `traefik.yml` config file mentioned in above:
```yml
...
entryPoints:
web:
address: ":80"
http:
redirections:
entryPoint:
to: "websecure"
scheme: "https"
websecure:
address: ":443"
...
```