logo
Apache Lounge
Webmasters

 


About

Forum Index Downloads Search Register Log in  RSS Apache Lounge
 



Keep Server Online

If you find the Apache Lounge, the downloads and overall help useful, please express your satisfaction with a donation.

or

Bitcoin

A donation makes a contribution towards the costs, the time and effort that's going in this site and building.

Thank You! Steffen

Apache Lounge is not sponsored.

Your donations will help to keep this site alive and well, and continuing building binaries.



CGI setup

 
Post new topic   Reply to topic    Apache Forum Index -> Apache



View previous topic :: View next topic  
Author Message
Tycek



Joined: 19 Oct 2017
Posts: 6
Location: Czech Republic

PostPosted: Thu 19 Oct '17 13:17    Post subject: CGI setup Reply with quote

Hello.
I have been trying to setup my Apache server so it can run CGI scripts. I run this script in python and it is really simple script, which should output just "Hello CGI" to the site.
When I try to access this script from browser, it gives me Internal server error and in the error log there is a message, that says "End of script output before headers".
When I save that script as .py file, it runs just fine.
Also when I try to run the .cgi file in terminal on server, it works fine too.
I tried to change the apache2.conf file, where I added directory with the cgi-bin folder, but it still did not solve my problem.
I have this server on VPS, where runs Ubuntu 14.04 64-bit with PLESK installed on it.

Thank you for your replies.
Back to top
Steffen
Moderator


Joined: 15 Oct 2005
Posts: 2580
Location: Hilversum, NL, EU

PostPosted: Thu 19 Oct '17 14:07    Post subject: Reply with quote

This is typically an error that occurs when you are unable to view or execute the file, try:

chmod 755 the_file

You can also try to add -w th=o the shebang


#!/usr/bin/perl -w
Back to top
Tycek



Joined: 19 Oct 2017
Posts: 6
Location: Czech Republic

PostPosted: Sun 22 Oct '17 12:18    Post subject: Reply with quote

I gave it 777 permission via PLESK, but it still does not work.

-w in the shebang also did not solve it.
Back to top
Jan-E



Joined: 09 Mar 2012
Posts: 794
Location: Amsterdam, NL, EU

PostPosted: Sun 22 Oct '17 18:35    Post subject: Reply with quote

A *.py file is being executed by python, a *.cgi usually by perl. Could you post the contents of the script? Does it need python?
Back to top
Tycek



Joined: 19 Oct 2017
Posts: 6
Location: Czech Republic

PostPosted: Sun 22 Oct '17 21:30    Post subject: Reply with quote

It needs python, since the project I want to publish, is written in python.

Content of CGI file:
Code:
#!/usr/bin/python3 -w

print("Content-type:text/html")
print("")
print ("<html><head><title>CGI</title></head>")
print ("<body>")
print ("hello cgi")
print ("</body>")
print ("</html>")
Back to top
Jan-E



Joined: 09 Mar 2012
Posts: 794
Location: Amsterdam, NL, EU

PostPosted: Sun 22 Oct '17 23:08    Post subject: Reply with quote

Try https://www.linux.com/blog/configuring-apache2-run-python-scripts

I do not know if 'import cgitb' is required, but you might be able to test it.

Or, on Ubuntu 16.04 LTS:
https://www.server-world.info/en/note?os=Ubuntu_16.04&p=httpd&f=5

Not that both articles contain 'a2enmod cgi'.
Back to top
Tycek



Joined: 19 Oct 2017
Posts: 6
Location: Czech Republic

PostPosted: Tue 24 Oct '17 11:42    Post subject: Reply with quote

I tried to configure it according to those sites, but it still does not work.
Command a2enmod says, that CGI is enabled. Restarting Apache also did not solve it.
Back to top
mraddi



Joined: 27 Jun 2016
Posts: 28
Location: Schömberg, Baden-Württemberg, Germany

PostPosted: Tue 24 Oct '17 22:25    Post subject: Reply with quote

Might help or not Question - here are the steps that I used to get this script to work on Ubuntu 16.04 - Python3 was already installed:
    Installed Apache2 (sudo apt-get install apache2)
    enabled cgi (sudo a2enmod cgi)
    saved the script (without the "-w") into /var/www/cgi-bin/helloworld.py (vi /var/www/cgi-bin/helloworld.py)...
    ...and made it executable (chmod a+x /var/www/cgi-bin/helloworld.py)
    edited apache2's config as cgi seems to use /usr/lib/cgi-bin as default instead of my /var/www/cgi-bin
    restarted apache2 (sudo service apache2 restart)
    http://192.168.0.4/cgi-cin/helloworld.py worked as well as http://192.168.0.4/cgi-bin/motd.pl (192.168.0.4 is my Ubuntu-"Server", both files [pl and py] are located in the same directory)


By the way - the interesting part in the /etc/apache2/conf-available/serve-cgi-bin.conf looks this way:
Code:
ScriptAlias /cgi-bin/ /var/www/cgi-bin/
<Directory "/var/www/cgi-bin">
    AllowOverride None
    Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
    Require all granted
</Directory>

and the python-file reads this:
Code:
#!/usr/bin/python3

print("Content-type:text/html")
print("")
print ("<html><head><title>CGI</title></head>")
print ("<body>")
print ("Hello World!1elf1")
print ("</body>")
print ("</html>")
Back to top
James Blond
Moderator


Joined: 19 Jan 2006
Posts: 6255
Location: Germany, Next to Hamburg

PostPosted: Tue 31 Oct '17 17:57    Post subject: Reply with quote

Also make sure to have the Linux line endings. A file save on windows can cause some trouble.
Back to top
Tycek



Joined: 19 Oct 2017
Posts: 6
Location: Czech Republic

PostPosted: Fri 03 Nov '17 17:25    Post subject: Reply with quote

OK, so I decided not to execute it as .cgi file, but as .py file since that works.

But I stumbled on another problem. It seems that the CGI ignores the shebang line which says
Code:
#!usr/bin/python3


When I try to check what python version it uses by writing
Code:
print(sys.version)
it says, it uses python 2.7.6. Even when I delete the shebang, it works just fine, but uses the wrong version.

Does Apache have some config file, where it can set what python version it is going to use?
Back to top
Jan-E



Joined: 09 Mar 2012
Posts: 794
Location: Amsterdam, NL, EU

PostPosted: Fri 03 Nov '17 17:34    Post subject: Reply with quote

Try renaming /usr/bin/python in /usr/bin/python2 and /usr/bin/python3 in /usr/bin/python.

Probably Apache looks for 'python' in the directories specified in the PATH variable. In my case the PATH variable contains

/sbin:/usr/sbin:/bin:/usr/bin
Back to top
Tycek



Joined: 19 Oct 2017
Posts: 6
Location: Czech Republic

PostPosted: Thu 16 Nov '17 17:53    Post subject: Reply with quote

So you say, that I should try to rename the link

/usr/bin/python3 to /usr/bin/python

so the apache takes the python3 instead of the default python?

This change should be reversible if something goes wrong right?
I still wonder why it ignores the shebang though.
Back to top


Post new topic   Reply to topic    Apache Forum Index -> Apache
Page 1 of 1