Calibre Server

Calibre is one of my favorite pieces of open source software. It's amazing, free, and constantly updated. Aside from adroitly managing your digital library from the pc, it can also operate as a server, making all your ebooks available online wherever you go. Here's a quick guide on how to set that up.

You'll want to keep some references handy like the Calibre manual and DigitalOcean's guide.

The DigitalOcean guide is pretty complete, but I want to note a few snags I ran into and to make a more streamlined guide for the specific purpose of hosting on a VPS behind nginx rather than Apache.

First, download Calibre on your server. I find versions after 6.0 complain about PyQt not being installed or one of its dependencies. You can probably resolve this by installing whatever it wants, but this is a lot of unnecessary stuff if you don't otherwise have a windowing system, so I just stick with an earlier version.

sudo apt update && sudo apt install -y libfontconfig libgl1-mesa-glx
sudo -v && sudo calibre-uninstall && wget -nv -O- https://download.calibre-ebook.com/linux-installer.sh | sudo sh /dev/stdin version=5.44

Make a folder for Calibre. I like to put it under /usr/local/lib (for library, get it?). Assuming you already have a folder of books on your local machine, copy them to the server.

sudo mkdir /usr/local/lib/calibre
sudo chown -R user:user /usr/local/lib/calibre

# from your local machine
rsync -avz /path/to/your/calibre/books user@server:/usr/local/lib/calibre

Now you should have a books folder that is already set up as a calibre library on your server, so you can just point calibre at it.

Create a user for calibre on the server.

sudo useradd system --home /usr/local/lib/calibre --shell /bin/bash calibre

Create a system service for the calibre server. Specify the library, a location for the user database and enable authentication.

## startup service
[Unit]
Description=calibre content server
After=network.target

[Service]
Type=simple
User=calibre
Group=calibre
ExecStart=/opt/calibre/calibre-server /usr/local/lib/calibre/books --enable-local-write --userdb /usr/local/lib/calibre/users.sqlite --enable-auth 

[Install]
WantedBy=multi-user.target

Set up a domain for your Calibre server (probably a subdomain if you're using the server to host other things) and create an nginx virtual host. The example below is after using Let's Encrypt to create an SSL certificate.

upstream books_app_server {
  # fail_timeout=0 means we always retry an upstream even if it failed
  # to return a good HTTP response (in case the Unicorn master nukes a
  # single worker for timing out).
 
  server 127.0.0.1:8080 fail_timeout=0;
}
 
server {
    server_name calibre.example.com;
 
    client_max_body_size 4G;
    fastcgi_buffers 16 16k;
    fastcgi_buffer_size 32k;
 
    access_log /usr/local/lib/calibre/logs/nginx-access.log;
    error_log /usr/local/lib/calibre/logs/nginx-error.log;
 
    location / {
        # an HTTP header important enough to have its own Wikipedia entry:
        #   http://en.wikipedia.org/wiki/X-Forwarded-For
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
 
        # enable this if and only if you use HTTPS, this helps Rack
        # set the proper protocol for doing redirects:
        # proxy_set_header X-Forwarded-Proto https;
 
        # pass the Host: header from the client right along so redirects
        # can be set properly within the Rack application
        proxy_set_header Host $http_host;
 
        # we don't want nginx trying to do something clever with
        # redirects, we set the Host: header above already.
        proxy_redirect off;

        # Try to serve static files from nginx, no point in making an
        # *application* server like Unicorn/Rainbows! serve static files.
        if (!-f $request_filename) {
            proxy_pass http://books_app_server;
            break;
        }
    }

    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/calibre.example.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/calibre.example.com/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

}
 
server {
    if ($host = calibre.example.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


	listen	80;
    server_name calibre.example.com;
    return 404; # managed by Certbot


}

Add a user to Calibre so you can log in. Make sure the Calibre server is stopped first if it was running.

sudo systemctl stop calibre-server
sudo /opt/calibre/calibre-server --userdb /usr/local/lib/calibre/users.sqlite --manage-users

Enable and start the Calibre server service

sudo systemctl enable calibre-server
sudo systemctl start calibre-server