Api_v0.php not found / handled as a directory

,

Hello Ladies and Gentlemen,

I’m running LibreNMS 1.33 on a Debian 9, apache2 2.4.25, php7 + php7-fpm. This way it’s super fast, but the API is not working. Before I upgraded from Debian 8 and php 5.5 it worked fine.

Here a curl output on the api_v0.php

Here the rewrite log about the same curl earlier

Here you have a strace of the curl:

[pid 18375] open(“/dev/urandom”, O_RDONLY|O_CLOEXEC) = 20
[pid 18375] stat(“/opt/librenms/html/api_v0.php”, {st_mode=S_IFREG|0644, st_size=10402, …}) = 0
[pid 18375] open(“/opt/librenms/html/.htaccess”, O_RDONLY|O_CLOEXEC) = 20
[pid 18375] open(“/opt/librenms/html/api_v0.php/.htaccess”, O_RDONLY|O_CLOEXEC) = -1 ENOTDIR (Not a directory)
[pid 18375] stat(“/opt/librenms/html/api_v0.php”, {st_mode=S_IFREG|0644, st_size=10402, …}) = 0

Here a strace of a /api/v0/devices curl

Here my .htaccess

Validate.php output:

Component Version
LibreNMS 201605-3592-g13f0b9726
DB Schema 214
PHP 7.0.19-1
MySQL 10.1.26-MariaDB-0+deb9u1
RRDTool 1.6.0
SNMP NET-SNMP 5.7.3

Here you have my fpm include of the virtual host:

        <FilesMatch \.php$>
                SetHandler "proxy:unix:/run/php/php7.0-fpm.sock|fcgi://localhost"
        </FilesMatch>

I can access every file in my librenms/html directory, execept the api_v0.php. The file itself is accessable by www-data as i show you below:

-rw-r--r-- 1 librenms librenms 11K Oct 25 09:24 html/api_v0.php

librenms@server:/opt/librenms$ groups
librenms www-data

The Webgui is working fine. Only the api is not running at all. It seems that the api_v0.php file is not recognized as a php file but a directory.

Thank you in advance!

###Additions due to comments:

Zmegolaz33m
Hi! You should use curl -H ‘X-Auth-Token: YOURAPITOKENHERE’ https://librenms.org/api/v0 instead of accessing the api_v0.php file directly.

See https://docs.librenms.org/#API/#endpoints

Here a rewrite log of:
curl -si -H "X-Auth-Token: MYTOKEN" https://librenms.example.com/api/v0

Here a rewrite log of:
curl -si -H "X-Auth-Token: MYTOKEN" https://librenms.example.com/api/v0/devices

lafGreat contributor7m
Double check your apache config, if you’ve updated from an older version of apache then it has a slight difference.

https://docs.librenms.org/#Installation/Installation-Ubuntu-1604-Apache/#configure-apache

Here my VHost

Hi! You should use curl -H 'X-Auth-Token: YOURAPITOKENHERE' https://librenms.org/api/v0 instead of accessing the api_v0.php file directly.

See https://docs.librenms.org/#API/#endpoints

Hi,
I used your given curl too. The above given “Here a strace of a /api/v0/devices curl” is from
curl -H 'X-Auth-Token: YOURAPITOKENHERE' https://librenms.example.com/api/v0/devices

When I curl:
curl -H 'X-Auth-Token: YOURAPITOKENHERE' https://librenms.example.com/api/v0
I get redirected to /index.php and I receive the curl output of my librenms website.

Double check your apache config, if you’ve updated from an older version of apache then it has a slight difference.

https://docs.librenms.org/#Installation/Installation-Ubuntu-1604-Apache/#configure-apache

I added my VHost to the end of my Post. It seems to be correct for me.

Post what you have

Here my VHost

Looks ok. I don’t have that version of apache to test against. I can however confirm that it’s working ok for me on earlier versions but I don’t run php-fpm on apache.

Not sure what else to suggest :frowning:

Ok now, I have a nice story:

I copied the api_v0.php to html/plugins and opened librenms.example.com/plugins/api_v0.php.

It gave me this:

Warning: require(/opt/librenms/html/includes/init.php): failed to open stream: No such file or directory in /opt/librenms/html/plugins/api_v0.php on line 16

Fatal error: require(): Failed opening required ‘/opt/librenms/html/includes/init.php’ (include_path=‘.:/usr/share/php’) in /opt/librenms/html/plugins/api_v0.php on line 16

So I fixed line 16 to:
require realpath(__DIR__ . '/../..') . '/includes/init.php';

I again opened librenms.example.com/plugins/api_v0.php and it came back with the 404. So I ran the api_v0.php locally and it gave me this:

root@server:/opt/librenms# php html/plugins/api_v0.php

404 Page Not Foundbody{margin:0;padding:30px;font:12px/1.5 Helvetica,Arial,Verdana,sans-serif;}h1{margin:0;font-size:48px;font-weight:normal;line-height:48px;}strong{display:inline-block;width:65px;}404 Page Not Found

The page you are looking for could not be found. Check the address bar to ensure your URL is spelled correctly. If all else fails, you can visit our home page at the link below.

Visit the Home Page

So actually the api_v0.php was called the whole time but doesn’t seem to work?

You can’t move the file around like that, it won’t work.

Please note that when using php-fpm with nginx, we do this:

 location /api/v0 {
  try_files $uri $uri/ /api_v0.php?$query_string;
 }

You will need to replicate that as the api expects to be access via that url and not by api_v0.php.

Thanks for your advice. After hours of try and error I finally fixed it.

When you’re using apache2 + php-fpm, the .htaccess has to be modified. The rewrite rule that worked for me:
RewriteRule ^api/v0/(.*)$ fcgi://127.0.0.1:9000/opt/librenms/html/api_v0.php/$1 [P,L]

Here you can find the complete curl as my proof on pastebin. Maye you can add the Rewrite Rule to your Docs?

You can do that :slight_smile: