Install CouchDB on Ubuntu 22.04 - HostnExtra
Tags: couchdb, ubuntu, database, nginx
Published: 2022-10-13 13:47:32

In this article, we'll explain how to install CouchDB on Ubuntu 22.04 and configure it with Nginx proxy server and Certbot SSL.

Apache CouchDB is an open-source document-oriented NoSQL database, implemented in Erlang. Seamless multi-master sync, that scales from Big Data to Mobile, with an Intuitive HTTP/JSON API and designed for Reliability. Store your data safely, on your own servers, or with any leading cloud provider. Your web- and native applications love CouchDB, because it speaks JSON natively and supports binary data for all your data storage needs.

#### Prerequisites:

- A Ubuntu 22.04 installed dedicated server or KVM VPS with root or non-root access (for non-root, use "sudo").
- A DNS A record that points your domain to the public IP address of the server.

Let's start with the installation process

[Install CouchDB on Ubuntu](https://www.hostnextra.com/kb/install-couchdb-on-ubuntu/)

#### 1. Update the server and install dependency

`# sudo apt update && sudo apt install -y curl apt-transport-https gnupg`

#### 2. Configure the CouchDB repository and key.

```
# curl https://couchdb.apache.org/repo/keys.asc | gpg --dearmor | sudo tee /usr/share/keyrings/couchdb-archive-keyring.gpg >/dev/null 2>&1
# echo "deb [signed-by=/usr/share/keyrings/couchdb-archive-keyring.gpg] https://apache.jfrog.io/artifactory/couchdb-deb/ ${VERSION_CODENAME} main" | sudo tee /etc/apt/sources.list.d/couchdb.list >/dev/null
# sudo apt update
```

**Note:**

```
If you get following error, edit couchdb.list and add jammy before main. It will resolve it.

E: Malformed entry 1 in list file /etc/apt/sources.list.d/couchdb.list (Component)
E: The list of sources could not be read
```

#### 3. Install CouchDB

`# sudo apt install -y couchdb`

The installation will ask few question.

```
1. standalone 2. clustered 3. none
General type of CouchDB configuration: 1
```

Choose according to your choice. We have select 1 for this demonstration purpose.

```
A CouchDB node has an Erlang magic cookie value set at startup.

This value must match for all nodes in the cluster. If they do not match, attempts to connect the node to the cluster will be rejected.

CouchDB Erlang magic cookie: monkey
```

Set the Erlang Magic Cookie. This is a unique identifier to authenticate for your cluster, all nodes must have the same cookie. Here we have wrote monkey. You can write anything you want.

The default is 127.0.0.1 (loopback) for standalone nodes, and 0.0.0.0 (all interfaces) for clustered nodes. In clustered mode, it is not allowed to bind to 127.0.0.1.

```
CouchDB interface bind address: 0.0.0.0

Add bind address as 0.0.0.0
```

A pre-existing admin user will not be overwritten by this package.

```
Password for the CouchDB "admin" user:

Repeat password for the CouchDB "admin" user:
```

Here add admin password as per your choice.

The installation has been completed successfully. Navigate to browser and open http://<SERVER IP>:5984/_utils/

#### 4. Configure Nginx

Now, let's configure Nginx proxy. Install Nginx using following command:

`# sudo apt-get install -y nginx apache2-utils`

Remove default Nginx config file, we'll create our own config file.

`# sudo rm -rf /etc/nginx/sites-enabled/default`

Now create a new config file

`# nano /etc/nginx/sites-available/couchdb-site`

Add following content:

```
server {
listen 80 default_server;
server_name coachdb.local;

location / {
proxy_pass http://localhost:5984;
proxy_redirect off;

proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;

}

}
```

**Note:**

For security reason, you can change the default http 80 port to something else like 5000. So to access it you need to mention 5000 port at the end of website name. But it may create conflict with SSL. We've not test different port with SSL.

Now, create a symbolic link and restart Nginx service using following command:

```
# ln -s /etc/nginx/sites-available/couchdb-site /etc/nginx/sites-enabled/
# nginx -t
# systemctl restart nginx
```

#### 5. Configure Firewall

Enable UFW if its not enabled.

`# ufw enable`

Add following ports:

```
# ufw allow 22/tcp

# ufw allow 80/tcp

# ufw allow 443/tcp

# ufw allow 5984/tcp
```

Check the status:

`# ufw status`

**Note:**

Your SSH port might be different.
If you have custom Nginx web server port, add that port also.

#### 6. Install certbot's nginx package

`# apt install certbot python3-certbot-nginx -y`

#### 7. Obtaining a Certificate

Obtain a certificate using certbot command. The Nginx plugin will take care of reconfiguring Nginx and reloading the config.

`# certbot --nginx -d yoursite.com -d www.yousite.com`

By running certbot first time, you will be prompted to enter an email address and agree to the terms of service.

That's it, we have successfully install CouchDB on Ubuntu 22.04 and configured it with Nginx proxy server and Certbot SSL.