Debugging CGI
- Posted by jmduro Oct 05, 2018
- 2166 views
Here is my method for debugging OpenEuphoria CGI scripts with Apache2. To be used for development not for production because of weak safety rules.
Configure Apache2 to compile OpenEuphoria CGI scripts
cd /etc/apache2/
sudo nano conf-available/serve-cgi-bin.conf
add:
AddHandler exu-file .exu .ex
Action exu-file /cgi-bin/runexu.cgi
under:
ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
cd mods-enabled/
sudo ln -s ../mods-available/cgi.load
sudo ln -s ../mods-available/actions.load
sudo ln -s ../mods-available/actions.conf
sudo a2enmod ssl
sudo a2ensite default-ssl
sudo service apache2 reload
sudo chmod 777 /usr/lib/cgi-bin/
sudo vi /usr/lib/cgi-bin/runexu.cgi # adapt to your OEU path !!!
#!/bin/sh
#runexu.cgi - wrapper to run a euphoria program without the need for #!
debug="log/debug_$(date +"%Y-%m-%d_%H-%M-%S")_runexu.log"
echo "-----------------------------------" >> $debug
date --rfc-3339='ns' >> $debug
echo "-----------------------------------" >> $debug
/usr/local/euphoria-4.1.0-ARMv6-hardfloat/bin/eui $PATH_TRANSLATED | tee -a $debug
# END of runexu.cgi
sudo chmod +x /usr/lib/cgi-bin/runexu.cgi
sudo /etc/init.d/apache2 restart
sudo chown -R $(id -un):www-data /var/www/html
sudo chmod 777 /var/www/html
Create a sub-directory for debug logs under /var/www/html
sudo mkdir /var/www/html/log sudo chmod 777 /var/www/html/log
Link /var/www/html/log and /usr/lib/cgi-bin/log
sudo ln -s /var/www/html/log /usr/lib/cgi-bin/log
Create following shell scripts to clean logs before run and to see logs after run:
error.log is built by EU4 Standard library (you may forget it if you don't use it)
vi /var/www/html/clean #!/bin/bash rm -f log/* rm -f /usr/lib/cgi-bin/ex.err rm -f /usr/lib/cgi-bin/error.log
vi /var/www/html/debug #!/bin/bash echo "--------------------------------------------------------------------------------" echo "Debug starts at $(date +"%Y-%m-%d %H:%M:%S")" echo "--------------------------------------------------------------------------------" echo -e "\n/usr/lib/cgi-bin/ex.err" [ -f /usr/lib/cgi-bin/ex.err ] && head -20 /usr/lib/cgi-bin/ex.err || echo "Not found" echo -e "\n/usr/lib/cgi-bin/error.log" [ -f /usr/lib/cgi-bin/error.log ] && cat /usr/lib/cgi-bin/error.log || echo "Not found" for i in $(ls -1tr log/*) do echo -e "\n$i" cat $i done
My CGI scripts include following lines:
sequence dt = date() sequence path = test_val("PATH_TRANSLATED") sequence webDir = dirname(path) sequence script = filename(path) f_debug = open(webDir & SLASH & "log" & SLASH & sprintf("debug_%d-%02d-%02d_%02d-%02d-%02d_%s.log", {dt[1]+1900, dt[2], dt[3], dt[4], dt[5], dt[6], script}), "w")
Here is an exemple:
:/var/www/html$ ./clean
# Access your CGI script with your browser
:/var/www/html$ ./debug
--------------------------------------------------------------------------------
Debug starts at 2018-10-05 15:48:05
--------------------------------------------------------------------------------
/usr/lib/cgi-bin/ex.err
/var/www/html/test.ex:39
<0074>:: Errors resolving the following references:
'f_debug' (/var/www/html/test.ex:31) has not been declared.
'f_debug' (/var/www/html/test.ex:39) has not been declared.
'InitialDir' (/var/www/html/test.ex:39) has not been declared.
'SLASH' (/var/www/html/test.ex:39) has not been declared.
'with_debug' (/var/www/html/test.ex:42) has not been declared.
'with_debug' (/var/www/html/test.ex:39) has not been declared.
f_debug = open(InitialDir & SLASH & "log" & SLASH &
^
--- Defined Words ---
EU4
EU4_1
EU4_1_0
CONSOLE
UNIX
LINUX
/usr/lib/cgi-bin/error.log
Not found
log/debug_2018-10-05_15-47-53_runexu.log
-----------------------------------
2018-10-05 15:47:53.246376036+00:00
-----------------------------------
:/var/www/html$
Jean-Marc

