You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PHP exec()权限问题:无法调用Conda执行TensorFlow图像处理任务

问题

我正尝试在PHP中使用exec()函数调用Shell脚本,实现用户上传图片后在网页端执行TensorFlow图像处理。Shell脚本负责激活Conda环境并运行TensorFlow处理的Python文件,但执行Conda命令时出现权限错误:PermissionError: [Errno 13] Permission denied: '/root/.config/conda/.condarc'。已确认PHP运行用户为daemon,尝试过修改Miniconda目录权限、添加环境变量、将www-data加入sudoers等操作,均未解决。

相关代码

upload.php

<?php
$targetDir = "/home/vto/HumanParse/datasets/images/"; // Specify the location
if (!file_exists($targetDir)) {
    mkdir($targetDir, 0777, true); // Create directories recursively if necessary
}
$targetFile = $targetDir . basename(@$_FILES["fileToUpload"]["name"]);
print_r($_FILES); // Output the contents of the $_FILES array to check if the file upload information is correctly passed

// Check if the file already exists
if (file_exists($targetFile)) {
    echo "Sorry, the file already exists.\n";
    //exit;
}
echo "Target file path: " . $targetFile . "\n";
// Upload file
if (move_uploaded_file(@$_FILES["fileToUpload"]["tmp_name"], $targetFile)) {
    echo "File uploaded successfully.\n";
    $output = null;
    $retval = null;
    exec('bash script.sh', $output, $retval);
} else {
    echo "Error uploading file.\n";
}
?>

script.sh

#!/bin/bash

# Specify the path to the log file
LOG_FILE="/home/vto/logfile.log"

# Redirect standard output and standard error to the log file
exec > >(tee -a "$LOG_FILE") 2>&1

# Change directory
cd '/home/vto/HumanParse'

# Activate the virtual environment
source /home/vto/miniconda3/bin/activate tf

# Display virtual environment information to the log file
conda info >> "$LOG_FILE"

# Execute the Python script
python inference_pgn.py

错误报告

# >>>>>>>>>>>>>>>>>>>>>>>> ERROR REPORT <<<<<<<<<<<<<<<<<<<<<<

    Traceback (most recent call last):
      File "/home/vto/miniconda3/lib/python3.8/site-packages/conda/exception_handler.py", line 17, in __call__
        return func(*args, **kwargs)
      File "/home/vto/miniconda3/lib/python3.8/site-packages/conda/cli/main.py", line 97, in main_sourced
        context.__init__()
      File "/home/vto/miniconda3/lib/python3.8/site-packages/conda/base/context.py", line 472, in __init__
        self._set_search_path(
      File "/home/vto/miniconda3/lib/python3.8/site-packages/conda/common/configuration.py", line 1416, in _set_search_path
        self._search_path = IndexedSet(self._expand_search_path(search_path, **kwargs))
      File "/home/vto/miniconda3/lib/python3.8/site-packages/boltons/setutils.py", line 125, in __init__
        self.update(other)
      File "/home/vto/miniconda3/lib/python3.8/site-packages/boltons/setutils.py", line 355, in update
        for o in other:
      File "/home/vto/miniconda3/lib/python3.8/site-packages/conda/common/configuration.py", line 1389, in _expand_search_path
        if path.is_file() and (
      File "/home/vto/miniconda3/lib/python3.8/pathlib.py", line 1439, in is_file
        return S_ISREG(self.stat().st_mode)
      File "/home/vto/miniconda3/lib/python3.8/pathlib.py", line 1198, in stat
        return self._accessor.stat(self)
    PermissionError: [Errno 13] Permission denied: '/root/.config/conda/.condarc'

`$ /home/vto/miniconda3/bin/conda shell.posix activate tf`

解决方案

1. 指定Conda配置路径,避开root目录

Conda默认会读取/root/.config/conda/.condarc,但daemon用户无权访问。先创建daemon用户的Conda配置目录:

sudo -u daemon mkdir -p /home/daemon/.config/conda
sudo -u daemon touch /home/daemon/.config/conda/.condarc

修改script.sh,在激活环境前添加环境变量,指定daemon的配置文件:

export CONDA_CONFIG_FILE="/home/daemon/.config/conda/.condarc"
source /home/vto/miniconda3/bin/activate tf

2. 直接调用环境内的Python解释器,跳过Conda激活

无需激活环境,直接使用tf环境的Python路径运行脚本:

# 替换原有的conda激活和python执行命令
/home/vto/miniconda3/envs/tf/bin/python inference_pgn.py

3. 确保daemon用户加载完整环境配置

exec()调用的Shell是非交互式的,不会自动加载~/.bashrc。修改script.sh的开头,让bash读取登录shell配置:

#!/bin/bash -l

或手动加载配置:

source ~/.bashrc

4. 修复目录权限

给daemon用户添加Miniconda和项目目录的访问权限:

sudo chown -R daemon:daemon /home/vto/miniconda3
sudo chmod -R 755 /home/vto/miniconda3

sudo chown -R daemon:daemon /home/vto/HumanParse
sudo chmod -R 755 /home/vto/HumanParse

内容的提问来源于stack exchange,提问作者李姿儀

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.28 08:31:13