Multiple Calibre Servers under Mac OS X

So there’s this program out there called Calibre which, despite it’s pretty terrible UI, is pretty much the gold standard for managing eBooks. Seriously, it’s such a great program whose only fault is its terrible engineer UI.

One of the nice things that Calibre includes is a built-in web server that can serve books via OPDS. If you have an OPDS-compatible reader (I use Marvin), you can browse and download from your library directly on your device, basically creating your own private eBook cloud.

But, this presents a little bit of an issue. Namely, I don’t want all of my books to be publicly available, while still providing a subset of my library for visitors to browse and use. But I still want to be able to access them myself from my “private reserve collection.”

Fortunately, with a little bit of work, you can do that under Calibre.

Initial Setup

Start by creating two (or more) virtual libraries within Calibre.

One thing to note, any library other than the initial one should have no spaces in the name.

Next, set up your first server using the GUI. To do this, go to Preferences menu, then Preferences (again). In the Preferences window, find “Sharing over the net” and open that preference pane.

Note the server port (default is 8081), because you’ll need that in just a few minutes. Find the “Virtual Library to Apply” setting and select your first library. Then click “Start Server.” Wait a few seconds, then click “Test Server.” This should open a browser window and you should see a page that lets you navigate only your first library.

So far so good. On to the next step.

Multiple Servers and Libraries

Now it gets tricky (if you’re not used to the shell, that is). Open a Terminal window and type:

cd /Applications/calibre.app/Contents/MacOS/
ls

You should see something that looks like:

calibre           calibre-customize  calibre-parallel  calibre-smtp  ebook-convert  ebook-edit  ebook-polish  fetch-ebook-metadata  lrfviewer  markdown-calibre  web2disk
calibre-complete  calibre-debug      calibre-server    calibredb     ebook-device   ebook-meta  ebook-viewer  lrf2lrs               lrs2lrf    qt-plugins

The one we want here is calibre-server. Type the following command:

./calibre-server -p 8082 --virtual-library=Library2

Where Library2 is the name of the second virtual library you created above. Note that I incremented the port number from the initial server above.

Open a browser to http://localhost:8082 and you should see your second library. You can do this for as many libraries as you like. Just be sure to increment the port number for each additional server you run. Press Ctrl+C to end the server.

Launching on Reboot

So now you have this server, but you don’t want to keep a shell window open with it just to keep the server running, and you want it to start back up when you reboot.

Now, for the first library, I just have Mac OS X launching Calibre automatically, from the Login Items preferences in Users and Groups. I also use Calibre’s monitoring of a folder, so I need the actual program itself to launch at boot, so that takes care of the first library. But what about the second?

Well, this is where Mac OS X’s launchd comes in.

Back in your terminal window, type:

cd ~/Library/LaunchAgents/

Create a com.username.calibre-server.plist file, replacing username with your username. You can use vi, nano or any other editor of your choice. Put the following in the file:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Label</key>
    <string>com.username.calibre-server</string>
    <key>ProgramArguments</key>
    <array>
	    <string>/Applications/calibre.app/Contents/MacOS/calibre-server</string>
        <string>-p 8082</string>
        <string>--virtual-library=Library2</string>
    </array>
    <key>KeepAlive</key>
    <true/>
    <key>RunAtLoad</key>
    <true/>
</dict>
</plist>

Note that all we did here is recreate the command we ran above within the plist. Save that and exit your editor, then type:

launchctl load com.username.calibre-server.plist

And navigate to http://localhost:8082 in your browser. If everything worked, you should now see your second calibre library running. And this should load automatically after you reboot.

Bonus Setup: Pretty URLs!

Because I like pretty URLs, we’re going to set up pretty URLs for this one too. Because calibre.internal.domain is much nicer.

Mac OS X already has Apache installed, so no need to install it yourself. You just need to make a few quick changes.

cd /etc/apache2

Now, edit the httpd.conf file using vi, nano or any other editor of your choice. Be sure to use sudo because these files are owned by root. Find and uncomment the following lines:

# Find:
#LoadModule vhost_alias_module libexec/apache2/mod_vhost_alias.so

# Change To:
LoadModule vhost_alias_module libexec/apache2/mod_vhost_alias.so

# Find:
#Include /private/etc/apache2/extra/httpd-vhosts.conf

# Change To:
Include /private/etc/apache2/extra/httpd-vhosts.conf

Close and save. Now go to:

cd /etc/apache2/extra/

Now, edit the httpd-vhosts.conf file using vi, nano or any other editor of your choice. Be sure to use sudo because these files are owned by root. Add the following vhost:

<VirtualHost *:80>
    ServerName calibre.internal.domain

    ProxyRequests Off
    ProxyPreserveHost On

    ProxyPass / http://localhost:8081/
    ProxyPassReverse / http://localhost:8081/

   <Proxy *>
        Order deny,allow
        Allow from all
        Require all granted
    </Proxy>
</VirtualHost>

You’ll recognize what we’re doing here if you read my earlier article on pretty URLs. Basically, we’re proxying requests to the actual server on the correct port.

You can optionally create a new vhost for each library you run. If you do create additional vhosts, be sure you’re pointing each one at the correct ports. Once you’re finished, type

sudo apachectl restart

And navigate to calibre.internal.domain. If everything went okay, you should now see your second library, without any of the :8081 nastiness.

Did something I wrote help you out?

That's great! I don't earn any money from this site - I run no ads, sell no products and participate in no affiliate programs. I do this solely because it's fun; I enjoy writing and sharing what I learn.

All the same, if you found this article helpful and want to show your appreciation, here's my Amazon.com wishlist.

Read More

Pretty URLs - Serving Plex from behind a proxy using mod_proxy and Apache

I’m obsessed with pretty URLs. I admit it. I love looking at a properly formatted URL that just looks nice. I’m slowly converting our internal media library over to Plex now that it is available on the new AppleTV. In doing that, I noticed that the Plex web interface serves, by default, serves from port 32400. So the URL ends up looking somthing like this: http://172.16.104.4:32400/web/index.html Twitch.

Google Chrome, Mac OS X and Self-Signed SSL Certificates

I’ve been using Google Chrome as my primary browser for the last few months. Sorry, Firefox, but with all the stuff I need to work installed, you’re so slow as to be unusable. Up to and including having to force-quit at the end of the day. Chrome starts and stops quickly But that’s not the purpose of this entry. The purpose is how to live with self-signed SSL certificates and Google Chrome. Let’s say you have a server with a self-signed HTTP SSL certificate. Every time you hit a page, you get a nasty error message. You ignore it once and it’s fine for that browsing session. But when you restart, it’s back. Unlike Firefox, there’s no easy way to say “yes, I know what I’m doing, ignore this.” This is an oversight I wish Chromium would correct, but until they do, we have to hack our way around it. Caveat: these instructions are written for Mac OS X. PC instructions will be slightly different at PCs don’t have a keychain, and Google Chrome (unlike Firefox) uses the system keychain. So here’s how to get Google Chrome to play nicely with your self-signed SSL certificate: On your web server, copy the crt file (in my case, server.crt) over to your Macintosh. I scp'd it to my Desktop for ease of work. ** These directions has been updated. Thanks to Josh below for pointing out a slightly easier way.** In the address bar, click the little lock with the X. This will bring up a small information screen. Click the button that says “Certificate Information.” Click and drag the image to your desktop. It looks like a little certificate. Double-click it. This will bring up the Keychain Access utility. Enter your password to unlock it. Be sure you add the certificate to the System keychain, not the login keychain. Click “Always Trust,” even though this doesn’t seem to do anything. After it has been added, double-click it. You may have to authenticate again. Expand the “Trust” section. “When using this certificate,” set to “Always Trust” That’s it! Close Keychain Access and restart Chrome, and your self-signed certificate should be recognized now by the browser. This is one thing I hope Google/Chromium fixes soon as it should not be this difficult. Self-signed SSL certificates are used **a lot **in the business world, and there should be an easier way for someone who knows what they are doing to be able to ignore this error than copying certificates around and manually adding them to the system keychain.