WebFaction
Community site: login faq

I need to run a cgi script on Webfaction. For the cgi script to work, an "environment variable" is required. I tried to set the environment variable by creating a .htaccess file with:

SetEnv TINYOWS_CONFIG_FILE /home/keskari/webapps/kala_app/tinyows.xml

But it seems that my cgi script doesn't "see" the TINYOWS_CONFIG_FILE environment variable. To narrow down the problem, I created a simple test cgi script:

#!/bin/bash

echo "Content-type: text/html"
echo ""

echo '<html>'
echo '<head>'
echo '<title>List of environment variables</title>'
echo '</head>'
echo '<body>'
echo '<pre>'
env
echo '</pre>'
echo '</body>'
echo '</html>'

exit 0

But when I run the script, it doesn't list my custom environment variable. It only lists:

SERVER_SIGNATURE=
HTTP_X_FORWARDED_SERVER=kala.hydrodata.org
HTTP_USER_AGENT=Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/31.0.1650.63 Chrome/31.0.1650.63 Safari/537.36
HTTP_X_FORWARDED_FOR=130.233.217.167
SERVER_PORT=80
HTTP_HOST=kala.hydrodata.org
HTTP_HTTPS=off
PHP_INI_SCAN_DIR=/home/keskari/webapps/kala_app
DOCUMENT_ROOT=/home/keskari/webapps/kala_app
SCRIPT_FILENAME=/home/keskari/webapps/kala_app/hello.cgi
HTTPS=off
REQUEST_URI=/hello.cgi
SCRIPT_NAME=/hello.cgi
HTTP_X_FORWARDED_HOST=kala.hydrodata.org
SCRIPT_URI=http://kala.hydrodata.org/hello.cgi
HTTP_CONNECTION=close
REMOTE_PORT=39387
PATH=/usr/local/bin:/usr/bin:/bin
SCRIPT_URL=/hello.cgi
PWD=/home/keskari/webapps/kala_app
SERVER_ADMIN=[no address given]
HTTP_ACCEPT_LANGUAGE=en-US,en;q=0.8,cs;q=0.6,fi;q=0.4
HTTP_FORWARDED_REQUEST_URI=/hello.cgi
HTTP_ACCEPT=text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
REMOTE_ADDR=130.233.217.167
SHLVL=1
SERVER_NAME=kala.hydrodata.org
SERVER_SOFTWARE=Apache
HTTP_X_FORWARDED_SSL=off
QUERY_STRING=
HTTP_HTTP_X_FORWARDED_PROTO=http
SERVER_ADDR=127.0.0.1
GATEWAY_INTERFACE=CGI/1.1
SERVER_PROTOCOL=HTTP/1.0
HTTP_ACCEPT_ENCODING=gzip,deflate,sdch
HTTP_CACHE_CONTROL=max-age=0
REQUEST_METHOD=GET
HTTP_X_FORWARDED_PROTO=http
_=/usr/bin/env

Any ideas of what I'm doing wrong?

asked 25 Jan '14, 23:06

jirik
121812
accept rate: 66%


Our suexec environment for CGI doesn't allow custom environment variables to be set with SetEnv - instead, then need to be set in the script itself.

One way to work around this is to create a wrapper script that sets the variables and then calls the actual script. You then rewrite all requests to the wrapper via .htaccess.

For example, the wrapper script (we'll call it wrapper.cgi):

#!/bin/sh

export TINYOWS_CONFIG_FILE=/home/keskari/webapps/kala_app/tinyows.xml
exec /home/keskari/webapps/kala_app$SCRIPT_URL

And the rewrite:

RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !wrapper\.cgi
RewriteRule ^.*\.cgi /wrapper.cgi

Hope that helps!

permanent link

answered 26 Jan '14, 02:03

seanf
12.2k42136
accept rate: 37%

edited 30 Jan '14, 16:31

Yes, it worked!

(27 Jan '14, 08:01) jirik
Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Question tags:

×31
×11

question asked: 25 Jan '14, 23:06

question was seen: 8,597 times

last updated: 30 Jan '14, 16:31

WEBFACTION
REACH US
SUPPORT
AFFILIATE PROGRAM
LEGAL
© COPYRIGHT 2003-2020 SWARMA LIMITED - WEBFACTION IS A SERVICE OF SWARMA LIMITED
REGISTERED IN ENGLAND AND WALES 5729350 - VAT REGISTRATION NUMBER 877397162
5TH FLOOR, THE OLD VINYL FACTORY, HAYES, UB3 1HA, UNITED KINGDOM