API not working: <p>The requested URL /api/v0 was not found on this server.</p>

Tags: #<Tag:0x00007fed76437720>

When trying to access the API from another server I get 404 not found.

Example:

#curl -H 'X-Auth-Token: MYTOKEN' 'https://librenms.myurl.se/api/v0'
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>404 Not Found</title>
</head><body>
<h1>Not Found</h1>
<p>The requested URL /api/v0 was not found on this server.</p>
</body></html>

Here’s my Apache2 config:

[email protected]:~# cat /etc/apache2/sites-available/librenms.myurl.se.conf
<VirtualHost *:443>
        Protocols h2 http/1.1
        DocumentRoot /opt/librenms/html
        ServerName librenms.myurl.se
        SSLEngine on
        SSLCertificateFile /etc/apache2/cert/myurl/2019/wildcard.myurl.se.crt.pem
        SSLCertificateKeyFile /etc/apache2/cert/myurl/2019/wildcard.myurl.se.key.pem
        SSLCertificateChainFile /etc/apache2/cert/myurl/2019/wildcard.myurl.se.intermediate.pem
        AddDefaultCharset utf8
        AllowEncodedSlashes NoDecode
        <Directory /opt/librenms/html>
                AllowOverride All
                #Options FollowSymLinks MultiViews
                Options Indexes FollowSymLinks MultiViews
                Require all granted
        </Directory>
        <Directory /opt/librenms/html/plugins/Weathermap/>
                <Limit GET POST>
                        require ip myIP
                        require all denied
                </Limit>
        </Directory>
        # Enable http authorization headers
        <IfModule setenvif_module>
                SetEnvIfNoCase ^Authorization$ "(.+)" HTTP_AUTHORIZATION=$1
        </IfModule>
        <FilesMatch ".+\.php$">
                SetHandler "proxy:unix:/run/php-fpm-librenms.sock|fcgi://localhost"
        </FilesMatch>
        ErrorLog ${APACHE_LOG_DIR}/error.log
        LogLevel warn
        CustomLog ${APACHE_LOG_DIR}/access.log combined
        ServerSignature Off
</VirtualHost>

Result from Validate:

[email protected]:/opt/librenms# ./validate.php
====================================
Component | Version
--------- | -------
LibreNMS  | 1.68-53-gb32aa03d5
DB Schema | 2020_10_03_1000_add_primary_key_transport_group_transport (186)
PHP       | 7.2.24-0ubuntu0.18.04.7
Python    | 3.6.9
MySQL     | 10.3.24-MariaDB-1:10.3.24+maria~bionic
RRDTool   | 1.7.0
SNMP      | NET-SNMP 5.7.3
====================================

[OK]    Composer Version: 1.10.15
[OK]    Dependencies up-to-date.
[OK]    Database connection successful
[OK]    Database schema correct
[WARN]  PHP version 7.3 is the minimum supported version as of November, 2020. We recommend you update PHP to a supported version (7.4 suggested) to continue to receive updates. If you do not update PHP, LibreNMS will continue to function but stop receiving bug fixes and updates.
[WARN]  IPv6 is disabled on your server, you will not be able to add IPv6 devices.
[email protected]:/opt/librenms#

check https://docs.librenms.org/Installation/Installation-Ubuntu-1804-Apache/

make sure you enabled rewrite mod for apache

a2enmod rewrite
systemctl restart apache2

API url is a rewrite rule in the .htaccess

Thank you for the answer but the problem remains. Rewrite is enabled.

[email protected]:/opt/librenms# a2enmod rewrite
Module rewrite already enabled
[email protected]:/opt/librenms# systemctl restart apache2
[email protected]:/opt/librenms#

hmm check your .htaccess exists in /opt/librenms/html

Otherwise it might be something else interferring in your apache config, i don’t use it but for example this isn’t in the guide:
<FilesMatch “.+.php$”>
SetHandler “proxy:unix:/run/php-fpm-librenms.sock|fcgi://localhost”

Try to get your apache and php settings to match exactly here first https://docs.librenms.org/Installation/Installation-Ubuntu-1804-Apache/

Those lines are for enabling PHP-FPM per: https://docs.librenms.org/Installation/Install-LibreNMS/
I want PHP-FPM as the WebUI seems much snappier with it. I can’t see how that would affect apache2 ignoring the .htaccess.

[email protected]:/opt/librenms# cat /opt/librenms/html/.htaccess
#  DO NOT CHANGE THIS FILE
#  If you need to change this file, you are doing something wrong.

Options +FollowSymlinks +Multiviews

AddType image/svg+xml .svg
<IfModule mod_filter.c>
  <IfModule mod_deflate.c>
    AddOutputFilterByType DEFLATE text/css
    AddOutputFilterByType DEFLATE application/javascript
    AddOutputFilterByType DEFLATE text/javascript
    AddOutputFilterByType DEFLATE application/x-javascript
    AddOutputFilterByType DEFLATE image/svg+xml
    AddOutputFilterByType DEFLATE text/plain
    AddOutputFilterByType DEFLATE text/xsd
    AddOutputFilterByType DEFLATE text/xsl
    AddOutputFilterByType DEFLATE text/xml
    AddOutputFilterByType DEFLATE image/x-icon
  </IfModule>
</IfModule>

RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !\.(js|ico|txt|gif|jpg|png|css|php)
RewriteRule ^api/v0(.*)$ api_v0.php/$1 [L]
RewriteCond %{REQUEST_URI} !=/server-status
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !\.(js|ico|txt|gif|jpg|png|css|php)
RewriteRule ^(.*)$ index.php

AcceptPathInfo On
[email protected]:/opt/librenms#

Tried the composer_wrapper to see if I’ve missed any packages

[email protected]:/opt/librenms# su - librenms
$ ./scripts/composer_wrapper.php install --no-dev
> LibreNMS\ComposerHelper::preInstall
Loading composer repositories with package information
Installing dependencies from lock file
Nothing to install or update

Tried the apt install one-liner

[email protected]:/opt/librenms# apt install curl apache2 composer fping git graphviz imagemagick libapache2-mod-php7.2 mariadb-client mariadb-server mtr-tiny nmap php7.2-cli php7.2-curl php7.2-gd php7.2-json php7.2-mbstring php7.2-mysql php7.2-snmp php7.2-xml php7.2-zip python-memcache python-mysqldb rrdtool snmp snmpd whois python3-pip
Reading package lists... Done
Building dependency tree
Reading state information... Done
mtr-tiny is already the newest version (0.92-1).
nmap is already the newest version (7.60-1ubuntu5).
python-mysqldb is already the newest version (1.3.10-1build1).
rrdtool is already the newest version (1.7.0-1build1).
whois is already the newest version (5.3.0).
composer is already the newest version (1.6.3-1).
fping is already the newest version (4.0-6).
graphviz is already the newest version (2.40.1-2).
apache2 is already the newest version (2.4.29-1ubuntu4.14).
curl is already the newest version (7.58.0-2ubuntu3.10).
git is already the newest version (1:2.17.1-1ubuntu0.7).
imagemagick is already the newest version (8:6.9.7.4+dfsg-16ubuntu6.8).
libapache2-mod-php7.2 is already the newest version (7.2.24-0ubuntu0.18.04.7).
php7.2-cli is already the newest version (7.2.24-0ubuntu0.18.04.7).
php7.2-curl is already the newest version (7.2.24-0ubuntu0.18.04.7).
php7.2-gd is already the newest version (7.2.24-0ubuntu0.18.04.7).
php7.2-json is already the newest version (7.2.24-0ubuntu0.18.04.7).
php7.2-mysql is already the newest version (7.2.24-0ubuntu0.18.04.7).
php7.2-snmp is already the newest version (7.2.24-0ubuntu0.18.04.7).
php7.2-xml is already the newest version (7.2.24-0ubuntu0.18.04.7).
python-memcache is already the newest version (1.57-2ubuntu0.18.04.1).
snmp is already the newest version (5.7.3+dfsg-1.8ubuntu3.6).
snmpd is already the newest version (5.7.3+dfsg-1.8ubuntu3.6).
php7.2-mbstring is already the newest version (7.2.24-0ubuntu0.18.04.7).
php7.2-zip is already the newest version (7.2.24-0ubuntu0.18.04.7).
python3-pip is already the newest version (9.0.1-2.3~ubuntu1.18.04.3).
The following additional packages will be installed:
  mariadb-client-10.3 mariadb-client-core-10.3 mariadb-common mariadb-server-10.3 mariadb-server-core-10.3
Suggested packages:
  mailx mariadb-test tinyca
The following packages will be upgraded:
  mariadb-client mariadb-client-10.3 mariadb-client-core-10.3 mariadb-common mariadb-server mariadb-server-10.3 mariadb-server-core-10.3
7 upgraded, 0 newly installed, 0 to remove and 6 not upgraded.

Dont want to upgrade mariadb atm as it will make holes in my graphs but this shouldn’t affect Apache2 ignoring the .htaccess either.

Ah ok, not sure then but pretty sure its just something to do with the apache config, do you have another default conf file like /etc/apache2/sites-enabled/000-default if yes, double check the AllowOverride setting is set like this

AllowOverride All

Also as another test change the order in your libre conf file to

<Directory "/opt/librenms/html/">
    Require all granted
    AllowOverride All
    Options Indexes FollowSymLinks MultiViews
  </Directory>

Doesn’t look like you are listening on port 80 in that config file either, do you have another?

Sadly, changing the order didn’t help. No we don’t listen on Port 80, only HTTPS.

000-default is removed

[email protected]:/opt/librenms# ls -l /etc/apache2/sites-enabled/
total 0
lrwxrwxrwx 1 root root 38 Feb  9  2018 librenms.myurl.se.conf -> ../sites-available/librenms.myurl.se.conf

The workaround here is to just access the API via the hosts IPnumber instead of hostname.

Then everything works as intended.