首页 > 解决方案 > 从 Apache 调用 PHP 无法执行 Python 脚本

问题描述

这是我在 /var/log/apache2/error.log 中得到的错误:

Traceback (most recent call last):
  File "/var/www/html/combined.py", line 9, in <module>
    ltr559 = LTR559()
  File "/usr/local/lib/python2.7/dist-packages/ltr559-0.1.1-py2.7.egg/ltr559/__init__.py", line 212, in __init__
    BitField('ALS', 0x0F)
  File "/usr/local/lib/python2.7/dist-packages/i2cdevice-0.0.7-py2.7.egg/i2cdevice/__init__.py", line 166, in __init__
    self._i2c = smbus.SMBus(1)
IOError: [Errno 13] Permission denied

这是我的 PHP 文件:

<?php 

$cmd = escapeshellcmd("python /var/www/html/combined.py");
$out = shell_exec($cmd);

echo $out;

组合.py:

#!/usr/bin/env python3

import time
import ST7735
from subprocess import Popen, PIPE
try:
    # Transitional fix for breaking change in LTR559
    from ltr559 import LTR559
    ltr559 = LTR559()
except ImportError:
    import ltr559

from bme280 import BME280
from pms5003 import PMS5003, ReadTimeoutError as pmsReadTimeoutError, SerialTimeoutError
from enviroplus import gas

# BME280 temperature/pressure/humidity sensor
bme280 = BME280()

# PMS5003 particulate sensor
pms5003 = PMS5003()
time.sleep(1.0)

# Create a values dict to store the data
variables = ["temperature",
             "pressure",
             "humidity",
             "light",
             "oxidised",
             "reduced",
             "nh3",
             "pm1",
             "pm25",
             "pm10"]

units = ["C",
         "hPa",
         "%",
         "Lux",
         "kO",
         "kO",
         "kO",
         "ug/m3",
         "ug/m3",
         "ug/m3"]

values = {}


# Get the temperature of the CPU for compensation
def get_cpu_temperature():
    process = Popen(['vcgencmd', 'measure_temp'],
                    stdout=PIPE, universal_newlines=True)
    output, _error = process.communicate()
    return float(output[output.index('=') + 1:output.rindex("'")])


def main():
    # Tuning factor for compensation. Decrease this number to adjust the
    # temperature down, and increase to adjust up
    factor = 0.75

    cpu_temps = [get_cpu_temperature()] * 5

    mode = 10    # The starting mode

    # The main loop
    if mode == 10:
        cpu_temp = get_cpu_temperature()
        # Smooth out with some averaging to decrease jitter
        cpu_temps = cpu_temps[1:] + [cpu_temp]
        avg_cpu_temp = sum(cpu_temps) / float(len(cpu_temps))
        raw_temp = bme280.get_temperature()
        raw_data = raw_temp - ((avg_cpu_temp - raw_temp) / factor)
        print("Temp: " + str(raw_data))
        raw_data = bme280.get_pressure()
        print("Pressure: " + str(raw_data))
        raw_data = bme280.get_humidity()
        print("Humidity: " + str(raw_data * 10))
        gas_data = gas.read_all()
        print("Oxidising: " + str(gas_data.oxidising / 1000))
        print("Reducing: " + str(gas_data.reducing / 1000))
        print("NH3: " + str(gas_data.nh3 / 1000))
        lux = ltr559.get_lux()
        print("Lux: " + str(lux))
        prox = ltr559.get_proximity()
        print("Prox: " + str(prox))
        pms_data = None
        try:
            pms_data = pms5003.read()
        except (SerialTimeoutError, pmsReadTimeoutError):
            print("Failed to read PMS5003")
        else:
            print("PMS 1.0" + str(float(pms_data.pm_ug_per_m3(1.0))))
            print("PMS 2.5" + str(float(pms_data.pm_ug_per_m3(2.5))))
            print("PMS 10" + str(float(pms_data.pm_ug_per_m3(10))))
        print("=============================")


if __name__ == "__main__":
    main()

combine.py 由 root 拥有,所有用户都具有写入和执行权限。我尝试将目录 /var/www/html 的所有者更改为 www-data 但它仍然无法正常工作。

我想使用 PHP 执行脚本并查看结果而不是错误。

我认为这是服务器权限问题。

标签: pythonphppermission-denied

解决方案


推荐阅读