Johan Klinge's picture

Hi,

I'm a newbie to Turnkey and have just set up LAMP as a virtual development environment for a website I support. Everything works great, except that the website uses cgi programs and I want to configure LAMP to run them. The cgi scripts are in directory /var/www/SITENAME/privat.  

Reading some other posts on the forum I changed the configuration for the default apache site to:

<VirtualHost *:80>
    ServerAdmin webmaster@localhost
 DocumentRoot /var/www/
 <Directory "/var/www/SITENAME/privat/">
  Options +ExecCGI
  AddHandler cgi-script .cgi
 </Directory>
LogLevel error
</VirtualHost>

But when running cgi script from the browser I get a 500 Internal Server Error and the apache logs says:

[error] [client IP] (2)No such file or directory: exec of '/var/www/SITENAME/privat/index.cgi' failed, referer: http://IP/SITENAME/
[Mon Feb 08 10:20:18 2010]
[error] [client IP] Premature end of script headers: index.cgi, referer: http://IP/SITENAME/

Any suggestions as to what I'm doing wrong would be appreciated.

Regards, /Johan

Forum: 
Andrew Stewart's picture

Having this issue as well.  Why doesn't CGI work out of the box on LAMP?

Alon Swartz's picture

I've just tested cgi on LAMP and it works, but requires a minor tweak. I've updated the upcoming 11.0 release to include this tweak, as well as a test cgi script for reference.
 

Test CGI script

/var/www/cgi-bin/test.cgi
#!/usr/bin/perl
print "Content-type: text/html\n\n" ;
print <<EOF ;
<html>
<head><title>CGI Test</title></head>
<body>
<h1>Hello, world.</h1>
</body>
</html>
EOF
Don't forget to make the cgi script executable:
chmod +x /var/www/cgi-bin/test.cgi
 

Tell apache about cgi-bin

/etc/apache2/sites-available/default
NameVirtualHost *:80
NameVirtualHost *:443

<VirtualHost *:80>
        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/
</VirtualHost>

<VirtualHost *:443>
        SSLEngine on
        SSLCertificateFile /etc/ssl/certs/cert.pem
        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/
</VirtualHost>

ScriptAlias /cgi-bin/ /var/www/cgi-bin/

<Directory /var/www/>
        Options Indexes FollowSymLinks MultiViews
        Order allow,deny
        allow from all
</Directory>
Don't forget to restart apache after making the change:
/etc/init.d/apache2 restart
OR
service apache2 restart
 
As a side note, if you have a cgi script outside of the ScriptAlias that you want to be executable, you need to specify:
AddHandler cgi-script cgi
Options +ExecCGI
 
I hope the helps.
olc's picture

This was the missing part of the puzzle and it did solve our problems. THANKS A LOT!

Jeremy Davis's picture

This thread is quite old. See Alon's comment above:

I've updated the upcoming 11.0 release to include this tweak, as well as a test cgi script for reference.

Jeremy Davis's picture

This thread is really old and things have changed a lot since this was relevant so it's not a huge surprise that it's not useful.

However, it should "just work"! But you are right. I just tested it and confirmed that it doesn't!

I have lodged an issue on our tracker.

[update] Here's the specifics to get the example cgi script working.

Add new comment