LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   Execute .sh files in webbrowser with apache2 as server (https://www.linuxquestions.org/questions/linux-general-1/execute-sh-files-in-webbrowser-with-apache2-as-server-4175553453/)

_n00b 09-14-2015 08:50 AM

Execute .sh files in webbrowser with apache2 as server
 
Hi, i just installed "apache2" on my Ubuntu 14.04 virtual machine.
I try to execute this simple .sh file from the browser.

Code:

#!/bin/bash
DATE="$(date)"
echo "Content-type: text/html"
echo ""
echo "<html><head><title>Test</title></head><body>"
echo "Today is $DATE <br>"

I saved this file as "date.sh" under "/var/www/html/projekt/cgi-bin/date.sh" and changed it's permissions to 755 with chmod. In firefox i write this into the address bar:
Code:

localhost/projekt/cgi-bin/date.sh
But the window in the following screenshot opens, and the code is not executed.
http://www.pasteall.org/pic/show.php?id=93097

It is a save/open file dialog. I already searched for a solution on google and found this:
Code:

sudo a2enmod cgi
service apache2 reload

I get "Module cgi already enabled".
And the problem still remains, even after restarting the browser.
Does anyone know how to solve this?

bathory 09-14-2015 12:30 PM

Since this is not the default cgi-bin directory for apache in ubuntu, you need to add the following:
Code:

<Directory /var/www/html/projekt/cgi-bin>
Options +ExecCGI
AddHandler cgi-script .sh
</Directory>


_n00b 09-15-2015 01:18 AM

Solved
 
Quote:

Originally Posted by bathory (Post 5420267)
Since this is not the default cgi-bin directory for apache in ubuntu, you need to add the following:
Code:

<Directory /var/www/html/projekt/cgi-bin>
Options +ExecCGI
AddHandler cgi-script .sh
</Directory>


Thank you for the hint! I added these lines to my /etc/apache2/apache2.conf file. After this i restartet my browser and apache2, now it finally works!! Thank you very much sir. :hattip:

_n00b 09-15-2015 09:02 AM

Ok with .sh it works fine. But i just tried it with this simple perl script.
Code:

#! /usr/bin/perl
use CGI':standard';
print "<p>Hello World</p>";
exit(0);

But it tries to download it again.
Should i create an extra thread for this problem?

bathory 09-15-2015 04:15 PM

Quote:

Originally Posted by _n00b (Post 5420646)
Ok with .sh it works fine. But i just tried it with this simple perl script.
Code:

#! /usr/bin/perl
use CGI':standard';
print "<p>Hello World</p>";
exit(0);

But it tries to download it again.
Should i create an extra thread for this problem?

If the perl script has a .pl extension, you should add it in the AddHandler directive:
Code:

<Directory /var/www/html/projekt/cgi-bin>
Options +ExecCGI
AddHandler cgi-script .sh .pl
</Directory>


_n00b 09-16-2015 08:42 AM

I already tried this before exactly like you wrote, it still tries to download tho.

bathory 09-16-2015 10:30 AM

Quote:

Originally Posted by _n00b (Post 5421176)
I already tried this before exactly like you wrote, it still tries to download tho.

Are you sure it's mot your browser cache?
Clear the cache, or use another browser and try again

_n00b 09-18-2015 01:36 AM

Ah ok, but now i get internal server error. Do you know what a possible cause could be? I already did "chmod 755 test.pl". I tried to execute the script from the console, following output appeared: <p>Hello World</p>, so the script should work, right?
But why i get the internal server error then?

Edit

Solved, i had to add this line to my script:

So this is the working script:

Code:

#! /usr/bin/perl
use CGI':standard';
print "Content-type: text/html\n\n";
print "<p>Hello World</p>";
exit(0);


bathory 09-18-2015 03:43 AM

Quote:

Originally Posted by _n00b (Post 5422110)
Ah ok, but now i get internal server error. Do you know what a possible cause could be? I already did "chmod 755 test.pl". I tried to execute the script from the console, following output appeared: <p>Hello World</p>, so the script should work, right?
But why i get the internal server error then?

Edit

Solved, i had to add this line to my script:

So this is the working script:
Edit

Solved, i had to add this line to my script:

So this is the working script:

Code:

#! /usr/bin/perl
use CGI':standard';
print "Content-type: text/html\n\n";
print "<p>Hello World</p>";
exit(0);


Code:

#! /usr/bin/perl
use CGI':standard';
print "Content-type: text/html\n\n";
print "<p>Hello World</p>";
exit(0);


Glad to see you've made it and thx for providing the solution.
You can mark the thread "SOLVED" using the "Thread Tools_ on top of the page

Regards


All times are GMT -5. The time now is 02:35 PM.