Open PY in PHP


8 posts
by piet bierbuik » Sun Mar 19, 2017 9:27 am
Hi guys,

My name is piet and i started a raspberry project with my father.
We installed apache and php on a jessy OS
in the directory /var/www/html/webiste we placed 2 files.
buttons.php
Code: Select all
<?php
exec('/var/www/html/webiste/hello.py');
?>

hello.py
Code: Select all
#!/usr/bin/python
print("hello world")


But this does not work.
If i go to http://192.168.178.15/website/button.php
The screen is empty, no error and no hello world

in the terminal i typed in sudo visudo
and added
www-data ALL=(ALL:ALL) ALL

What are we doing wrong ?

Thanks guys
Posts: 4
Joined: Sun Mar 19, 2017 9:11 am
by jelopo » Sun Mar 19, 2017 1:38 pm
Hello,

Perhaps a PHP problem try this :
Code: Select all
<?php
echo exec('/var/www/html/webiste/hello.py');
?>

Be sure hello.py is executable.

Bye
Posts: 1073
Joined: Wed Oct 17, 2012 7:53 pm
by piet bierbuik » Sun Mar 19, 2017 4:48 pm
No that does not work, still a blank page

But you tell me the py file should be executable.
How do i make it executable ??
In the terminal i used chmod 777 hello.py to change the permissions
Posts: 4
Joined: Sun Mar 19, 2017 9:11 am
by jelopo » Mon Mar 20, 2017 8:59 am
Hi,

With the chmod command hello.py is executable.

Now either apache or php is not running.
You can try by adding a line in your php file
Code: Select all
echo "PHP Hello \n ";


or Python file doesn't run.
check the python program in a terminal connected with www-data user
Code: Select all
/var/www/html/webiste/hello.py

and
Code: Select all
python /var/www/html/webiste/hello.py


bye
Posts: 1073
Joined: Wed Oct 17, 2012 7:53 pm
by ghp » Mon Mar 20, 2017 12:52 pm
Hello,
background processes are a bit difficult to analyze.
You can use log files and linux logging system for this.
in your hello.py, add the following lines:
Code: Select all
#!/usr/bin/python
import syslog
syslog.syslog("hello.py")
print("hello world")

Then each time this is executed, an entry in /var/syslog is appended. If it is not logged, it did not run.
Code: Select all
Mar 20 13:38:40 raspberrypi hello.py: hello.py

In a separate terminal window, type "tail -f /var/syslog" and leave this open and you see all incoming messages.

Also important is apache2 log files in /var/log/apache2/ . There are quite a few, the last one written is found with "ls -altr"
example:
Code: Select all
[Mon Mar 20 13:29:35.247507 2017] [core:notice] [pid 1031] AH00094: Command line: '/usr/sbin/apache2'
sh: 1: /var/www/html/webiste/hell.py: not found


As already stated by jelopo, there is a php problem:
Code: Select all
<?php
echo exec('/var/www/html/webiste/hello.py');
?>

Last, you mix the 'website' and 'webiste', 'button.php' and 'buttons.php'. Review your url to reflect the names in the file system.
Regards,
Gerhard
Posts: 1109
Joined: Wed Jun 12, 2013 12:41 pm
Location: Stuttgart Germany
by piet bierbuik » Mon Mar 20, 2017 9:28 pm
Thanks for your reply

I learned a bit more about the log files.
I used syslog to check if the script was running (location /var/log/syslog)
When i used apache to run the script no record.

When i ran the script from the terminal it got recorded
Mar 20 21:59:05 raspberrypi /hello.py: hello.py

Then i checked the apche log
sh: 1: /var/www/html/website/hello.py: Permission denied (changed to chomd 777 *)
second run
sh: 1: /var/www/html/website/hello.py: not found

So now what ? :D

something i think is strange
I use jessie, when i change the permission and double clicked hello.py a screen popped up.
saying something like: this file looks to be an executable do you want to run, run in terminal, open,cancel
when i run it says, runnig of the daughter file failed. file does not exist. (my settings are in dutch so its not exact)
???
Posts: 4
Joined: Sun Mar 19, 2017 9:11 am
by ghp » Mon Mar 20, 2017 10:29 pm
Hello,
when there are multiple problems, then nothing happens until all problems solved.
So try it inside out:

open a terminal (aka console aka lxterminal)
File /var/www/html/website/hello.py
go inside dir: cd /var/www/html/website
ls -l hello.py
result should be 'readable, 'executable for all (for me, it is -rwxrwxrwx 1 root root 54 Mar 20 13:50 hello.py I have made this from root, but when permissions are correct this does not matter)
execute: ./hello.py
result should be a printout.

File /var/www/html/website/buttons.php
go inside dir: the terminal is already in this dir, doublecheck with pwd
ls -l buttons.py
result should be 'readable, 'executable for all
execute: php ./buttons.php
result should be a printout 'hello world'.
if not,
display file cat buttons.php
- add the echo before the exec statement in buttons.php
- check path in exec statement, must be '/var/www/html/website/hello.py' (edited later, name was wrong)

check directory access.
cd /var/www/html/
ls -l
look for entry 'website', for me it is drwxr-xr-x so everybody can read and execute.

In browser, place url http://localhost/html/website/buttons.php

If there are still problems, can you access by apache/web simple php scripts like
<?php
echo "Hello, World!";
?>

Hope this helps
Gerhard
Posts: 1109
Joined: Wed Jun 12, 2013 12:41 pm
Location: Stuttgart Germany
by piet bierbuik » Tue Mar 21, 2017 4:40 pm
It sure helped
I found the problem !

these are the results:
-rwxrwxrwx 1 pi pi 83 mrt 20 22:28 hello.py

pi@raspberrypi:/var/www/html/website $ ./hello.py
bash: ./hello.py: /usr/bin/python^M: invallid interpreter: File or folder does not exist

pi@raspberrypi:/var/www/html/website $ python ./hello.py
hello world

pi@raspberrypi:/var/www/html/website $ php ./buttons.php
sh: 1: /var/www/html/website/hello.py: not found

The interpreter was not correct. i found out that hello.py was in dos format, because of the ^M. But it should be unix format.

I installed the program dos2unix (sudo get-apt dos2unix)
And changed the format to unix (cd /var/www/html/website/)(sudo dos2unix hello.py)

Now the url is without the ^M

I ran the scipt and it worked !

Thanks guys for your help !
Could not found this out without you
Posts: 4
Joined: Sun Mar 19, 2017 9:11 am