My web server is prompting me to download my cgi scripts

My apache web server is configured to execute cgi and python scripts (see attached default and site configuration from my apache2 sites directory). Python is installed on my server and in the path that matches what my cgi script is looking fork. The cgi script executes from the command line (by printing an html form in the same directory) and it our office web server correctly serves the page. But at home I am prompted to download a text file when I try to open http://myurl.com/executables_dir/myscript.cgi.

I suspect that my apache2 environment variables are broken, but I don't know how to fix that. I installed mod_python with the expectation that apache2 would read in the correct environment variables to recognize python scripts but that failed.

So far the only thing that does work is this:
in both the web root and executables directories, when I run a test.py script, it returns 'test successful'.

I attach the env var script and its output below. Thanks for any help!

apache2 default site config:

	<Directory /var/www/>
		Options Indexes Includes FollowSymLinks MultiViews
		AllowOverride FileInfo
                AllowOverride AuthConfig
                AllowOverride None 
		Order allow,deny
		allow from all
                AddHandler mod_python .py
                PythonHandler mod_python.publisher
                PythonDebug On
	</Directory>

my_site config:

<Directory /var/www/mydomain.com> # my root dir = my domain name
                Options Indexes FollowSymLinks MultiViews
                AllowOverride None
                Order allow,deny
                allow from all
                AddHandler mod_python .py
                PythonHandler mod_python.publisher
                PythonDebug On
        </Directory>

        <Directory /var/www/mydomain/executables/>
                Options +ExecCGI
		AddHandler cgi-script .cgi 

#Tried AddHandler cgi-script .cgi .py, which failed so I added this:

                AddHandler mod_python .py
                PythonHandler mod_python.publisher
                PythonDebug On
        </Directory>

Open in new window

#!/usr/bin/python 
import os 

for param in os.environ.keys():
    print "%20s %s" % (param,os.environ[param])

Output:

            USERNAME root
                LANG en_US.UTF-8
                TERM xterm
               SHELL /bin/bash
           LESSCLOSE /usr/bin/lesspipe %s %s
        SUDO_COMMAND /bin/bash
               SHLVL 1
              OLDPWD /var/www/rootdir
            SUDO_UID 1001
            SUDO_GID 1001
            LESSOPEN | /usr/bin/lesspipe %s
                 PWD /var/www/rootdir/executables_dir
             LOGNAME root
                USER root
                MAIL /var/mail/myuser
                PATH /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/lib
           SUDO_USER myuser
           LS_COLORS rs=0:di=01;34:ln=01;36:hl=4401;32:*.tar=01; # rest omitted
                   _ ./envir.py

Open in new window

sara_bellumAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

farzanjCommented:
Do you want to fix the PATH variable?  Do you know what you want to get included into your path, may be I can help you with that.

There are normally number of ways, depending upon your Apache version and your OS.
One way is to set by using mod_env.  This should be pretty standard.  You need to include mod_env and then you can use SetEnv, UnsetEnv or PassEnv.

Please refer to this:
http://httpd.apache.org/docs/2.0/mod/mod_env.html

Other ways,  with some version you can modify some environment from the init script like /etc/init.d/httpd
or /etc/sysconfig/httpd.  It may be different in Ubuntu
0
sara_bellumAuthor Commented:
Thanks very much!

I checked the apache docs for mod_env and added this line to my web root directory:
SetEnv PYTHONPATH /usr/lib/python2.6
and restarted apache2. Since I'm running two domains on this server, I made sure to add this line in my domain1 site as well as in the default site. There was no apache error on restart.

But the result is the same: when I load
http://www.domain1.com/executables_dir/myscript.cgi,
apache prompts me to download the file

I tried the same url with a py extension and got this error:
mod_python (pid=13488, interpreter='www.domain1.com'): Importing module '/var/www/domain1.com/myscript.py'

mod_python was trying to import the script to my root dir which is not what I want, so I removed the mod_python instructions from my executables dir (see below).

I had installed mod_python with the hope of fixing my cgi problem, and I think it helped, since opening http://www.domain1./com/test.py prints 'test successful' (FYI the script consists of only these lines:
#!/usr/bin/python
def index(req):
  return "Test successful";)

But surely if I had a python path problem now, I would not be seeing this result.

So I have a cgi problem. But apache docs only say to make sure that your site config contains a directory where you have authorized the execution of cgi scripts:

 <Directory /var/www/domain1/executables/> # this dir contains the script
                Options +ExecCGI
            AddHandler cgi-script .cgi .py
 </Directory>

which I have done. With the above config I am prompted to download the script whether I try to access it as a .cgi or as a .py file. Any thoughts?


0
farzanjCommented:
did you actually see the PYTHONPATH variable getting set?

Please make sure it did.  Also, please also include it in the PATH variable too.  I don't see /usr/lib/python2.6 in your PATH variable.  Add it there too.

Apart from SetEnv, you may have to do PassEnv.

I just want to make sure that you exhaust this as a path issue completely.  Make sure that both PATH and PYTHONPATH are set and and passed after forking and if you still have problems, you would go from there.
0
The 7 Worst Nightmares of a Sysadmin

Fear not! To defend your business’ IT systems we’re going to shine a light on the seven most sinister terrors that haunt sysadmins. That way you can be sure there’s nothing in your stack waiting to go bump in the night.

sara_bellumAuthor Commented:
I don't know of any way to verify whether the PATH variable is being set in apache  other than to run the program that I mentioned in the /var/www/domain1.com/executables/ directory:
#!/usr/bin/python
for param in os.environ.keys():
    print "%20s %s" % (param,os.environ[param])

I tried the same for Perl:
#!/usr/bin/perl
foreach $key (keys %ENV) {
print "$key --> $ENV{$key}\n";
}
The path variables are the same:
PATH --> /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/lib
PATH     /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/lib

I had set the python path in my web root directory but then copied it also to my executables directory:
<Directory /var/www/cerebellum.biz/eei/>
                Options +ExecCGI
                AddHandler cgi-script .cgi .py .pl
                SetEnv PYTHONPATH /usr/lib/python2.6
        </Directory>
however the problem persists: I'm prompted to download any test script that appears, whether it's written in python or perl.

Clearly putting SetEnv + path to python doesn't work, but I don't know what else to do (I should also fix the problem for perl and cgi extensions).

TIA


0
farzanjCommented:
Yes, you had to check your environment using your program.
From what I, the environment did not get set.

Did you include or uncomment mod_env lines in the httpd.conf (Apache config file)?

If this method does not work, we would try another way.

Do you have /etc/init.d/httpd file? This should be the file that you use to start and restart your Apache server.
In that file, you should have the PATH variable.  Set your variable values in this file.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
sara_bellumAuthor Commented:
I removed the mod_env lines because they were causing unexpected errors. I don't believe that mod_env is enabled on our office server either.

I then enabled a script alias in my apache site configuration, and tried to execute my script in a cgi-bin folder under my web doc root. It worked.

I then tried executing the script in my executables folder. It also worked.

I then disabled the cgi-bin script alias in my site config and restarted apache2 to see whether the web server could still execute the scripts in my executables folder. It can, problem solved.

0
sara_bellumAuthor Commented:
The environment variables were not getting set in part because apache wasn't recognizing scripts as executable files.  Setting a script alias, not the addition of a PYTHONPATH, is what changed this behavior (after which I could disable the alias and everything still worked).

But I appreciate the exchange - this stuff isn't always an exact science.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Scripting Languages

From novice to tech pro — start learning today.