1

I cannot get python files to be served up with Apache 2.2 or 2.4 without a 500 error. I have WebStation installed, python, perl, php, and Apache 2.2 and 2.4 installed.

I can serve up static files just fine with apache. When I try to serve up a most basic "hello world" cgi, I get a 500 error. The error is

[cgid:error] [pid 10076:tid 140542621480832] (2)No such file or directory: AH01241: exec of ['/volume2/Development/WebRepo/cgi-bin/test.py' failed.

Tried to execute both a perl script and a python script. Both run successfully from a command line, but not from served up with Apache (same errors of "no such file..") Also note this is a 500 error, not a 404, so it's seeing the file. I can serve up static HTML files just fine.

The python script couldn't be simpler:

#!/usr/bin/python
print "Content-type: text/html\n\n";
print "Hello, World.";

All files have 755 permissions. The path to python is correct. I'm at a loss as to what to do next.

LarryBud
  • 990
  • 9
  • 20
  • Not much of an Apache user but once that I used it my files had correct permissions etc, but one of the folders didn't. Did you happen to check that too? Edit: this seems related too: https://stackoverflow.com/q/7858987/6655150 – Kostas Mouratidis Dec 04 '20 at 13:00

1 Answers1

2

Python can serve CGI scripts out of the box, using http.server.CGIHTTPRequestHandler.

On my Synology NAS I have official Python3 package installed (version 3.8.2-0150). I can SSH into NAS as admin and add a script:

mkdir -p app/cgi-bin

cat << EOF > app/cgi-bin/hello.py
#!/usr/bin/env python3

print('Content-Type: text/html')
print()
print('<html><body><h2>Hello World!</h2></body></html>')
EOF

After that I can run it like this (note that --directory doesn't have effect for --cgi so I cd there):

cd app && python3 -m http.server --cgi

Then on my machine, I can curl http://nas:8000/cgi-bin/hello.py.

Running on boot

You can run this automatically on boot via the task scheduler.

Control PanelTask SchedulerCreateTriggered TaskUser-defined script. Fill these on General tab:

  • Task: Python CGI
  • User: admin
  • Enabled: [x]

And User-defined script on Task Settings tab:

cd /var/services/homes/admin/app
python3 -m http.server --cgi

Then you can run it manually. It should also run on reboot.

Permissions

If you want to run the task as root, make sure file permissions are correct from root's point of view. In my case there's discrepancy by some reason.

$ ls -l app/cgi-bin/hello.py 
-rwxrwxrwx+ 1 admin users 122 Nov 29 14:50 app/cgi-bin/hello.py
$ sudo ls -l app/cgi-bin/hello.py 
Password: 
-rwx--x--x+ 1 admin users 122 Nov 29 14:50 app/cgi-bin/hello.py
saaj
  • 23,253
  • 3
  • 104
  • 105
  • Thanks, but I don't have a problem running the python script from a CLI, the issue is having the Apache server in Synology serve it up successfully. – LarryBud Dec 01 '20 at 16:03
  • My answer shows how to use Python built-in HTTP server to serve CGI scripts (which can be arbitrary executables, not just "Python scripts"). It can also serve static files, so you may not need another server (i.e. Apache) at all if it already covers your use case(s). – saaj Dec 01 '20 at 18:15
  • Thanks. I understand what you're showing, but it doesn't really pertain with getting Apache working to serve up python scripts, which was the original question posted. – LarryBud Dec 01 '20 at 20:00
  • this was exactly what i needed. 1000x would rather deal with python than apache. – keithpjolley Dec 31 '21 at 01:44