Why PHP exec() Fails to Run Python Scripts from /usr/lib/cgi-bin
You have a Raspberry Pi where terminal commands run perfectly, yet executing the same Python script via PHP fails. The discrepancy isn't magic; it's how operating systems handle environments differently for interactive shells versus web processes. When you type sudo python script.py in your terminal, you are working inside an interactive Bash session with full access to your user's environment variables, PATH settings, and configuration files like .bashrc.
When PHP runs, however, it operates as a non-interactive service process. By default, the web server (Apache or Nginx) spawns PHP-FPM workers that do not inherit the complex environment of your logged-in terminal session. The exec() function in PHP does execute commands, but it often inherits a stripped-down environment where critical paths, permissions, or required libraries are missing.
Environment Variables and PATH
The most common culprit is the PATH variable. Your terminal knows exactly where to find the Python interpreter because your shell configuration adds system directories to the search path. When PHP tries to execute python, it might be looking in a directory that isn't listed, or it might resolve to a different version of Python installed elsewhere on the system.
Running the command directly works because you explicitly used sudo, which likely invoked a full environment lookup. Inside PHP, without a specific path, the execution context is limited. You need to ensure the command line passed to exec() includes the absolute path to the Python interpreter and the script file.
Instead of relying on generic names, update your PHP code to specify the full paths:
<?php
// Define the absolute path to the python executable
$pythonPath = '/usr/bin/python3';
// Define the path to your script relative to the current execution context
$scriptPath = '/usr/lib/cgi-bin/script.py';
// Combine them into a single command string
// Note: Avoid using 'cd' inside exec() for subprocesses unless necessary
$command = "$pythonPath $scriptPath";
// Execute the command
exec($command, $output, $return_var);
if ($return_var === 0) {
echo "Script executed successfully.";
} else {
echo "Error executing script: " . implode("\n", $output);
}
?>
Handling Permissions and sudo
The second major hurdle is permission escalation. Web servers run as specific system users, typically www-data on Ubuntu-based systems like Raspberry Pi OS. Even if you use sudo in your terminal, the PHP process cannot simply assume the privileges of your logged-in user. Running sudo python script.py directly from PHP will likely result in "Permission denied" errors because the web server user does not have a password configured for sudo access.
To resolve this without configuring complex sudo passwords for the web user, you should ensure the script and directory permissions are set correctly so the web server can run them as itself. If script.py requires root privileges to function (e.g., writing to /dev or modifying system configs), you must configure a sudoers file entry that allows the www-data user to run that specific command without a password.
Edit /etc/sudoers.d/web_scripts and add:
www-data ALL=(ALL) NOPASSWD: /usr/bin/python3 /usr/lib/cgi-bin/script.py
Then, ensure the script itself is readable by the web server:
chmod 755 /usr/lib/cgi-bin/script.py
chown www-data:www-data /usr/lib/cgi-bin/script.py
Avoiding Directory Changes in exec()
Your original code included exec('cd /usr/lib/cgi-bin');. This is a significant architectural flaw. The PHP command interpreter does not maintain a persistent filesystem state between commands executed via exec(), system(), or shell_exec(). Running cd changes the directory for the current shell process, but since PHP executes these as separate subprocesses, the subsequent python command runs in the default working directory (usually /var/www/html) unless you provide an absolute path.
Relying on relative paths inside your script or changing directories before execution leads to unpredictable behavior. Always construct commands using absolute paths for both the interpreter and the target file. This practice ensures portability across different server configurations and prevents issues when the web root location changes.
Conclusion
The difference between terminal success and PHP failure stems from environment isolation and permission models. By switching to absolute paths for your Python interpreter and script, and by configuring the web server user to have the necessary permissions without relying on interactive sudo, you can achieve reliable execution of system scripts from your web interface.