Calibre OPDS (COPS) eBook Server on Ubuntu Server

COPS or Calibre OPDS (and HTML) PHP Server is a lightweight eBook server. It allows you to sync your library via Dropbox or any other type of network share from your PC where you can use the Calibre desktop app to manage your library and COPS to server it up on your network or to the internet.

In this tutorial I’m going to setup COPS (using nginx and php5-fpm as the web server and php engine), use a network share to sync my library, and make it available over my network.

First thing to do is install nginx and php5.

sudo apt-get install nginx php5-fpm php5-gd php5-sqlite samba unzip
Hit y when prompted.

Create a location to store your library in your home directory

mkdir /home/allan/ebooks
nano /etc/samba/smb.conf

Scroll down to the bottom of the file into the Share Definitions section. There paste the following configuration lines.

[ebooks]
 comment = Calibre eBook Library
 path = /home/allan/ebooks
 valid users = allan
 browsable = yes
 guest ok = no
 read only = no
 create mask = 0755

Create a samba user and a password if you haven’t already. In this case the user is name allan, the password will be entered after you enter the last of the two lines.
sudo useradd allan
sudo smbpasswd -a allan

Then restart samba.

sudo service samba restart

You can now navigate to your server’s ip address in Windows Explorer: \\10.13.14.10

COPS-Network Share

Use the samba username and password you just created to access the ebooks folder. Once you’ve navigated into the folder move your Calibre library to this new location.

Now that your library is moved to the server you can finish setting up COPS.

The first step is to create a new nginx site.

sudo nano /etc/nginx/sites-available/cops

Paste in the following configuration.

server {

 listen 55555;

 server_name 10.13.14.10:55555;

 access_log /var/log/nginx/opds.access.log;
 error_log /var/log/nginx/opds.error.log;
 root /var/www/cops;
 index feed.php;

 location ~ \.php$ {
 try_files $uri =404;
 fastcgi_split_path_info ^(.+\.php)(/.+)$;
 fastcgi_pass unix:/var/run/php5-fpm.sock;
 fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
 fastcgi_index index.php;
 include fastcgi_params;

 }

 location /ebooks {
 root /home/allan;
 internal;
 }
}

Next create a link from sites-available to sites-enabled.

sudo ln -s /etc/nginx/sites-available/cops /etc/nginx/sites-enabled/cops

The last piece of the puzzle is to actually download and configure COPS. Move to your home directory (in case you’ve moved away) and download the zip file that contains COPS.

cd ~ (assuming you aren’t logged in as the root user, if so specify your home folder cd /home/allan)

wget https://github.com/seblucas/cops/releases/download/1.0.0RC3/cops-1.0.0RC3.zip

Unzip it to your /var/www/cops/ directory (if you haven’t created that directory this command will create it).

sudo unzip cops-1.0.0RC3.zip -d /var/www/cops/

Next copy the config_local.php.example file to config_local.php

sudo cp config_local.php.example config_local.php

Now edit this new config file and add make it look like the following.

sudo nano config_local.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'] = '/home/allan/ebooks/';

    /*
     * The URL to the calibre internal directory you specified in your nginx site
     * config file.
     */
    $config['calibre_internal_directory'] = '/ebooks/';

    /*
     * URL to access the COPS page
     */
    $config['cops_full_url'] = '10.13.14.10:55555';

    /*
     * Default timezone
     * Check following link for other timezones :
     * http://www.php.net/manual/en/timezones.php
     */
    $config['default_timezone'] = "America/Denver";

     /*
     * Wich header to use when downloading books outside the web directory
     * Possible values are :
     *   X-Accel-Redirect   : For Nginx
     *   X-Sendfile         : For Lightttpd or Apache (with mod_xsendfile)
     *   No value (default) : Let PHP handle the download
     */
    $config['cops_x_accel_redirect'] = "X-Accel-Redirect";

    /*
     * Catalog's title
     */
    $config['cops_title_default'] = "COPS";

    /*
     * use URL rewriting for downloading of ebook in HTML catalog
     * See README for more information
     *  1 : enable
     *  0 : disable
     */
    $config['cops_use_url_rewriting'] = "0";

    /*
     * split authors by first letter
     * 1 : Yes
     * 0 : No
     */
    $config['cops_author_split_first_letter'] = "0";

Leave a Reply

Your email address will not be published. Required fields are marked *