I setup Ubuntu server 18.04 LTS, LAMP, and mod_mono (which appears to be working fine alongside PHP now by the way.) Got python working too; at first it gave an HTTP “Internal Server Error” message. sudo chmod +x myfile.py
fixed this error and the code python generates is displayed fine. But any time the execute permission is removed from the file (such as by uploading a new version of the file), the execute bit is stripped and it breaks again.
A work-around was implemented with incrontab, where the cgi-bin folder was monitored for changes and any new writes caused chmod +x %f
to be ran on them. This worked for awhile, then stopped, and seems a hokey solution at best. Perl, PHP, even ASPX do not need to be marked executable – only python.
Is there any way Apache can “run” python without the file marked as executable?
2
Answers
I don't think Apache is capable of serving executed python scripts without the execute bit set on the .py file.
But here is a work-around: simply leave that file marked executable, but
import
a second python file. That second file does not need to be marked executable.myfile.py (marked as executable and read-only - use this with apache):
myfile2.py (marked RW only, edit this file freely):
The reason PHP works, is because the interpreter is loaded into Apache. So Apache interprets the code.
For your Python, it runs as a CGI, so the interpreter is outside of Apache.
In your Python script, you probably have a
#!/usr/bin/python
first line (or something similar). This tells the script to run using this interpreter. This requires executable permission on the.py
file, and allows you to callmyfile.py
directectly.Instead run it like this:
/usr/bin/python myfile.py
. This way the interpreter is the executable, and it will run myfile.py as the code.Examples
You want to run the py file “alone”:
You want to run it via the python executable, like you want via Apache: