This is a card in Dave's Virtual Box of Cards.

Testing CGI locally

Created: 2022-09-28

I love old-fashioned CGI scripts. And I love running them locally for a fast edit-save-refresh test cycle.

But sometimes you’ve gotta do things a little differently from a local test directory. Since I use the simplest rsync deployment to my website that you can imagine, I’ve struggled to figure out how to reliably inform my scripts that they’re running locally.

But since this is CGI, the answer is: environment variables!

I use Apache because it’s already installed on Slackware and it’s what I know.

See the SetEnv LOCAL_TEST true line in the CGI setup here:

#
# /etc/httpd/httpd.conf
#

LoadModule mpm_prefork_module lib64/httpd/modules/mod_mpm_prefork.so
LoadModule env_module lib64/httpd/modules/mod_env.so
LoadModule cgi_module lib64/httpd/modules/mod_cgi.so

...

# SERVE RATFACTOR LOCALLY
<Directory "/home/dave/wiki/ratf/src/">
    Options Indexes FollowSymLinks
    AllowOverride None
    Require all granted
</Directory>
AddType text/html .adoc
Alias /ratf /home/dave/wiki/ratf/src/

# api/cgi directory
<Directory "/home/dave/wiki/ratf/src/api">
    Options +ExecCGI
    SetHandler cgi-script
    Require all granted
    SetEnv LOCAL_TEST true
</Directory>

Then reading them from a Ruby CGI script is no problemo! (Short answer: if ENV['LOCAL_TEST'] …​)

This is running on my 5 Watt always-on home computer (read about that here, also, I’m writing this page via SSH on that computer right now). :-)