[SOLVED]Fix API (CakePHP/Crud) on Nginx + PHP7 (Alpine Linux)

Forum for questions and support relating to the 1.32.x releases only.
Post Reply
User avatar
Bluemax
Posts: 121
Joined: Wed Jun 12, 2019 5:15 pm

[SOLVED]Fix API (CakePHP/Crud) on Nginx + PHP7 (Alpine Linux)

Post by Bluemax »

Hallo,
i'm running Zoneminder v1.32.3 on Alpine Linux with Nginx and PHP7. Everything's working fine so far but API access. The Crud plugin wasn't installed on Alpine (just a dead symlink pointing to a crud-c3976f1478c681b0bbc132ec3a3e82c3984eeed5 folder. That's a very old crud version anyway). So i fixed it by doing this with latest versions (Crud 3.2.0 into api/app/Plugin/Crud and cakephp-2.10.18 into api/lib).

Now, when i open /zm/api i get this error:

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): Use of undefined constant ZM_OPT_USE_AUTH - assumed 'ZM_OPT_USE_AUTH' (this will throw an Error in a future version of PHP) [APP/Controller/AppController.php, line 72]

Warning (2): require_once(../../../includes/auth.php) [<a href='http://php.net/function.require-once'>function.require-once</a>]: failed to open stream: No such file or directory [APP/Controller/AppController.php, line 73]


Fatal error: require_once() [function.require]: Failed opening required '../../../includes/auth.php' (include_path='.:/usr/share/php7') in /usr/share/webapps/zoneminder/htdocs/api/app/Controller/AppController.php on line 73
For debugging i've injected an echo getcwd() . "\n"; and the current directory is "/" so the relative paths cannot work indeed.

My guess is that CakePHP is not running properly due to nginx not configured properly. I've mainly taken the config from here and here. It currently looks like this:

Code: Select all

server {
	listen 80 default_server;
	listen [::]:80 default_server;
	root	/usr/share/webapps/zoneminder/htdocs;
	
	location /cgi-bin {
		gzip off;
		alias /usr/share/webapps/zoneminder/cgi-bin;

		include fastcgi_params;
		fastcgi_param SCRIPT_FILENAME $request_filename;
		fastcgi_pass unix:/run/fcgiwrap/fcgiwrap.sock;
	}
	
	location /zm {
		gzip off;
		alias /usr/share/webapps/zoneminder/htdocs;
		index index.php;
		
		location ~ \.php$ {
			if (!-f $request_filename) { return 404; }
			expires				epoch;
			include				fastcgi_params;
			# NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
			fastcgi_param		SCRIPT_FILENAME $request_filename;
			fastcgi_pass		unix:/var/run/php-fpm7/php-fpm.sock;
		}
		
		location ~* /zm/.*\.(txt|log)$ {
			deny all;
		}
		
		location ~* /zm/.*\.(m4a|mp4|mov)$ {
			mp4;
			mp4_buffer_size 5M;
			mp4_max_buffer_size 10M;
		}
		
		location /zm/api/ {
			alias		/usr/share/webapps/zoneminder/htdocs/api;
			rewrite		^/zm/api(.+)$ /zm/api/index.php?p=$1 last;
		}
	}
}
The /cgi-bin (instead of /zm/cgi-bin) is correct here. Folders are a little bit different on Alpine. I've added the 'root' entry on top. Not sure if needed or correct.
When i look at the current Apache config i see several rewrites and aliases at the bottom for api/app/webroot and api/app that are related to /zm/api. I'm wondering if i have to handle this on nginx as well. I guess the initial script that sets the ROOT path (or something) is not invoked at all. But i obviously need a hand here.

Any idea how to fix this?
Last edited by Bluemax on Thu Jun 13, 2019 1:36 am, edited 1 time in total.
User avatar
Bluemax
Posts: 121
Joined: Wed Jun 12, 2019 5:15 pm

Re: Fix API (CakePHP/Crud) on Nginx + PHP7 (Alpine Linux)

Post by Bluemax »

That's the fix. It seems to pass/catch the arguments automatically (index.php?$1).

Code: Select all

		location /zm/api/ {
			rewrite		^/zm/api(.+)$ /zm/api/app/webroot/index.php last;
		}
/zm/api still complains about 'URL rewriting is not properly configured' but API works and others have that warning as well. I don't know how to get rid of it. Another complain was 'Your tmp directory is NOT writable' so i've added /zm/api/app/tmp folder and warning was gone.
Post Reply