Configure CouchDB with nginx, basic_auth and SSL on Ubuntu 12.04

Here my notes from configuring CouchDB to face the big wide web.

First the version off, version of CouchDB in the Ubuntu repos was quite out of date so I decided it was best to compile from source, which is pretty straight forward. I followed the instructions from CouchDB, The Definitive Guide. but the guide in the wiki is just as good and contains some vital troubleshooting tips.

First off install the dependencies:

sudo apt-get install build-essential erlang libicu-dev libmozjs-dev libcurl4-openssl-dev

Next download the souce:

cd /tmp
sudo wget http://apache.is.co.za/couchdb/releases/1.2.0/apache-couchdb-1.2.0.tar.gz

Instal!

cd apache-couchdb-1.2.0.tar.gz
sudo ./configure
sudo make && sudo make install

Now to secure the installation, first lets create a user to run it from

adduser --system --home /usr/local/var/lib/couchdb --no-create-home --shell /bin/bash --group --gecos "CouchDB" couchdb

Then change the ownership and permissions of the Couchdb files

sudo chown -R couchdb:couchdb /usr/local/etc/couchdb
sudo chown -R couchdb:couchdb /usr/local/var/lib/couchdb
sudo chown -R couchdb:couchdb /usr/local/var/log/couchdb
sudo chown -R couchdb:couchdb /usr/local/var/run/couchdb
sudo chmod -R 0770 /usr/local/etc/couchdb
sudo chmod -R 0770 /usr/local/var/lib/couchdb
sudo chmod -R 0770 /usr/local/var/log/couchdb
sudo chmod -R 0770 /usr/local/var/run/couchdb

Next install logrotate and initd scripts

sudo ln -s /usr/local/etc/logrotate.d/couchdb /etc/logrotate.d/couchdb
sudo ln -s /usr/local/etc/init.d/couchdb /etc/init.d
sudo update-rc.d couchdb defaults

I spent a fair bit of time trying to configure CouchDB to use the built in SSL webserver, but in the end was unsuccessfull. Along the way I found out the built server has difficulty handling high loads and it is been actively patched to try resolve this issue.

Nginx it is, installing nginx is outside the scope of this post, so I’m just going to assume you already have it installed. Here is my nginx config which I saved in /etc/nginx/conf.d/couchdb.conf:

server {
    listen 80;
    server_name couch.domain.com;
    rewrite ^ https://$server_name$request_uri? permanent;
}

server {
    listen 443;
    server_name couch.domain.com;

    ssl on;
    ssl_certificate /etc/ssl/certs/cert.pem;
    ssl_certificate_key /etc/ssl/private/key.pem;

    ssl_session_timeout 5m;
    ssl_protocols SSLv2 SSLv3 TLSv1;
    ssl_ciphers HIGH:!aNULL:!MD5;
    ssl_prefer_server_ciphers on;

    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 Authorization "";
        proxy_set_header X-Forwarded-Ssl on;
    }
}

I scrapped my config together by using various parts of the wiki until I had a working configuration.

I’m using self signed certificates so we’ll have to generate them and put them in the correct place

cd /tmp
openssl genrsa -out couch_key.pem 4096
openssl req -new -x509 -key couch_key.pem -out couch_cert.pem -days 1095
sudo mv couch_key.pem /etc/ssl/private
sudo mv couch_cert.pem /etc/ssl/certs

Finally lets setup basic_auth for CouchDB, it’s just to lines you need change in /usr/local/etc/couchdb/local.ini. First find uncomment

; Uncomment next line to trigger basic-auth popup on unauthorized requests.
WWW-Authenticate = Basic realm="administrator"

Next add an admin user 

[admins]
admin = secret

Lastly, see that it is all working

curl -L -k https://admin:secret@couch.domain.com/
{"couchdb":"Welcome","version":"1.2.0"}

That’s it, please feel free to leave any suggestions for improvement/ask for help in the comments.

Ludwig Wittgenstein

Ludwig Wittgenstein

If people never did silly things nothing intelligent would ever get done.
Ludwig Wittgenstein (via philosophy-quotes)
Hmm, that’s a hard question. I guess that the best answer is that by the time you are old enough to understand that you are NOT going to be a rock start or a unique, beautiful snowflake, it will be too late. Lack of motivation fundamentally comes from fear — you are afraid to do things since you don’t think you can do them internally — which is 99% true.However, the good news is that if you REALLY try to do anything, there is nothing that you can’t do within reason of course. So my suggestion is simple and basically how I start anyone I mentor — start with ANYTHING that you want to do, something small. Then start it, work on it everything single day until its done, I don’t care if you are sick, haev to work, goto school, your girlfriend wants to do something, whatever, just work on it everyday — at some point you will get to a point where you find your own mental limits, this is usually the point where every single person quits and then sells themselves the bullshit concept, “I could finish it if I wanted”– this is crap, the fact is THEY can’t finish it, but this is just the moment of transformation — now, here is where you are going to triple your efforts, read whatever you need to read, and finish it, no matter what — And if you don’t think this advice is true I have heard it personally from every single iconic computer person on the planet — the key to success is so simple — just finish everything.You will never become motivated, until you don’t need to become motivated then you will be motivated, hard to explain, but that’s the way it works –Additional, arguement — what are you doing anyway?
If you aren’t 10x smarter than everyone now, how will you compete in 10-20 years? Imagine the math, physics, computer science, nanotech, biotech, etc. that will be common then — thus, you better get crackin! There is so much work to be done and no time to do it — if you are less than 40 and aren’t working at least 15-18 hours a day you are not working enough, you simply can’t compete with the best in the world unless you are willing to throttle it that much
Andre Lamothe, Quote (in answer to how he motivates himself
Most artists and designers I know would rather work all night than turn in a sub-standard job. It is a universal truth that all artists think they a [sic] frauds and charlatans, and live in constant fear of being exposed. We believe by working harder than anyone else we can evaded [sic] detection. The bean-counters rumbled this centuries ago and have been profitably exploiting this weakness ever since. You don’t have to drive creative folk like most workers. They drive themselves. Just wind ‘em up and let ‘em go.
Even if you were about to live three thousand years or thrice ten thousand, never the less remember this, that no one loses any other life than this which he is living, nor lives any other than this he is losing. Thus the longest and the shortest come to the same thing.
Marcus Aurelius - Meditations - Book 2