Calibre ebooks server Apache WSGI Application

Calibre ebooks server Apache WSGI Application

Once you've got a lot of ebooks in Calibre it is handy to have them available straight from your tablet. An easy way to do this is using the content server that is part of Calibre. If you have an Apache server it is fairly easy integrate the content server. There are two methods that can be used to do this:

  1. Proxy the content server using mod_proxy
  2. Run as an in-process server using WSGI (mod_wsgi under Apache)

mod_proxy is fairly well documented and works well for most people. I've used it for a few years but recently decided to give the mod_wsgi route a try. It was surprisingly easy to get working. Configuration files, wsgi script and notes are below.

Create WSGI application script

Firstly you'll need to create the WSGI application script that Apache will call when it needs to start an instance of the Calibre content server. Copy the following into /usr/lib/cgi-bin/calibre-wsgi-adapter.wsgi:

calibre-wsgi-adapter.wsgi

# WSGI script file to run calibre content server as a WSGI app
import sys, os

# You can get the paths referenced here by running
# calibre-debug --paths
# on your server

# The first entry from CALIBRE_PYTHON_PATH
sys.path.insert(0, '/usr/lib/calibre')

# CALIBRE_RESOURCES_PATH
sys.resources_location = '/usr/share/calibre'

# CALIBRE_EXTENSIONS_PATH
sys.extensions_location = '/usr/lib/calibre/calibre/plugins'

# Path to directory containing calibre executables
sys.executables_location = '/usr/bin'

# Path to a directory for which the server has read/write permissions
# calibre config will be stored here
os.environ['CALIBRE_CONFIG_DIRECTORY'] = '/storage/ebooks/calibre-config'

del sys
del os

from calibre.library.server.main import create_wsgi_app
application = create_wsgi_app(
        # The mount point of this WSGI application (i.e. the first argument to
        # the WSGIScriptAlias directive). Set to empty string is mounted at /
        prefix='/calibre',

        # Path to the calibre library to be served
        # The server process must have write permission for all files/dirs
        # in this directory or BAD things will happen
        path_to_library='/storage/ebooks/Calibre_Library',

        # The virtual library (restriction) to be used when serving this
        # library.
        virtual_library=None
)

del create_wsgi_app

Apache Virtual Host configuration

Add the following virtual host config file to the appropriate place in your Apache configuration.

e.g.: for Ubuntu put it in /etc/apache2/sites-available then enable it with a2ensite ebooks.domain.com

Restart Apache and navigate to https://ebooks.domain.com/calibre/ to browse your ebooks.

ebooks.domain.com.conf

<VirtualHost *:80>
	ServerName ebooks.domain.com
	ServerAdmin admin@ebooks.domain.com
	DocumentRoot /storage/ebooks/

        RewriteEngine on
        ReWriteCond %{SERVER_PORT} !^443$
        RewriteRule ^/(.*) https://%{HTTP_HOST}/$1 [NC,R,L]

</VirtualHost>
<IfModule mod_ssl.c>
	<VirtualHost *:443>
	ServerName ebooks.domain.com
	ServerAdmin admin@ebooks.domain.com
	DocumentRoot /storage/ebooks/
	<Directory /storage/ebooks>
		AllowOverride None
		Require all granted
	</Directory>
	DirectoryIndex index.md index.sd index.php index.html
	Header set Accept-Ranges "none"
	RequestHeader unset Accept-Encoding
        WSGIDaemonProcess calibressl processes=1 threads=3
        WSGIScriptAlias /calibre /usr/lib/cgi-bin/calibre-wsgi-adapter.wsgi
        <Directory /storage/ebooks/>
                WSGIProcessGroup calibressl
                WSGIApplicationGroup %{GLOBAL}
                Order deny,allow
                Allow from all
        </Directory>

	SSLEngine on
	SSLCertificateFile	/etc/ssl/server/ebooks.domain.com.crt
	SSLCertificateKeyFile   /etc/ssl/private/ebooks.domain.com.key
	BrowserMatch "MSIE [2-6]" \
			nokeepalive ssl-unclean-shutdown \
			downgrade-1.0 force-response-1.0
	BrowserMatch "MSIE [17-9]" ssl-unclean-shutdown
	</VirtualHost>
</IfModule>

Related Article