I need a way to execute "python3.8 createfile.py" script on my Linux server through a web html interace. In the future I will need it to also retrieve form data from HTML web interface to provide as arguments to the shell command, thus the method must be expandable to that, yet I am not looking for this part of the solution right now
I am using a PHP exec_shell method, but Python throws an error:
[Errno 13] Permission denied: '/var/www/html/ytr/output/createfile_2020-10-16_203623.txt'
I am running apache (httpd) on my CentOS linux VPS.
ytr/index.html
<! -- Version 0.10 -->
<form action="/ytr/testexec.php">
<input type="submit" value="Open Script">
</form>
ytr/testexec.php
// version 0.10
<?php
$log = shell_exec("python3.8 /var/www/html/ytr/createfile.py");
echo "<pre>Erorr log: $log</pre>";
//header('Location: http://xxx.xxx.xxx.94/ytr/index.html?success=true');
//x's are of course real numbers.
?>
ytr/createfile.py
from datetime import datetime
import os
# version 0.10
# Maybe its going to be woring directory
scr_dir = os.path.dirname(os.path.realpath(__file__)) + "/output/"
fin_name = scr_dir + "createfile_" + \
datetime.now().strftime("%Y-%m-%d_%H%M%S" + ".txt")
try:
fin = open(fin_name, "w")
fin.write("Today is " + str(datetime.now()))
except Exception as e:
print(e)
All of the files work properly alone. index.html is displayed properly when accessed through internet browser; when called through linux CLI, testexec.php successfully runs createfile.py, which then successfully executes (creating a file in ytr/output); manually calling "python3.8 createfile.py" in Linux CLI also works well.
When I click the "Open script" button on my page, the file does not appear in ytr/output and I get this: error displayed on web interface
Weirdly, there is no error message, that would be contained in $log in PHP script.
Things I have tried:
- Persmissions. I have given put 'apache' user in the 'web' group. Set the group of all files and folders in ytr to 'web' and given the group permissions to rwx (read write execute). For testing I have even put the other (o) permissions to rwx. Here is info from CLI: lid, ls -l and getfacl CLI outputs
- Updating PHP to 7.3.23 and rebooting linux server.
Thank you in advance!
P.S. If there is another (non php) solution that could run a bash command from html, with the ability to take parameters from web interface in the future, I would be happy to use it!
$log = shell_exec("/path/to/python3.8 /var/www/html/ytr/createfile.py");