I am very new to PHP & Apache.
I am building a small webpage - which can run some python code on the server on click of a button from a client computer. To do this, I have read a few articles on the web which tell PHP is the way to do it. I also read that I wouldn't need AJAX. My understanding of PHP, AJAX and related technologies are at a beginner level.
I have an Apache server running on my RHEL 6.5 machine. I have PHP v5.3.3, Python v2.6.
PHP script (script.php) :
<?php
echo shell_exec("python pytest.py");
?>
Location : /var/www/html/script.php
Permissions : chmod 777 script.php
Python Script (pytest.py) :
import os
os.remove("/<path>/hon.txt")
Location : /var/www/html/pytest.py
Permissions : chmod 777 pytest.py
Index HTML (index.html) :
<!DOCTYPE html>
<html>
<header>
<title>Python Run</title></header>
<body>
<p>
<b><font size="10" color="red"><div align="center">Run Python Jobs</div></font></b>
</p>
<table style="width:100%">
<tr>
<td>Script1</td>
<td>Description</td>
<td>
<form action="script.php" method="get">
<input type="submit" value="Run Py Script">
</form>
</td>
</tr>
</table>
</body>
</html>
Other things/tests done :
- The Python script works standalone, its a simple python script to delete a file.
- The PHP script also works standalone on the machine running the Apache server.
When I try the button click from HTML, the PHP script does not seem to work.
I have checked the log messages from
cd /var/log/ tail messages -f May 6 18:22:01 machine abrt: detected unhandled Python exception in 'pytest.py'I was not sure if this could be because of permissions, so I did a
chmod 777 -R /var/wwwEven then I had the same problems
I was not sure where things were going wrong, so I made 2 more changes to the PHP script (script.php) :
<?php echo shell_exec("/usr/bin/python /<path>/pytest.py"); echo shell_exec("ls"); ?>Over here - the second line works from browser, the first one does not. Which means my python portion is not getting executed.
I read somewhere if this is a problem because my Apache server is not run with root privileges. Being new - I don't know how to check that.
If any more information is needed - I can provide details in successive edits.
1 Answer 1
Try placing the full path to your file. In your case you just say 'pytest.py', so replace it with the full path.
Doing so, this would result in something like this:
<?php
echo shell_exec("/usr/bin/python /home/username/scripts/pytest.py");
?>
(Also, you missed one / in front of usr/bin/python)
5 Comments
"/usr/bin/python /var/www/html/pytest.py" already? And what's the error message when you try this? (or is it still the same?)
I am very new to PHP- Run! Run away! Run like your life depends on it!#!/usr/bin/python2.7(or whatever version) to the top of the file.May 6 18:22:01 machine abrt: detected unhandled Python exception in 'pytest.py'you need to debug your python script, handle the exception to see what the error is.