0

This is the code and it's purpose is to run a bash script. (index.php)

<html>
<title>
        hello
</title>


<b>hello</b>
<body>
<?php
exec('bash /var/www/html/test.sh');


?>
</body>
</html>

when do php index.php it will execute the test.sh but when I run it with browser it wont run the bash script.

consider that index.php is in the same directory of test.sh i've done chmod and chown.

this is the test.sh

#!/bin/bash
python3 /var/www/html/test.py

and this is the test.py

import logging
a = "Hello world "

LOG_FILENAME = '/var/www/html/test.txt'
logging.basicConfig(filename=LOG_FILENAME,level=logging.DEBUG)
logging.debug(a)
Reza Azimi
  • 56
  • 9
  • What does the test.sh do? How are you sure its not executed? Is there supposed to be an answer from the script? Because youre not echoing nor saving the result. Any error messages? – Definitely not Rafal Dec 03 '20 at 12:24
  • You can't run php directly in browser. There has to be a server to parse php – Ambrish Pathak Dec 03 '20 at 12:25
  • 1
    This is most probably an issue with no having the full path to "bash test.sh" – Raman Sailopal Dec 03 '20 at 12:26
  • @AmbrishPathak I have a webserver and I use it in /var/www/html/index.php – Reza Azimi Dec 03 '20 at 12:27
  • @DefinitelynotRafal The main purpose is run some python apps but and the shell is just executing them. and one of the is a hello word python that make log in test.txt and I tail that txt file. when I run index.php in terminal it will write the log but when I execute it with www.example.com/index.php it will show me the "hello" but no log in test.txt – Reza Azimi Dec 03 '20 at 12:29
  • @RezaAzimi what about all my other questions? – Definitely not Rafal Dec 03 '20 at 12:30
  • @DefinitelynotRafal The main purpose is run some python apps but and the shell is just executing them. and one of the is a hello word python that make log in test.txt and I tail that txt file. when I run index.php in terminal it will write the log but when I execute it with www.example.com/index.php it will show me the "hello" but no log in test.txt – Reza Azimi Dec 03 '20 at 12:32
  • Any error messages? have you checked the error log? Does PHP have the rights to write into the test.txt file? We would probably need to know what the test.sh does in detail. – Definitely not Rafal Dec 03 '20 at 12:36
  • Use the full path to `bash` _and_ use the full path to your `test.sh`. Also, maybe, inside your `test.sh` use full paths to whatever python scripts you're running – brombeer Dec 03 '20 at 12:36
  • Check you your web server logs for possible errors – Raman Sailopal Dec 03 '20 at 12:36
  • @RamanSailopal I tried that too but didn't work at all. – Reza Azimi Dec 03 '20 at 12:37
  • @brombeer i changed all of them but didn't work – Reza Azimi Dec 03 '20 at 12:45
  • @RamanSailopal How should I grant php to access that? and I had edit the question and wrote the .py and .sh details. – Reza Azimi Dec 03 '20 at 12:47
  • Does this answer your question? [PHP exec() command: how to specify working directory?](https://stackoverflow.com/questions/1679045/php-exec-command-how-to-specify-working-directory) – CBroe Dec 03 '20 at 12:58
  • Not sure why you'd need `sudo` to run a python script. – brombeer Dec 03 '20 at 13:07
  • @brombeer just a habit of using linux :D – Reza Azimi Dec 03 '20 at 13:22
  • @ the link wasn't useful for my case :( – Reza Azimi Dec 03 '20 at 13:56

2 Answers2

0

Could it be because the web server is trying to run a sudo'd command (sudo python3 /var/www/html/test.py) and is waiting for some user input?

Or could it be because the path to bash should be fully qualified, as the www-user account on your system may be missing some configuration.

Or maybe your shell script is missing an execute bit?

kaigoh
  • 500
  • 3
  • 11
0

After some changes of permissions it worked:

chown -R www-data:www-data /var/www
chmod go-rwx /var/www
chmod go+x /var/www
chgrp -R www-data /var/www
chmod -R go-rwx /var/www
chmod -R g+rx /var/www
chmod -R g+rwx /var/www
Reza Azimi
  • 56
  • 9