Author |
|
Tycek
Joined: 19 Oct 2017 Posts: 6 Location: Czech Republic
|
Posted: Thu 19 Oct '17 13:17 Post subject: CGI setup |
|
|
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: 3092 Location: Hilversum, NL, EU
|
Posted: Thu 19 Oct '17 14:07 Post subject: |
|
|
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
|
Posted: Sun 22 Oct '17 12:18 Post subject: |
|
|
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: 1266 Location: Amsterdam, NL, EU
|
Posted: Sun 22 Oct '17 18:35 Post subject: |
|
|
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
|
Posted: Sun 22 Oct '17 21:30 Post subject: |
|
|
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: 1266 Location: Amsterdam, NL, EU
|
|
Back to top |
|
Tycek
Joined: 19 Oct 2017 Posts: 6 Location: Czech Republic
|
Posted: Tue 24 Oct '17 11:42 Post subject: |
|
|
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: 152 Location: Schömberg, Baden-Württemberg, Germany
|
Posted: Tue 24 Oct '17 22:25 Post subject: |
|
|
Might help or not - 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: 7371 Location: Germany, Next to Hamburg
|
Posted: Tue 31 Oct '17 17:57 Post subject: |
|
|
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
|
Posted: Fri 03 Nov '17 17:25 Post subject: |
|
|
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
When I try to check what python version it uses by writing 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: 1266 Location: Amsterdam, NL, EU
|
Posted: Fri 03 Nov '17 17:34 Post subject: |
|
|
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
|
Posted: Thu 16 Nov '17 17:53 Post subject: |
|
|
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 |
|