Broke my APIs on Upgrade

Forum for questions and support relating to the 1.32.x releases only.
databoy2k
Posts: 28
Joined: Thu Nov 16, 2017 5:20 pm

Broke my APIs on Upgrade

Post by databoy2k »

Hey All:

The usual "done screwed something up" message that we all get flooded with after an upgrade.

http://[ip]/zm/api/ returns the following:

Code: Select all

Warning (2): Use of undefined constant ZM_OPT_USE_API - assumed 'ZM_OPT_USE_API' (this will throw an Error in a future version of PHP) [APP/Controller/AppController.php, line 63]
Warning (2): ini_set() [<a href='http://php.net/function.ini-set'>function.ini-set</a>]: Headers already sent. You cannot change the session module's ini settings at this time [CORE/Cake/Model/Datasource/CakeSession.php, line 581]
CakePHP: the rapid development php framework
Warning (2): ini_set() [<a href='http://php.net/function.ini-set'>function.ini-set</a>]: Headers already sent. You cannot change the session module's ini settings at this time [CORE/Cake/Model/Datasource/CakeSession.php, line 581]
Fatal error: Uncaught FatalErrorException: [CakeSessionException] Unable to configure the session, setting session.use_trans_sid failed. #0 /usr/share/zoneminder/www/api/lib/Cake/Model/Datasource/CakeSession.php(732): CakeSession::_configureSession() #1 /usr/share/zoneminder/www/api/lib/Cake/Model/Datasource/CakeSession.php(219): CakeSession::_startSession() #2 /usr/share/zoneminder/www/api/lib/Cake/Model/Datasource/CakeSession.php(248): CakeSession::start() #3 /usr/share/zoneminder/www/api/lib/Cake/View/Helper/SessionHelper.php(134): CakeSession::check('Message.flash') #4 /usr/share/zoneminder/www/api/app/View/Layouts/error.ctp(46): SessionHelper->flash() #5 /usr/share/zoneminder/www/api/lib/Cake/View/View.php(971): include('/usr/share/zone...') #6 /usr/share/zoneminder/www/api/lib/Cake/View/View.php(933): View->_evaluate('/usr/share/zone...', Array) #7 /usr/share/zoneminder/www/api/lib/Cake/View/View.php(546): View->_render('/usr/share/zone...') #8 /usr/share/zoneminder/www/api/lib/Cake/View/View.php(481): View->renderL in /usr/share/zoneminder/www/api/lib/Cake/Error/ErrorHandler.php on line 138
I can access the main install and it seems to work, but my APIs are non-responsive.

Any tips on what I did wrong? 1.32.1 on Ubuntu Server 18.04, upgraded using an update to the iconnor PPA and then apt install zoneminder. I'm using a LEMP server rather than LAMP, but nothing in the server should have changed, right?
security_dude
Posts: 23
Joined: Sun Oct 07, 2018 6:55 pm

Re: Broke my APIs on Upgrade

Post by security_dude »

This maybe a silly question but I had to ask, did you enable it under 'options' ==> 'system'? The "OPT_USE_API" option should be checked and then the server should be rebooted for the changes to take affect.
rockedge
Posts: 1173
Joined: Fri Apr 04, 2014 1:46 pm
Location: Connecticut,USA

Re: Broke my APIs on Upgrade

Post by rockedge »

restart the web server as well. There are some slightly different configs that need to be enabled.
databoy2k
Posts: 28
Joined: Thu Nov 16, 2017 5:20 pm

Re: Broke my APIs on Upgrade

Post by databoy2k »

I've got OPT_USE_API checked. I've also rebooted the server as a whole (because I can do that on mine - ZM's the most critical software it runs). Neither have fixed it. Did get some evolution in the error page, though:
Warning (2): Use of undefined constant ZM_OPT_USE_API - assumed 'ZM_OPT_USE_API' (this will throw an Error in a future version of PHP) [APP/Controller/AppController.php, line 63]
Warning (2): ini_set() [<a href='http://php.net/function.ini-set'>function.ini-set</a>]: Headers already sent. You cannot change the session module's ini settings at this time [CORE/Cake/Model/Datasource/CakeSession.php, line 581]
CakePHP: the rapid development php framework
Fatal Error
Error: require_once() [<a href='http://php.net/function.require'>function.require</a>]: Failed opening required '../../../includes/config.php' (include_path='.:/usr/share/php')
File: /usr/share/zoneminder/www/api/app/Config/bootstrap.php
Line: 123

Notice: If you want to customize this error message, create app/View/Errors/fatal_error.ctp

CakePHP: the rapid development php framework
CakePHP 2.10.8

Warning (2): Use of undefined constant ZM_DB_USER - assumed 'ZM_DB_USER' (this will throw an Error in a future version of PHP) [CORE/Cake/Model/ConnectionManager.php, line 69]
Warning (2): Use of undefined constant ZM_DB_PASS - assumed 'ZM_DB_PASS' (this will throw an Error in a future version of PHP) [CORE/Cake/Model/ConnectionManager.php, line 69]
Warning (2): Use of undefined constant ZM_DB_NAME - assumed 'ZM_DB_NAME' (this will throw an Error in a future version of PHP) [CORE/Cake/Model/ConnectionManager.php, line 69]
Warning (2): Use of undefined constant ZM_DB_SSL_CA_CERT - assumed 'ZM_DB_SSL_CA_CERT' (this will throw an Error in a future version of PHP) [CORE/Cake/Model/ConnectionManager.php, line 69]
Warning (2): Use of undefined constant ZM_DB_SSL_CLIENT_KEY - assumed 'ZM_DB_SSL_CLIENT_KEY' (this will throw an Error in a future version of PHP) [CORE/Cake/Model/ConnectionManager.php, line 69]
Warning (2): Use of undefined constant ZM_DB_SSL_CLIENT_CERT - assumed 'ZM_DB_SSL_CLIENT_CERT' (this will throw an Error in a future version of PHP) [CORE/Cake/Model/ConnectionManager.php, line 69]
Warning (2): Use of undefined constant ZM_DB_HOST - assumed 'ZM_DB_HOST' (this will throw an Error in a future version of PHP) [APP/Config/database.php, line 92]
Warning (2): Use of undefined constant ZM_DB_HOST - assumed 'ZM_DB_HOST' (this will throw an Error in a future version of PHP) [APP/Config/database.php, line 101]
User avatar
knight-of-ni
Posts: 2404
Joined: Thu Oct 18, 2007 1:55 pm
Location: Shiloh, IL

Re: Broke my APIs on Upgrade

Post by knight-of-ni »

OP is using nginx, which means he needs to manually update the webserver config file.
So far, I have yet to hear anyone report success with this. You may want to contact pedulla as he was working on an updated nginx config for zoneminder. Once I get confirmation of a working config, I can add it to the project.
Visit my blog for ZoneMinder related projects using the Raspberry Pi, Orange Pi, Odroid, and the ESP8266
All of these can be found at https://zoneminder.blogspot.com/
Nocifer
Posts: 37
Joined: Mon Oct 01, 2018 4:05 pm

Re: Broke my APIs on Upgrade

Post by Nocifer »

These errors show up because in the official configuration file for Apache '/api' is transcribed as '/api/app/webroot', so '../../../includes/config.php' points to 'www/includes/config.php'. In an improperly configured Nginx '/api' is not transcribed to '/api/app/webroot', so '../../../includes/config.php' points 2 directory levels higher than it should be.

You can try this config here: https://pastebin.com/pUSDWiFM

It's configured to run ZoneMinder from 'localhost:8095' rather than 'localhost/zm', and uses '/srv/zoneminder/www' as its webroot, so you may have to slightly tweak it to fit your needs.
User avatar
knight-of-ni
Posts: 2404
Joined: Thu Oct 18, 2007 1:55 pm
Location: Shiloh, IL

Re: Broke my APIs on Upgrade

Post by knight-of-ni »

hmmmm.... that config does not look like it contains an alias for the new cache folder.

This one might be a little closer to what is needed:
https://github.com/ZoneMinder/zoneminde ... -423321998

That user built from source though... you will need to update the paths to match your system.
Visit my blog for ZoneMinder related projects using the Raspberry Pi, Orange Pi, Odroid, and the ESP8266
All of these can be found at https://zoneminder.blogspot.com/
Nocifer
Posts: 37
Joined: Mon Oct 01, 2018 4:05 pm

Re: Broke my APIs on Upgrade

Post by Nocifer »

Yeah, the config assumes a link to the cache folder inside the webroot, so it doesn't need a separate location block for it. But if one is absolutely needed, then this should do the trick:

Code: Select all

location /cache {
    # Replace @ZM_CACHEDIR@ with the real path
    alias @ZM_CACHEDIR@

    include fastcgi_params;
           
    fastcgi_param SCRIPT_FILENAME $request_filename;
    fastcgi_param HTTP_PROXY "";
           
    fastcgi_pass unix:/run/fcgiwrap.sock;
}
By the way, the conf file you've linked has no provision for the static content of CakePHP and will result in the latter complaining about it if one tries to visit the /api location in a browser, because it will always try to load /api/app/webroot/index.php with everything else given to it as an argument. Hence e.g. a call like /api/css/cake.generic.css, instead of correctly loading /api/app/webroot/css/cake.generic.css, will result in a call like /api/app/webroot/index.php?p=/css/cake.generics.css which will obviously fail.

Just a nitpick really, as it in no way hinders the API itself, but there you have it :)
Last edited by Nocifer on Sat Oct 13, 2018 10:55 am, edited 1 time in total.
User avatar
knight-of-ni
Posts: 2404
Joined: Thu Oct 18, 2007 1:55 pm
Location: Shiloh, IL

Re: Broke my APIs on Upgrade

Post by knight-of-ni »

Understood.
That's exactly the kind of feedback we are looking for. Nginx is one of things I never really have sufficient time to dig into.

Now if we could get an Ubuntu user to try this and claim success, I'll add it to the project.
Visit my blog for ZoneMinder related projects using the Raspberry Pi, Orange Pi, Odroid, and the ESP8266
All of these can be found at https://zoneminder.blogspot.com/
Nocifer
Posts: 37
Joined: Mon Oct 01, 2018 4:05 pm

Re: Broke my APIs on Upgrade

Post by Nocifer »

Oh, thanks. I just posted it for OP to fix the problem they were having, but if you'd really like to include it at some point, just tell me beforehand so I can make it conform to ZM's defaults. Right now it makes some incompatible assumptions that result from my modified setup (e.g. the aforementioned /cache folder link inside webroot, which is not present in the official setup).
databoy2k
Posts: 28
Joined: Thu Nov 16, 2017 5:20 pm

Re: Broke my APIs on Upgrade

Post by databoy2k »

--Total Edit as I'm messing around--

So tried @nocifer's configuration. I use a zondeminder.conf which has been added to the default sites-enabled document via include. Of course, that meant having to nuke the server directive.

I had to remove the root directive in the proposed confirmation, otherwise I was getting a "duplicate root" error on restart of nginx. Also had to remove the "/" location in the default file because of that zoneminder.conf. Nginx now restarts without issue.

...and going to [ip]:8095 results in a "connection refused" error. I've opened the port in UFW so that's not the issue. Any help on troubleshooting?

For the record, here's my current zoneminder.conf:

Code: Select all

listen localhost:8095;
server_name localhost;

root /usr/share/zoneminder/www;
index index.php;

access_log /var/log/zm/http_access.log;
error_log /var/log/zm/http_error.log;

location / {
    try_files $uri $uri/ /index.php?$args =404;

    location ~ /api/(css|img) {
        rewrite ^/api(.+)$ /api/app/webroot/$1 break;
        try_files $uri $uri/ =404;
    }

    location /api {
        rewrite ^/api(.+)$ /api/app/webroot/index.php?p=$1 last;
    }

    location /cgi-bin {
        include fastcgi_params;

        fastcgi_param SCRIPT_FILENAME $request_filename;
        fastcgi_param HTTP_PROXY "";

        fastcgi_pass unix:/var/run/fcgiwrap.socket;
    }

    location ~ \.php$ {
        include fastcgi_params;

        fastcgi_param SCRIPT_FILENAME $request_filename;
        fastcgi_param HTTP_PROXY "";

        fastcgi_index index.php;

        fastcgi_pass unix:/var/run/php-fpm/php7.2-fpm.sock;
    }
}
...and here's the old one before I removed the root directive from default:

Code: Select all

location /zm/cgi-bin {
    gzip off;
    alias /usr/lib/zoneminder/cgi-bin;

    include /etc/nginx/fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $request_filename;
    fastcgi_pass  unix:/var/run/fcgiwrap.socket;
}

location /zm {
#   if ($scheme ~ ^http:){
#       rewrite ^(.*)$  https://$host$1 permanent;
#   }

    gzip off;
    alias                   /usr/share/zoneminder/www;
    index                   index.php;

    location ~ \.php$ {
        if (!-f $request_filename) { return 404; }
        expires             epoch;
        include             /etc/nginx/fastcgi_params;
        fastcgi_param       SCRIPT_FILENAME         $request_filename;
        fastcgi_index       index.php;
        fastcgi_pass        unix:/var/run/php/php7.2-fpm.sock;
    }


    location ~ \.(jpg|jpeg|gif|png|ico)$ {
        access_log          off;
        expires         33d;
    }

    location /zm/api/ {
        alias                   /usr/share/zoneminder/www/api;
        rewrite ^/zm/api(.+)$ /zm/api/index.php?p=$1 last;
    }
}
Nocifer
Posts: 37
Joined: Mon Oct 01, 2018 4:05 pm

Re: Broke my APIs on Upgrade

Post by Nocifer »

You're on Ubuntu 18.04, right? The problem is that this conf file is tailored to a different distro (Arch) and also that, just like with the /cache folder that was mentioned above, in my setup there is a link to /cgi-bin inside the webroot which is obviously missing from yours because the default ZM setup procedure will not create it.

So, please confirm the following for me and I'll fix it for your system:

1) The location of ZoneMinder's webroot. It should be /usr/share/zoneminder/www

2) The location of ZoneMinder's cgi-bin folder. It should /usr/share/zoneminder/cgi-bin

3) The location of ZoneMinder's cache folder. It should be /var/lib/zoneminder/cache

4) The location and name of your php-fpm socket. It should be something like /run (or /var/run)/php-fpm/php7.x-fpm.sock

5) The location of your fgciwrap socket. It should be something like /run (or /var/run)/fcgiwrap.sock

Also, if this "default sites-enabled document" you mentioned is the default.conf file that comes with the Nginx package and presents a page on http://localhost saying "Welcome to Nginx!" or something like that, then there won't be any conflict between it and the conf I provided, since the default one listens on port 80 while mine listens on port 8095.

So given that fact, and also given that adding ZoneMinder's configuration in default.conf should be considered as Very Bad Practice™, it would probably be better to put my conf inside sites-available as a separate file, and then link to it from inside sites-enable. Nothing will break as long as you remove from default.conf the extra stuff you already copied from my conf. Also, you should consider disabling default.conf anyway (i.e. delete the link inside sites-enabled) because it's just that, a silly default to let you know Nginx is working properly :)
databoy2k
Posts: 28
Joined: Thu Nov 16, 2017 5:20 pm

Re: Broke my APIs on Upgrade

Post by databoy2k »

If it hasn't already become clear, I'm about 4 experience points above complete newbie with this stuff - somewhere between "not tripping on the tutorials" and "complete liability on a raid". So I'm not surprised to learn that I'm engaging in Very Bad Practice and doing all sorts of dumb things. Thanks for the pointers on that front.

Yes, Ubuntu 18.04.

1) Correct
2) I think this one's /usr/lib/zoneminder/cgi-bin. That one contains nph-zms, and zms
3) I think that this is either /var/cache/zoneminder or /var/cache/zoneminder/cache
4) /var/run/php/php7.2-fpm.sock
5) /var/run/fcgiwrap.socket

Insofar as that VBP™ is concerned, I was silightly over-simplifying. I have a "default" file in ./sites-available linked into ./sites-enabled. In that default file I have an include directive pointing to a zoneminder config sitting out in the root nginx (i.e. /etc/nginx) folder. I don't know where I google-fu-ed that solution together from but it meant that I didn't have to learn how to properly link from sites-available to sites-enabled. Isn't that the source of every bad practice?

I'll burn the include directive once we figure out the new config file and follow better practices from here on in.

Thanks again for your help.
Nocifer
Posts: 37
Joined: Mon Oct 01, 2018 4:05 pm

Re: Broke my APIs on Upgrade

Post by Nocifer »

Alright, try this one out:

https://pastebin.com/pUSDWiFM

Don't forget to rename the file from zoneminder-nginx.conf to zoneminder.conf.

If the info you've given me is accurate, then in theory it should work without a hitch and ZoneMinder will be listening on http://localhost/zm . In practice, I'm a bit too tired at the moment so any kind of weird error could have sneaked in there without me noticing. Try it and we'll see.

Regarding how to set it all up:

Right now you have an /etc/nginx/sites-available/default.conf file, correct? And this file's contents used to be something like this:

Code: Select all

server {
    // lots of stuff
}
And now, you have put ZM's conf file in /etc/nginx/zoneminder.conf, and you've edited default.conf to look something like this:

Code: Select all

server {
    // lots of stuff
    
    include zoneminder.conf;
}
Am I correct? I think I am, because "including" the contents of your current zoneminder.conf outside of that server block (that is, outside that set of curly braces) would make Nginx fail to run.

So, the best thing you can do is ONE of the following:

a) Change default.conf to this:

Code: Select all

server {
    // lots of stuff
}
include zoneminder.conf;
b) Put zoneminder.conf (the new one that I just gave you in the link above) inside sites-available, just like default.conf, and link it from inside sites-enabled, just like default.conf. And then you'll be running Nginx exactly "Just As (Ubuntu) Planned™"

~~~~~~~~~~~

I hope the instructions were clear enough. If you have any questions feel free to ask. If you have problems, first run "nginx -t" in the terminal, and then come tell me here what problems you're having along with anything that command told you.
databoy2k
Posts: 28
Joined: Thu Nov 16, 2017 5:20 pm

Re: Broke my APIs on Upgrade

Post by databoy2k »

It never ceases to amaze me when someone can effectively create this stuff in their sleep when I'm still at the "ah eff it, sudo everything and see if it works" stage...

So I had the "include /etc/nginx/zoneminder.conf" directive inside the server's brackets, so I commented that out and saved. I created the new zoneminder.conf in /sites-available and then linked it over to sites-enabled. Nginx -t failed, complaining about being unable to find http_access.log, so I commented out both logging directions (each of error and access; I didn't have that in my original zoneminder.conf so I assumed they were optional) and nginx -t had no problems. Restarted nginx, but now going to [ip]/zm/index.php returns a 404 not found error.

...are the logs not optional, or have I obviously done something wrong again?
Post Reply