LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-29-2012, 11:29 AM   #1
Annielover
Member
 
Registered: May 2011
Location: Belgium
Distribution: Ubuntu Server Edition, Fedora 16
Posts: 90

Rep: Reputation: 8
Bash/CGI script won't execute - error 500


Hi all,

I want to display the output of a bash script on a html page using CGI,
but I can't get it working... I get an "500 internal server error".
I changed the default document root to "/home/www".
According to the error.log file Apache can't find the .sh file or am I wrong?
But why does it say "/www/cgi-bin/sysinfo.sh" instead of "/home/www/cgi-bin/sysinfo.sh"?

And there is something wrong with the permissions too, despite the 755 permission on the script and the cgi-bin folder...

Any help?

Apache2 error.log:
Code:
/home/www/cgi-bin/sysinfo.sh: line 2: /home/www/cgi-bin/sysinfo.sh: Permission denied
chmod: cannot access `/www/cgi-bin/sysinfo.sh': No such file or directory
[Fri Jun 29 18:11:13 2012] [error] [client 192.168.1.32] Premature end of script headers: sysinfo.sh
Apache2 default site:
Code:
<VirtualHost *:80>
        ServerAdmin laurentdw007@hotmail.com

        DocumentRoot /home/www/
        <Directory />
                Options FollowSymLinks
                AllowOverride None
        </Directory>
        <Directory /home/www/>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride None
                Order allow,deny
                allow from all
        </Directory>

        ScriptAlias /cgi-bin/ /home/www/cgi-bin/
        <Directory "/home/www/cgi-bin">
                AllowOverride None
                Options ExecCGI MultiViews SymLinksIfOwnerMatch
                AddHandler cgi-script cgi pl
                Order allow,deny
                Allow from all
        </Directory>

        ErrorLog /var/log/apache2/error.log

        # Possible values include: debug, info, notice, warn, error, crit,
        # alert, emerg.
        LogLevel warn

        CustomLog /var/log/apache2/access.log combined

    Alias /doc/ "/usr/share/doc/"
    <Directory "/usr/share/doc/">
        Options Indexes MultiViews FollowSymLinks
        AllowOverride None
        Order deny,allow
        Deny from all
        Allow from 127.0.0.0/255.0.0.0 ::1/128
    </Directory>

</VirtualHost>
Directories:
/home
Code:
drwxr-xr-x  3 root       root       4096 2012-06-29 18:07 www
/home/www
Code:
drwxr-xr-x 2 root root     4096 2012-06-29 12:57 cgi-bin/
-rw-r--r-- 1 root root     2532 2012-06-29 18:07 index.htm
/home/www/cgi-bin
Code:
-rwxr-xr-x 1 root root  665 2012-06-29 12:57 sysinfo.sh
sysinfo.sh
Code:
cat > /home/www/cgi-bin/sysinfo.sh << EOF
#!/bin/bash
echo "Content-type: text/html\n\n"
echo ""
echo "<html class="background"><head><link rel="stylesheet" href="../index.css" type="text/css" /><title>system information for $(hostname -s)"
echo "</title></head><body>"
echo "<h1 class="center">General system information for host $(hostname -s)</h1>"
echo ""
echo "<h2>Memory Info</h2>"
echo "<pre> $(free -m) </pre>"
echo "<h2>Disk Info:</h2>"
echo "<pre> $(df -h) </pre>"
echo "<h2>Logged in users</h2>"
echo "<pre> $(w) </pre>"
echo "<center>Information generated on $(date)</center>"
echo "</body></html>"
EOF
Thanks!!
 
Old 06-29-2012, 12:27 PM   #2
lithos
Senior Member
 
Registered: Jan 2010
Location: SI : 45.9531, 15.4894
Distribution: CentOS, OpenNA/Trustix, testing desktop openSuse 12.1 /Cinnamon/KDE4.8
Posts: 1,144

Rep: Reputation: 217Reputation: 217Reputation: 217
Quote:
Originally Posted by Annielover View Post
Hi all,

I want to display the output of a bash script on a html page using CGI,
but I can't get it working... I get an "500 internal server error".
I changed the default document root to "/home/www".
According to the error.log file Apache can't find the .sh file or am I wrong?
But why does it say "/www/cgi-bin/sysinfo.sh" instead of "/home/www/cgi-bin/sysinfo.sh"?

And there is something wrong with the permissions too, despite the 755 permission on the script and the cgi-bin folder...

Apache2 default site:
Code:
<Directory "/home/www/cgi-bin">
                AllowOverride None
                Options ExecCGI MultiViews SymLinksIfOwnerMatch
                AddHandler cgi-script cgi pl    <-- the files you treat as CGI-BIN are: .cgi , .pl (not .sh which you have)
                Order allow,deny
                Allow from all
        </Directory>
sysinfo.sh
Code:
cat > /home/www/cgi-bin/sysinfo.sh << EOF
#!/bin/bash
echo "Content-type: text/html\n\n"
echo ""
echo "<html class="background"><head><link rel="stylesheet" href="../index.css" type="text/css" /><title>system information for $(hostname -s)"
echo "</title></head><body>"
echo "<h1 class="center">General system information for host $(hostname -s)</h1>"
echo ""
echo "<h2>Memory Info</h2>"
echo "<pre> $(free -m) </pre>"
echo "<h2>Disk Info:</h2>"
echo "<pre> $(df -h) </pre>"
echo "<h2>Logged in users</h2>"
echo "<pre> $(w) </pre>"
echo "<center>Information generated on $(date)</center>"
echo "</body></html>"
EOF
Thanks!!
Try CGI-BIN with Perl
- Hello world example in perl
- perl test example.

Then it should work.

- set up cgi-bin in apache

Or try google for more

good luck

Last edited by lithos; 06-29-2012 at 12:32 PM.
 
Old 06-29-2012, 02:25 PM   #3
sag47
Senior Member
 
Registered: Sep 2009
Location: Raleigh, NC
Distribution: Ubuntu, PopOS, Raspbian
Posts: 1,899
Blog Entries: 36

Rep: Reputation: 477Reputation: 477Reputation: 477Reputation: 477Reputation: 477
What distro are you using? If you're using something like Fedora or CentOS then they come with SELinux enabled by default and you need to set the security context in order for httpd to execute cgi...

SAM
 
Old 07-02-2012, 09:03 AM   #4
Annielover
Member
 
Registered: May 2011
Location: Belgium
Distribution: Ubuntu Server Edition, Fedora 16
Posts: 90

Original Poster
Rep: Reputation: 8
I'm using Ubuntu Server 10.04 LTS
 
Old 07-02-2012, 09:15 AM   #5
Annielover
Member
 
Registered: May 2011
Location: Belgium
Distribution: Ubuntu Server Edition, Fedora 16
Posts: 90

Original Poster
Rep: Reputation: 8
Hi, I shortened the test script a little and this is what I get:
(but it still doesn't work...)

Code:
[Mon Jul 02 16:01:31 2012] [error] (8)Exec format error: exec of '/home/www/cgi-bin/sysinfo.sh' failed
[Mon Jul 02 16:01:31 2012] [error] [client 192.168.1.32] Premature end of script headers: sysinfo.sh
Bash script
Code:
cat > /home/www/cgi-bin/sysinfo.sh << EOF
#!/bin/bash
echo "Content-type: text/html\n\n"
echo ""
echo "<html>"
echo "<head>"
echo "<title>system information for $(hostname -s)</title>"
echo "</head>"
echo "<body>"
echo "<h1>General system information for host $(hostname -s)</h1>"
echo "</body>"
echo "</html>"
EOF
When I execute the script from the terminal, this is the output:
(which is fine, isn't it?)
And I don't get any error messages at all
Code:
#!/bin/bash
echo "Content-type: text/html\n\n"
echo ""
echo "<html>"
echo "<head>"
echo "<title>system information for server</title>"
echo "</head>"
echo "<body>"
echo "<h1>General system information for host server</h1>"
echo "</body>"
echo "</html>"
I also changed the default site:
(changes are marked in red)
Code:
<VirtualHost *:80>
        ServerAdmin laurentdw007@hotmail.com

        DocumentRoot /home/www/
        <Directory />
                Options FollowSymLinks
                AllowOverride None
        </Directory>
        <Directory /home/www/>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride None
                Order allow,deny
                allow from all
        </Directory>

        ScriptAlias /cgi-bin/ /home/www/cgi-bin/
        <Directory "/home/www/cgi-bin">
                AllowOverride None
                Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
                AddHandler cgi-script .cgi .sh
                Order allow,deny
                Allow from all
        </Directory>

        ErrorLog /var/log/apache2/error.log

        # Possible values include: debug, info, notice, warn, error, crit,
        # alert, emerg.
        LogLevel warn

        CustomLog /var/log/apache2/access.log combined

    Alias /doc/ "/usr/share/doc/"
    <Directory "/usr/share/doc/">
        Options Indexes MultiViews FollowSymLinks
        AllowOverride None
        Order deny,allow
        Deny from all
        Allow from 127.0.0.0/255.0.0.0 ::1/128
    </Directory>

</VirtualHost>
According to the error log, the script isn't executed, but WHY NOT? It is also the only script in the "cgi-bin" directory...
Any help?
Thanks!!
 
Old 07-02-2012, 09:24 AM   #6
Annielover
Member
 
Registered: May 2011
Location: Belgium
Distribution: Ubuntu Server Edition, Fedora 16
Posts: 90

Original Poster
Rep: Reputation: 8
Nvm, I got it working:

Just removing "cat > /home/www/cgi-bin/sysinfo.sh << EOF" works fine for me!
 
Old 07-02-2012, 11:31 AM   #7
lithos
Senior Member
 
Registered: Jan 2010
Location: SI : 45.9531, 15.4894
Distribution: CentOS, OpenNA/Trustix, testing desktop openSuse 12.1 /Cinnamon/KDE4.8
Posts: 1,144

Rep: Reputation: 217Reputation: 217Reputation: 217
Quote:
Originally Posted by Annielover View Post
cat > /home/www/cgi-bin/sysinfo.sh << EOF
this can never work, it was so obvious, that probably most of us overlooked it ! Damn.

Because if you run "cat" command it expects a filename to parse (and you have just redirected the output to "sysinfo.sh"), without it, it doesn't do anything.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
CGI Files Download and Won't Execute 2kau Linux - Newbie 1 07-20-2011 06:01 PM
CGI script 500 error newuser455 Programming 8 06-11-2006 01:56 PM
Perl-CGI 500 Error tzarcone Programming 1 07-01-2005 02:45 AM
RH 8.0 CGI 500 error (apache) 3Dxx Linux - General 46 06-27-2005 05:51 AM
Apache & CGI error 500 sortia Linux - Newbie 2 01-24-2005 11:38 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:32 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration