Calibre, a free and open-source e-book manager, is a powerful tool that allows you to manage your ebook library, access your books from anywhere, and easily share them with others. In this tutorial, we will guide you through the process of installing the Calibre server on a Ubuntu 22.04 machine. By following these steps, you can set up a Calibre server and start enjoying the benefits of a centralized ebook library.

Prerequisites

Before we begin, make sure you have the following prerequisites:

  • A server running Ubuntu 22.04.
  • A non-root user with sudo privileges.
  • The Uncomplicated Firewall (UFW) enabled and running.
  • A Fully Qualified Domain Name (FQDN) pointed to your server. For this tutorial, we will use the domain calibre.example.com.
  • Ensure that your system is up to date by running the following commands:
sudo apt update && sudo apt upgrade

Step 1 – Configure Firewall

The first step in the installation process is to configure the firewall to allow HTTP and HTTPS connections. This will ensure that your Calibre server can be accessed securely.

To check the status of the firewall, run the following command:

sudo ufw status

You should see a list of rules that allow certain connections. To allow HTTP and HTTPS connections, run the following commands:

sudo ufw allow http
sudo ufw allow https

Confirm the changes by running sudo ufw status again. You should now see the HTTP and HTTPS ports listed as allowed.

Step 2 – Download and Install Calibre Server

While Ubuntu 22.04 ships with Calibre, it is recommended to install the latest version directly from the official website. To do this, we need to install some dependencies first. Run the following command to install the necessary dependencies:

sudo apt install libopengl0 libxkbcommon-x11-0 libegl1 libfontconfig libgl1-mesa-glx

Once the dependencies are installed, we can proceed to download and install the Calibre server. Start by downloading the Calibre server installer using the following command:

wget https://download.calibre-ebook.com/linux-installer.sh

After the download is complete, make the installer script executable with the following command:

chmod +x ./linux-installer.sh

Finally, run the installer with administrative privileges:

sudo ./linux-installer.sh

During the installation, you may encounter warnings indicating that the installer expects a desktop environment rather than a server. You can safely ignore these warnings.

Step 3 – Create a Calibre Library and Add Your First Book

Now that the Calibre server is installed, we can proceed to create a Calibre library and add our first book. For this tutorial, we will download “The Adventures of Sherlock Holmes” by Arthur Conan Doyle from Project Gutenberg.

Start by creating a directory for the Calibre library:

mkdir calibre-library

Next, download the book in MOBI format to the library directory:

wget http://www.gutenberg.org/ebooks/1661.kindle.noimages -O calibre-library/adventuresofsherlockholmes.mobi

You can also add multiple books at once by using the calibredb command. Simply place all the books you want to add in the library directory and run the following command:

calibredb add calibre-library/*.mobi --with-library calibre-library/

Step 4 – Start Calibre Server

With the library and books in place, we can now start the Calibre server. Run the following command to start the server:

calibre-server calibre-library

The Calibre server will start and listen on port 8080. To access the server from your browser, you need to open port 8080 by running the following command:

sudo ufw allow 8080

Now, open your browser and enter the URL http://<your_server_ip>:8080/. You will see the Calibre web interface, where you can browse and read your ebooks.

To stop the Calibre server, simply press Ctrl + C in the terminal where it is running.

Step 5 – Create a systemd Service File

To make the Calibre server process persistent and survive reboots, we need to create a systemd service file. This will allow the server to start automatically on system boot.

Create a file called calibre-server.service in the /etc/systemd/system/ directory:

sudo nano /etc/systemd/system/calibre-server.service

Paste the following code into the file:

[Unit]
Description=Calibre Server
After=network.target

[Service]
Type=simple
User=shapehost
Group=shapehost
ExecStart=/usr/bin/calibre-server /home/shapehost/calibre-library --enable-local-write

[Install]
WantedBy=multi-user.target

Replace shapehost with your username and save the file by pressing Ctrl + X, followed by Y.

Reload the systemd daemon to apply the changes:

sudo systemctl daemon-reload

Enable the Calibre service to start on boot:

sudo systemctl enable calibre-server

Start the Calibre service:

sudo systemctl start calibre-server

To check the status of the service, run the following command:

sudo systemctl status calibre-server

The output should indicate that the Calibre server is active and running.

Step 6 – Enable User Authentication

By default, the Calibre server does not require authentication to access the library. To add an extra layer of security, we can enable user authentication.

First, stop the Calibre server:

sudo systemctl stop calibre-server

Next, start the Calibre user management script to add a new user:

sudo calibre-server --manage-users

Choose option 1 to add a new user, then enter a username and password when prompted. For example, you can use “admin” as the username and choose a strong password.

Once the user is added, you need to edit the service file to enable authentication. Open the file for editing:

sudo nano /etc/systemd/system/calibre-server.service

Add the --enable-auth flag at the end of the ExecStart line:

...
ExecStart=/usr/bin/calibre-server /home/shapehost/calibre-library --enable-local-write --enable-auth
...

Save the file and exit the editor.

Reload the systemd daemon:

sudo systemctl daemon-reload

Start the Calibre service again:

sudo systemctl start calibre-server

Now, when you access the Calibre server in your browser, you will be prompted to enter the username and password you created earlier.

Step 7 – Automatically Add Books to the Library

Calibre provides a convenient way to automatically add books to the library by using a Cron job. This allows you to upload books to a specific directory, and Calibre will periodically scan the directory and add any new books it finds.

First, create a watch directory where you will upload the books:

mkdir ~/calibre-watch

Download a book in MOBI format to the watch directory. For example, you can download “War and Peace” by Leo Tolstoy from Project Gutenberg:

wget https://www.gutenberg.org/ebooks/2600.kindle.images-O~/calibre-watch/warandpeace.mobi

Open the Crontab editor:

crontab -e

Add the following line at the end of the file to run the Cron job every 5 minutes:

*/5 * * * * calibredb add /home/shapehost/calibre-watch/ -r --with-library http://localhost:8080#calibre-library --username admin --password StrongPassword! && rm -r /home/shapehost/calibre-watch/*

Replace shapehost with your username and admin with the username you created earlier. Also, make sure to use a strong password for added security.

Save the file and exit the editor.

This Cron job will add any books found in the watch directory to the Calibre library and then remove the original files. You can modify the frequency of the Cron job by adjusting the timing settings.

Step 8 – Install Nginx

To provide secure access to the Calibre server over HTTPS, we will use Nginx as a reverse proxy. Nginx will handle the SSL termination and proxy requests to the Calibre server.

Start by adding the Nginx repository and installing Nginx:

curl https://nginx.org/keys/nginx_signing.key | gpg --dearmor | sudo tee /usr/share/keyrings/nginx-archive-keyring.gpg >/dev/null
echo "deb [signed-by=/usr/share/keyrings/nginx-archive-keyring.gpg] http://nginx.org/packages/ubuntu $(lsb_release -cs) nginx" | sudo tee /etc/apt/sources.list.d/nginx.list
sudo apt update
sudo apt install nginx

Verify the installation by checking the Nginx version:

nginx -v

Step 9 – Install SSL

To secure the connection to your Calibre server, we will use Let’s Encrypt to generate and install an SSL certificate. Let’s Encrypt provides free SSL certificates that are trusted by most modern browsers.

First, install Certbot, the Let’s Encrypt client:

sudo snap install core
sudo snap install --classic certbot
sudo ln -s /snap/bin/certbot /usr/bin/certbot

Generate the SSL certificate by running the following command:

sudo certbot certonly --standalone --agree-tos --no-eff-email --staple-ocsp --preferred-challenges http -m name@example.com -d calibre.example.com

Make sure to replace name@example.com with your email address and calibre.example.com with your domain name.

To generate a Diffie-Hellman group certificate, run the following command:

sudo openssl dhparam-dsaparam -out /etc/ssl/certs/dhparam.pem 4096

Step 10 – Configure Nginx

Now that we have installed Nginx and obtained the SSL certificate, we can proceed to configure Nginx to proxy requests to the Calibre server.

Open the Nginx configuration file for Calibre:

sudo nano /etc/nginx/conf.d/calibre.conf

Paste the following configuration into the file:

server {
listen 443 ssl http2;
listen [::]:443 ssl http2;

server_name calibre.example.com;

client_max_body_size 50M;

access_log /var/log/nginx/calibre.access.log;
error_log /var/log/nginx/calibre.error.log;

ssl_certificate /etc/letsencrypt/live/calibre.example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/calibre.example.com/privkey.pem;
ssl_trusted_certificate /etc/letsencrypt/live/calibre.example.com/chain.pem;

ssl_session_timeout 5m;
ssl_session_cache shared:MozSSL:10m;
ssl_session_tickets off;

ssl_protocols TLSv1.2 TLSv1.3;
ssl_prefer_server_ciphers on;
ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384;
ssl_ecdh_curve X25519:prime256v1:secp384r1:secp521r1;
ssl_stapling on;
ssl_stapling_verify on;
ssl_dhparam /etc/ssl/certs/dhparam.pem;

location / {
proxy_pass http://127.0.0.1:8080;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme; 
}
}

server {
listen 80;
listen [::]:80;
server_name calibre.example.com;
return 301 https://$host$request_uri;
}

Replace calibre.example.com with your domain name.

Save the file and exit the editor.

Check the Nginx configuration for any syntax errors:

sudo nginx -t

If there are no errors, restart Nginx to apply the changes:

sudo systemctl restart nginx

Conclusion

Congratulations! You have successfully installed and configured the Calibre Ebook Server on your Ubuntu 22.04 machine. With Calibre and Nginx working together, you can securely access your ebook library from anywhere and easily manage your collection. If you have any questions or encounter any issues, feel free to reach out to our support team for assistance.

Remember, at Shape.host, we offer reliable and scalable Cloud VPS solutions to help businesses like yours thrive in the digital world. Our team of experts is dedicated to delivering top-notch performance and security, ensuring that your hosting experience is smooth and hassle-free. Visit Shape.host today to learn more about our services and how we can empower your online presence.