Integrating the calibre content server into apache

The calibre content server can be run directly, in process, inside a host server like Apache using the WSGI framework.

Note

For this to work, all the dependencies needed by calibre must be installed on your system. Doing so is highly non-trivial and you are encouraged not to use in process servers. You will not get any assistance with debugging in process server problems.

First, we have to create a WSGI adapter for the calibre content server. Here is a template you can use for the purpose. Replace the paths as directed in the comments

# 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, '/home/kovid/work/calibre/src')
# CALIBRE_RESOURCES_PATH
sys.resources_location = '/home/kovid/work/calibre/resources'
# CALIBRE_EXTENSIONS_PATH
sys.extensions_location = '/home/kovid/work/calibre/src/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'] = '/var/www/localhost/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='/home/kovid/documents/demo library',
        # The virtual library (restriction) to be used when serving this
        # library.
        virtual_library=None
)
del create_wsgi_app

Save this adapter as calibre-wsgi-adpater.py somewhere your server will have access to it.
Let’s suppose that we want to use WSGI in Apache. First enable WSGI in Apache by adding the following to httpd.conf:

LoadModule wsgi_module modules/mod_wsgi.so

The exact technique for enabling the wsgi module will vary depending on your Apache installation. Once you have the proxy modules enabled, add the following rules to httpd.conf (or if you are using virtual hosts to the conf file for the virtual host in question:

WSGIScriptAlias /calibre /var/www/localhost/cgi-bin/calibre-wsgi-adapter.py

Change the path to calibre-wsgi-adapter.py to wherever you saved it previously (make sure Apache has access to it).
That’s all, you will now be able to access the calibre Content Server under the /calibre URL in your apache server.

Note

For more help with using mod_wsgi in Apache, see mod_wsgi.

from: https://manual.calibre-ebook.com/server.html