Serving up Python CGI files on a Synology NAS

2k Views Asked by At

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.

1

There are 1 best solutions below

4
On

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