This article is about setting up you own ebook library with support for the OPDS catalog format . The COPS ebook catalog can read a Calibre library and expose it as a website. It is developed on PHP.

Prerequisites

  • Prepare a Linux system. In this guide we are using Ubuntu 14.04.4.
  • Have a cops.example.com DNS A record pointing to the IP of your server.

Install necessary packages

$  sudo apt-get -y samba nginx php5-fpm php5-gd php5-sqlite php5-json php5-intl git

Setup a SAMBA share

Samba is needed for sharing the Calibre library directory with the library administrator. Alternatively you could use NFS or even OwnCloud.

Note

It is a security risk to expose Samba on a public server. Restrict it through firewall or use OwnCloud instead.

  • Tweak the Samba configuration file (/etc/samba/smb.conf):

    #  unix password sync = yes
       unix password sync = no
    
    #  pam password change = yes
       pam password change = no
    
  • Add a Samba share for calibre (/etc/samba/smb.conf):

    [calibre$]
    path = /srv/calibre
    valid users = calibre
    write list = calibre
    read only = no
  • Restart Samba services:

    $ sudo service smbd restart ; sudo service nmbd restart
    
  • Add the calibre user:

    $ useradd -m -d /srv/calibre -s /usr/sbin/nologin calibre
    $ smbpasswd -a calibre
    

Setup Calibre

  • Now you should setup Calibre on your PC. On Debian/Ubuntu:

    $ sudo apt-get -y install calibre
    

    Windows users can download it from here:

    http://calibre-ebook.com/download_windows

  • Configure Calibre to use the calibre share as its catalog:

    • You can “mount” the calibre share on Linux or “map” the share on Windows.
    • Create an ebook Directory under the calibre share.
    • Create a new Calibre library inside the ebook directory. From the Calibre menu:
      • Callibre Library –> Switch/create library -> Create an empty Library at the new location and fill the path in the “New Location” text field.
    • If you get an error about “Corrupted database” just click ‘Yes” to rebuilt it.
    • Start adding books

Setup COPS OPDS

  • Download the software:

    $ mkdir /var/www
    $ cd /var/www
    $ git clone https://github.com/seblucas/cops.git
    
  • Setup COPS:

    Under the /var/www/cops/ directory, copy the config_default.php.example to config_default.php:

    $ cp config_local.php.example config_local.php
    

    Make the following changes in config_default.php:

    <?php
        if (!isset($config))
            $config = array();
    
        /*
         * The directory containing calibre's metadata.db file, with sub-directories
         * containing all the formats.
         * BEWARE : it has to end with a /
         */
        $config['calibre_directory'] = '/srv/calibre/ebooks/';
    
        /*
         * Catalog's title
         */
        $config['cops_title_default'] = 'My Ebook Portal';
    
        $config['calibre_internal_directory'] = '/ebooks/';
    
        $config['cops_full_url'] = 'cops.example.com';
    
        $config['cops_x_accel_redirect'] = 'X-Accel-Redirect';
    
        /*
         * use URL rewriting for downloading of ebook in HTML catalog
         * See README for more information
         *  1 : enable
         *  0 : disable
         */
        $config['cops_use_url_rewriting'] = '1';

Setup nginx

  • Create an /etc/nginx/sites-available/cops.example.com

    server {
    
        listen 80 default_server;
        listen [::]:80 default_server ipv6only=on;
    
        server_name cops.example.com;
    
        access_log  /var/log/nginx/cops.access.log;
        error_log /var/log/nginx/cops.error.log;
        root   /var/www/cops;
        #index feed.php;
        index index.php;
    
        #Useful only for Kobo reader
        location /download/ {
              rewrite ^/download/(\d+)/(\d+)/.*\.(.*)$ /fetch.php?data=$1&db=$2&type=$3 last;
              rewrite ^/download/(\d+)/.*\.(.*)$ /fetch.php?data=$1&type=$2 last;
              break;
            }
    
            #Can break loading the images - if you don't see anything, comment
            location ~ ^/images.*\.(gif|png|ico|jpg)$ {
                    expires 31d;
            }
            #Can also break loading the images, comment if it happens
            location ~ .(js|css|eot|svg|woff|ttf)$ {
                    expires 31d;
            }
    
        #Not necessarily correct, it depends on distro.
        location ~ \.php$ {
           try_files $uri =404;
           include /etc/nginx/fastcgi_params;
           fastcgi_param   SCRIPT_FILENAME  $document_root$fastcgi_script_name;
           fastcgi_pass    unix:/run/php5-fpm.sock;
        }
    
        location /ebooks {
            root /srv/calibre;
            internal;
        }
    }

    Note:

    The feed.php setting was redirecting me to an XML site with this error: This XML file does not appear to have any style information associated with it. The document tree is shown below. So I am using index.php instead, as the index file.

  • Enable the cops.example.com site and disable the default:

    $ cd /etc/nginx/sites-enabled/
    $ sudo ln -s /etc/nginx/sites-available/cops.example.com
    $ sudo unlink default
    
  • Restart nginx and php5-fpm:

    $ sudo service php5-fpm restart ; sudo service nginx restart
    
  • Change permissions to let nginx write to the library:

    $ usermod -a -G calibre www-data
    $ chmod -R g+w /srv/calibre/ebooks/
    

Now you can navigate to http://cops.example.com and enjoy your newly created ebook library!

References