This article is about setting up you own ebook library with support for the [OPDS](https://en.wikipedia.org/wiki/OPDS) catalog format . The COPS ebook catalog can read a [Calibre](https://en.wikipedia.org/wiki/Calibre_%28software%29) 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](https://en.wikipedia.org/wiki/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*:

“`
**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
———-
* https://github.com/seblucas/cops
* http://blog.slucas.fr/en/oss/calibre-opds-php-server
* https://github.com/seblucas/cops/wiki/Full-example-with-Nginx