0

I am using Amazon ec2 obunto micro instance. I have wrote a php code which executes a python code and echo the result which is a simple string. When I execute it on obuntu terminal it shows the result perfectly, but when I access it through the browser it doesn't show anything. And I have no idea why. Actually it cannot execute the python script.

$tmp = exec('/usr/bin/python /var/www/similarity.py employee/unemployed/ waiter');

If anyonw can help me I would really appreciate it. PS: I am using a mac book pro and when I use the same codes in the localhost of my computer everything works perfectly

0

2 Answers 2

1

After a lot of "scratching my head", I finally figured it out.

First of all you will need to figure out current user who is executing the php. You can either check out php.info file or use

$processUser = posix_getpwuid(posix_geteuid());
print $processUser['name'];

This will give you the user who is executing the code. In my case it was apache rather than www-data (I shouldn't have assumed so in first place).

After that you will need to edit the sudoers file (etc/sudoers)

Add the lines over there.

You can use @Janith's code, if you want to be specific.

apache ALL=NOPASSWD:/var/www/similarity.py
apache ALL=NOPASSWD:/usr/bin/python

or you can simply add

apache ALL=(ALL)        NOPASSWD:ALL

(You probably should just specify the path).

Then execute the script through php.

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

Comments

0

This is permission problem to access python file. When you running it through server python script access as apache user(most probably www-data). So apache user doesn't having privilege to execute the python file.

What you can do it is run this command as sudo and add all necessary access to apache user(www-data) in /etc/sudoers file as below sample.

 www-data ALL=NOPASSWD:/var/www/similarity.py
 www-data ALL=NOPASSWD:/usr/bin/python

This is just the sample, you should change this line as according to your environment.

Comments

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.