2

I've read multiple SO posts regarding this, but can't seem to get this to work. This is my first time working with Python on Apache so I would appreciate the help I can get!

So ultimately, I'm trying to run a Python script in my htdocs, but I can't seem to just get the simple python script running on XAMPP. I keep getting a 500 error:

error

myurl.py

#!/usr/bin/env python3

print("Content-Type: text/html")
print()
print ("""
    <TITLE>CGI script ! Python</TITLE>
    <H1>This is my first CGI script</H1>
    Hello, world!
"""
)
Sam
  • 495
  • 3
  • 11
  • 19
  • 1
    try replacing the first line with print("Content-type: text/html\n\n") – Madison Courto Nov 20 '18 at 22:42
  • @MadisonCourto I tried replacing it and it didn't help – Sam Nov 20 '18 at 22:45
  • @Sam Check out the error log written by Apache. It will give you more information about exactly what caused the 500 error. It's usually stored somewhere like `/var/log/apache2/error.log` – Rob Bricheno Nov 20 '18 at 22:47
  • @RobBricheno [Tue Nov 20 17:49:06.593901 2018] [cgi:error] [pid 47854] [client ::1:50462] AH01215: (13)Permission denied: exec of '/Applications/XAMPP/xamppfiles/htdocs/myurl.py' failed: /Applications/XAMPP/xamppfiles/htdocs/myurl.py [Tue Nov 20 17:49:06.595547 2018] [cgi:error] [pid 47854] [client ::1:50462] End of script output before headers: myurl.py – Sam Nov 20 '18 at 22:50
  • @RobBricheno it seems like I don't have the correct permissions right? how do I change that? – Sam Nov 20 '18 at 22:50
  • @Sam chmod 777 myurl.py - to at least test if it is a permission issue – Madison Courto Nov 20 '18 at 22:53
  • @Sam Right! You probably need to give execute permissions on the python file to the user under which apache is running. If you're on a debian-based system that's normally www-data. You can see which user is being used in the output of `ps aux` – Rob Bricheno Nov 20 '18 at 22:53
  • Using the command @MadisonCourto will work too, but will give everyone full permissions on your python file. That might be fine if this is just going to be a development system or you are the only user. In a real deployment it may present a security risk. – Rob Bricheno Nov 20 '18 at 22:55
  • @RobBricheno I completely agree, but it's a quick solution to test if it is indeed a permission issue – Madison Courto Nov 20 '18 at 22:58
  • @RobBricheno it doesn't seem to be a permission issue. I tried the command Madison suggested in my terminal, but now the error seems to be different. – Sam Nov 20 '18 at 23:00
  • [Tue Nov 20 17:59:04.720816 2018] [cgi:error] [pid 48715] [client ::1:50555] AH01215: python3: No such file or directory: /Applications/XAMPP/xamppfiles/htdocs/myurl.py [Tue Nov 20 17:59:04.720884 2018] [cgi:error] [pid 48715] [client ::1:50555] End of script output before headers: myurl.py – Sam Nov 20 '18 at 23:00
  • The file does indeed exist though... so I'm not really understanding why that error would exist – Sam Nov 20 '18 at 23:01
  • That means that `env` can't find `python3` in your system. Can you run `python3` by hand from the command prompt? Does `/usr/bin/python3` exist? If so, you could try changing the first line of your script (the *shebang* line) to read `#!/usr/bin/python3` – Rob Bricheno Nov 20 '18 at 23:04
  • @RobBricheno when I run` python3` by hand, it seems to exist but when I type in `/usr/bin/python3` it says no such file or directory exists – Sam Nov 20 '18 at 23:16
  • You can find the correct path to python3 using the command `which python3`. Then try using that complete path in the first line of your script. – Rob Bricheno Nov 20 '18 at 23:17
  • It worked! Thank you @RobBricheno – Sam Nov 20 '18 at 23:28
  • No problem, I hope you have a lot of fun. – Rob Bricheno Nov 20 '18 at 23:29

1 Answers1

4

As per the discussion, there were multiple problems here, which were solved by examining the error.log written by apache and then making appropriate changes.

The first error was:

[Tue Nov 20 17:49:06.593901 2018] [cgi:error] [pid 47854] [client ::1:50462] AH01215: (13)Permission denied: exec of '/Applications/XAMPP/xamppfiles/htdocs/myurl.py' failed: /Applications/XAMPP/xamppfiles/htdocs/myurl.py [Tue Nov 20 17:49:06.595547 2018] [cgi:error] [pid 47854] [client ::1:50462] End of script output before headers: myurl.py

The relevant part here is:

(13)Permission denied: exec of '/Applications/XAMPP/xamppfiles/htdocs/myurl.py' failed

Permissions needed to be set on the .py file being executed to allow the user running the apache process to execute the script. This was done using chmod.

Then, another error was presented:

[Tue Nov 20 17:59:04.720816 2018] [cgi:error] [pid 48715] [client ::1:50555] AH01215: python3: No such file or directory: /Applications/XAMPP/xamppfiles/htdocs/myurl.py [Tue Nov 20 17:59:04.720884 2018] [cgi:error] [pid 48715] [client ::1:50555] End of script output before headers: myurl.py

The relevant part is:

python3: No such file or directory

This shows that the system could not find a python3 binary to execute. The correct path to the python3 interpreter had to be determined using which python3. This was then edited into the shebang line of the script.

Rob Bricheno
  • 4,467
  • 15
  • 29