[Koha] Step by step guide to enable REST API koha 20.11

Javi Legido javi at legido.com
Fri Mar 26 17:48:51 NZDT 2021


Hi there.

I would like to enable REST API, but so far i got 404 while trying to
access:

http://mykoha-intra.mydnsname.org/api/v1/.html

http://mykoha.myDNSname.org/api/v1/.html

I enabled below options:

 OPACBaseURL => http://mykoha.myDNSname.org
 OpacPublic
 RESTBasicAuth
 RESTOAuth2ClientCredentials
 RESTPublicAnonymousRequests
 RESTPublicAPI

I see below errors in '/var/log/koha/mykoha/plack.log':

172.17.0.1 - - [26/Mar/2021:04:42:52 +0000] "GET /api/v1/app.pl/api/v1/.html
HTTP/1.1" 404 - "-" "Mozilla/5.0 (X11; Linux x86_64; rv:78.0)
Gecko/20100101 Firefox/78.0"

Many thanks in advice.

Now verbose things, configuration files and step by step ways to reproduce
the issue:

1. Configuration files

/etc/apache2/sites-enabled/mykoha.conf

# Koha instance mykoha Apache config.

# OPAC
<VirtualHost *:80>
  <IfVersion >= 2.4>
   Define instance "mykoha"
  </IfVersion>
   Include /etc/koha/apache-shared.conf
#  Include /etc/koha/apache-shared-disable.conf
  Include /etc/koha/apache-shared-opac-plack.conf
   Include /etc/koha/apache-shared-opac.conf

   ServerName mykoha.myDNSname.org
   SetEnv KOHA_CONF "/etc/koha/sites/mykoha/koha-conf.xml"
   AssignUserID mykoha-koha mykoha-koha

   ErrorLog    /var/log/koha/mykoha/opac-error.log
#  TransferLog /var/log/koha/mykoha/opac-access.log
</VirtualHost>

# Intranet
<VirtualHost *:80>
  <IfVersion >= 2.4>
   Define instance "mykoha"
  </IfVersion>
   Include /etc/koha/apache-shared.conf
#  Include /etc/koha/apache-shared-disable.conf
  Include /etc/koha/apache-shared-intranet-plack.conf
   Include /etc/koha/apache-shared-intranet.conf

   ServerName mykoha-intra.myDNSname.org
   SetEnv KOHA_CONF "/etc/koha/sites/mykoha/koha-conf.xml"
   AssignUserID mykoha-koha mykoha-koha

   ErrorLog    /var/log/koha/mykoha/intranet-error.log
#  TransferLog /var/log/koha/mykoha/intranet-access.log
</VirtualHost>

/etc/koha/apache-shared-opac-plack.conf

# Apache configuration settings that are shared for every Koha instance.
# This file contains settings for the Plack configuration of the OPAC.
#
# This file should be included from an instance's
# /etc/apache2/site-available file, from within the VirtualHost section
# for the OPAC.

# Plack is only available out-of-the-box for Apache 2.4.7+ setups
<IfVersion >= 2.4.8>
    <IfModule mod_proxy_http.c>

        ProxyPreserveHost On

        RequestHeader set X-FORWARDED-PROTO "https" env=HTTPS

        # Point the OPAC site to Plack
        ProxyPass /index.html "unix:/var/run/koha/${instance}/plack.sock|
http://localhost/opac/opac-main.pl"
        ProxyPassReverse /index.html
"unix:/var/run/koha/${instance}/plack.sock|
http://localhost/opac/opac-main.pl"
        ProxyPass /cgi-bin/koha "unix:/var/run/koha/${instance}/plack.sock|
http://localhost/opac"
        ProxyPassReverse /cgi-bin/koha
"unix:/var/run/koha/${instance}/plack.sock|http://localhost/opac"

        # Point the /api endpoint to Plack
        RewriteCond %{REQUEST_URI} !^/api/v[0-1]+/app.pl
        RewriteRule ^/api/(v[0-9]+)/(.*)$ /api/$1/app.pl/api/$1/$2 [L,PT]

        ProxyPass /api "unix:/var/run/koha/${instance}/plack.sock|
http://localhost/api"
        ProxyPassReverse /api "unix:/var/run/koha/${instance}/plack.sock|
http://localhost/api"

    </IfModule>
</IfVersion>

/etc/koha/apache-shared-intranet-plack.conf

# Apache configuration settings that are shared for every Koha instance.
# This file contains settings for the Plack configuration of the intranet.
#
# This file should be included from an instance's
# /etc/apache2/site-available file, from within the VirtualHost section
# for the intranet.

# Plack is only available out-of-the-box for Apache 2.4.8+ setups
<IfVersion >= 2.4.8>
    <IfModule mod_proxy_http.c>

        # FIXME: These scripts should be fixed so they
        # don't break under plack/starman
        ProxyPass "/cgi-bin/koha/offline_circ/process_koc.pl" "!"
        ProxyPass "/cgi-bin/koha/tools/background-job-progress.pl" "!"
        ProxyPass "/cgi-bin/koha/tools/batchMod.pl" "!"
        ProxyPass "/cgi-bin/koha/tools/export.pl" "!"
        ProxyPass "/cgi-bin/koha/tools/manage-marc-import.pl" "!"
        ProxyPass "/cgi-bin/koha/tools/stage-marc-import.pl" "!"
        ProxyPass "/cgi-bin/koha/tools/upload-cover-image.pl" "!"
        ProxyPass "/cgi-bin/koha/svc/cataloguing/metasearch" "!"

        ProxyPreserveHost On

        RequestHeader set X-FORWARDED-PROTO "https" env=HTTPS

        # Point the intranet site to Plack
        ProxyPass /index.html "unix:/var/run/koha/${instance}/plack.sock|
http://localhost/intranet/mainpage.pl"
        ProxyPassReverse /index.html
"unix:/var/run/koha/${instance}/plack.sock|
http://localhost/intranet/mainpage.pl"
        ProxyPass /cgi-bin/koha "unix:/var/run/koha/${instance}/plack.sock|
http://localhost/intranet"
        ProxyPassReverse /cgi-bin/koha
"unix:/var/run/koha/${instance}/plack.sock|http://localhost/intranet"

        # Point the /api endpoint to Plack
        RewriteCond %{REQUEST_URI} !^/api/v[0-1]+/app.pl
        RewriteRule ^/api/(v[0-9]+)/(.*)$ /api/$1/app.pl/api/$1/$2 [L,PT]

        ProxyPass /api "unix:/var/run/koha/${instance}/plack.sock|
http://localhost/api"
        ProxyPassReverse /api "unix:/var/run/koha/${instance}/plack.sock|
http://localhost/api"

    </IfModule>
</IfVersion>

2. Step by step way to reproduce the issue

docker stop test && docker rm test
docker run \
  --name test \
  --cap-add=SYS_NICE \
  --cap-add=DAC_READ_SEARCH \
  -p 8080:8080 \
  -p 80:80 \
  -d debian:testing \
bin/sh -c "while true; do echo hello world; sleep 1; done"
docker exec -ti test bash

apt-get update && apt-get install -y \
  wget \
  gnupg

wget -q -O- https://debian.koha-community.org/koha/gpg.asc | apt-key add -

echo 'deb http://debian.koha-community.org/koha stable main' | tee
/etc/apt/sources.list.d/koha.list

apt-get update && apt-get install -y \
  koha-common \
  mariadb-server

service mariadb start

a2enmod rewrite \
  cgi \
  headers \
  proxy_http \
  && a2dissite 000-default

service apache2 restart

koha-create --create-db mykoha

koha-plack --enable mykoha

koha-plack --start mykoha

service apache2 restart


More information about the Koha mailing list