2

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!

12
  • Have you tried using the full path to the python interpreter? $log = shell_exec("/path/to/python3.8 /var/www/html/ytr/createfile.py"); Commented Oct 16, 2020 at 19:21
  • The Python script begins to run, as we can see the first print() statement returns output in the web interface, so Python starts successufly Commented Oct 16, 2020 at 20:42
  • check permissions of the log file, if you run it like you say All of the files work properly alone and everything worked then it would have created that log file owned by the user which would have not been www-data.. then you go and try run it with apache, and www-data doesn't own the file so can't write to it.. delete the file, allow www-data to write to the output dir and then should work Commented Oct 16, 2020 at 20:47
  • @tink I tried. Still same error Commented Oct 16, 2020 at 20:56
  • @LawrenceCherone Hey, thank for your suggestion. I am not sure where can I find those logs. Anyways, the www-data user does not exits on my system. cls output: [ninjatxas@brikas ytr]$ sudo usermod -a -G web www-data [sudo] password for ninjatxas: usermod: user 'www-data' does not exist Commented Oct 16, 2020 at 21:01

1 Answer 1

3

I solved it myself. The problem was caused by SELinux.

First, see if you have SELinux on your system:

sestatus

To disable it temporarily and see if that is your problem too, run:

sudo setenforce 0

If your script worked after disabling it, consider relaxing SELinux or turning it off permanently. To turn it off permanently, you can follow this link.

Sign up to request clarification or add additional context in comments.

1 Comment

The best solution for every apache permission denied

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.