Skip to content

MODS Sample Basic CGI

Valéry Letroye edited this page Feb 18, 2021 · 2 revisions

Description

This is a Basic Package for Synology created with 'Mods Packager'. It illustrates how to run a php page and display it in a DMS' iFrame without any dependency on the Package Init_3rdparty. (One difference is that Init_3rdparty runs the php scripts as root while here they are run as http).

It's based on a post of Rob Van Aarle: the idea is to use php-cgi to execute the php page instead of php-fpm.

It contains one url:

  • Basic CGI Hello World: this is a call to a helloworld.cgi script which executes a helloworld.php page. That's the basics of Rob's idea. It works fine on DSM 6.1, i.e. with nginx !

Notice

Rob has a most advanced idea: to redirect calls to any php page to a single central cgi. Doing so, there is no need to write one cgi per php page and use those cgi in the hyperlinks. I did illustrate that idea in the package MODS Advanced Test CGI.

Making of

This package contains one "WebApp" item pointing at the script helloworld.cgi here under

#!/bin/sh
LOG=/var/log/MODS_BasicTestCGI
echo `date` "helloworld.cgi has been called" >> $LOG

# Set redirect_status to 1 to get php cgi working.
REDIRECT_STATUS=1 export REDIRECT_STATUS

# Define the name of the php page to be executed
SCRIPT_FILENAME=$(pwd)/helloworld.php export SCRIPT_FILENAME
echo `date` "it will execute" $SCRIPT_FILENAME >> $LOG

/usr/local/bin/php56-cgi -d open_basedir=none $SCRIPT_FILENAME 2>&1

As you can see, this cgi script is logging each call to itself in a log file and execute next the page helloworld.php located next to it.

NB.: here above, I was using php56. You can obviously update that script to use php70, php72 or php73 according to your need and the actual version of php installed on your NAS.

In order to give access to the log file, the package includes a custom start-stop-script. So, you can click on "View Log" when opening the package in DSM's Package Center.

#!/bin/sh
LOG=/var/log/MODS_BasicTestCGI
case $1 in
  start)
    exit 0
    ;;
  stop)
    exit 0
    ;;
  status)
    exit 0
    ;;
  log)
    echo $LOG
    exit 0
    ;;
esac
exit 0

And finally, in the post installation script, one grant write access on the log file for "others" so php-cgi can also log into it when it executes the php page.

#!/bin/sh
LOG=/var/log/MODS_BasicTestCGI
echo `date` "Granting access on log file for the webserver" >> $LOG
chmod o+w $LOG
exit 0

Illustration

php page executed by php-cgi