Meyertrix
Posts: 14
Joined: Tue Dec 10, 2013 8:16 am

Execute Python script from Apache

Tue Jan 07, 2014 2:50 pm

Hi, I need some help. I've been struggling for a while now to get a script executed from a local website. I have installed the following:
sudo apt-get install apache2 php5 libapache2-mod-php5
Added to bottom of sudoers file: www-data ALL=(root) NOPASSWD: ALL

Index.html

Code: Select all

<html>
<head>
<meta c
</head>


<?php
if (isset($_POST['RightOPEN']))
{
exec("sudo python /home/pi/test1.py");
}
if (isset($_POST['LeftOPEN']))
{
exec("sudo python /home/pi/test2.py");
}
?>
<form method="post">
<button name="LeftOPEN">Left Door</button>&nbsp;
<button name="RightOPEN">Right Door</button><br>



</form>
</html>
Python script

Code: Select all

#!/usr/bin/python
import os
os.system('sudo pihat --id=1 --channel=15 --state=0')
What am i doing wrong? Script executes fine from terminal?

JoeDaStudd
Posts: 274
Joined: Fri Jan 27, 2012 1:34 pm

Re: Execute Python script from Apache

Tue Jan 07, 2014 3:02 pm

Do you get any error messages from the php script?
I would guess its an issue with the permissions, the web user would need execution rights which iirc isn't set by default for security reasons.

Code: Select all

sudo chmod 777 /var/www
Should work, however I'm rubbish at permissions and I mainly play around offline so security is never a major issue for me.

Meyertrix
Posts: 14
Joined: Tue Dec 10, 2013 8:16 am

Re: Execute Python script from Apache

Wed Jan 08, 2014 6:48 am

Thanks for the tip. I have executed the command but it is still not working. I also don't care about security as I am running it offline also.

Where do I find a log file to see what the problem or error message is?

User avatar
Jednorozec
Posts: 809
Joined: Sun Nov 24, 2013 2:17 pm
Location: Deposit, NY

Re: Execute Python script from Apache

Wed Jan 08, 2014 8:17 am

PHP errors are written to error_log in the directory where the script is. You might want to run <?php phpinfo() ?> to make sure that error reporting is turned on.
The most important leg of a three legged stool is the one that's missing.
It's called thinking. Why don't you try it sometime?

User avatar
rpdom
Posts: 15615
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Execute Python script from Apache

Wed Jan 08, 2014 8:32 am

Have a look at the files /var/log/apache2/error.log and /var/log/apache2/access.log

Meyertrix
Posts: 14
Joined: Tue Dec 10, 2013 8:16 am

Re: Execute Python script from Apache

Wed Jan 08, 2014 8:36 am

I am a complete NOOB, can you please explain where to put the <?php phpinfo() ?>. I have tried and no error_log is showing up in the directory.

Meyertrix
Posts: 14
Joined: Tue Dec 10, 2013 8:16 am

Re: Execute Python script from Apache

Wed Jan 08, 2014 8:49 am

/var/log/apache2/error.log
[Wed Jan 08 08:39:48 2014] [error] [client 192.200.111.125] file does not exist: /var/www/favicon.ico
- above is repeated over and over.

/var/log/apache2/error.log
"
192.200.111.125 - - [08/Jan/2014:08:39:48 +0000] "POST / HTTP/1.1" 200 607 "http://192.200.111.123/" "Mozilla/5.0 (BlackBerry; U; BlackBerry 9900; en) AppleWebKit/534.11+ (KHTML, like Gecko) Version/7.1.0.714 Mobile Safari/534.11+"
192.200.111.125 - - [08/Jan/2014:08:39:48 +0000] "GET /favicon.ico HTTP/1.1" 404 502 "-" "Mozilla/5.0 (BlackBerry; U; BlackBerry 9900; en) AppleWebKit/534.11+ (KHTML, like Gecko) Version/7.1.0.714 Mobile Safari/534.11+"
- above is also repeated over and over

192.200.111.125 is the IP of the raspberry

User avatar
rpdom
Posts: 15615
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Execute Python script from Apache

Wed Jan 08, 2014 9:06 am

The "faveicon.ico" message is unimportant. Whenever most browsers visit a website they look for a file called faveicon.ico. This is the icon you can sometimes see in the address bar.

The other messages are a POST, which is usually a form being completed, which was successful (200 status), followed by an attempt to find a faveicon. Neither of these are PHP errors.

If you create a file in /var/www that contains just "<?php phpinfo() ?>", call it test.php for now, then use a browser to access page test.php on your IP address. You should get a long page of PHP information and settings. But error reporting is turned on by default on Debian-based systems like Raspbian.

Meyertrix
Posts: 14
Joined: Tue Dec 10, 2013 8:16 am

Re: Execute Python script from Apache

Wed Jan 08, 2014 9:16 am

rpdom I am now able to access the PHP info, is there anything I should be looking for as to why this is not working?

Found some info here but not sure how to proceed http://stackoverflow.com/questions/1501 ... eb-browser

Meyertrix
Posts: 14
Joined: Tue Dec 10, 2013 8:16 am

Re: Execute Python script from Apache

Wed Jan 08, 2014 9:40 am

Okay, just got it working :lol:

What I did was to create a file called home.php under /var/www and coppied my code from index.html above into home.php
I then navigated to it from my browser 192.200.111.123/home.php and it is working 100%

User avatar
DougieLawson
Posts: 36578
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: Execute Python script from Apache

Wed Jan 08, 2014 7:06 pm

Meyertrix wrote:I am a complete NOOB, can you please explain where to put the <?php phpinfo() ?>. I have tried and no error_log is showing up in the directory.
nano /var/www/foo.php
add the <? phpinfo() ?> in there
save it

then point a web browser at http://192.168.xxx.xxx/foo.php
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

revathirevs
Posts: 2
Joined: Fri Sep 04, 2015 12:06 pm

Re: Execute Python script from Apache

Wed Sep 09, 2015 9:56 am

hello friends,

iam trying to run python scripts on apache2. but it only shoing source codes of py scripts.
when iam trying to run .html files , it gives 100% result.

why not with .py ?

remarks: i added addhandler cgi.py to directory

Return to “General discussion”