简体   繁体   English

亚马逊EC2上的PHP和Python

[英]PHP and Python on amazon ec2

I am using Amazon ec2 obunto micro instance. 我正在使用Amazon ec2 obunto微型实例。 I have wrote a php code which executes a python code and echo the result which is a simple string. 我写了一个php代码,该代码执行一个python代码并回显一个简单字符串的结果。 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. 当我在obuntu终端上执行它时,它完美地显示了结果,但是当我通过浏览器访问它时,它什么也没有显示。 And I have no idea why. 而且我不知道为什么。 Actually it cannot execute the python script. 实际上,它无法执行python脚本。

$tmp = exec('/usr/bin/python /var/www/similarity.py employee/unemployed/ waiter'); $ tmp = exec('/ usr / bin / python /var/www/similarity.py员工/待业/服务员');

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 PS:我使用的是Mac book pro,当我在计算机的本地主机中使用相同的代码时,一切正常

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. 首先,您需要确定正在执行php的当前用户。 You can either check out php.info file or use 您可以签出php.info文件或使用

$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). 就我而言,它是apache而不是www-data (我本来不应该这样假设)。

After that you will need to edit the sudoers file (etc/sudoers) 之后,您将需要编辑sudoers文件(etc/sudoers)

Add the lines over there. 在那边添加线。

You can use @Janith's code, if you want to be specific. 如果要具体说明,可以使用@Janith的代码。

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. 然后通过php执行脚本。

This is permission problem to access python file. 这是访问python文件的权限问题。 When you running it through server python script access as apache user(most probably www-data). 通过服务器python脚本运行它时,以apache用户(最可能是www-data)的身份访问。 So apache user doesn't having privilege to execute the python file. 因此,apache用户没有权限执行python文件。

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. 您可以执行的操作是,以sudo身份运行此命令,并在/ etc / sudoers文件中将对apache用户(www-data)的所有必要访问权限添加到以下示例中。

 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. 这只是示例,您应根据您的环境更改此行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM