How to Execute a Shell Script in PHP
When moving from a terminal environment to a web server, the execution context changes drastically. You ran su www-data && ./myscript.sh successfully because you explicitly switched user contexts and relied on the shell's ability to resolve relative paths within that session. However, translating this directly into PHP with shell_exec('./myscript.sh') fails because the web server process (often www-data) lacks the necessary permissions or environment variables to execute the script as intended.
The core issue isn't just about running a command; it is about how the operating system resolves paths and manages user sessions. When you invoke ./myscript.sh, the shell attempts to find the executable in the current working directory of the PHP process. While this often works for simple scripts, the real blocker in your scenario is almost certainly permissioning. The update where adding www-data ALL=(ALL) NOPASSWD:ALL to /etc/sudoers resolved the issue confirms that the script requires elevated privileges or specific resource access that the standard web user cannot provide.
Relying on a blanket sudo rule for a single script is indeed a security anti-pattern. It effectively grants the www-data user root-level access, bypassing the principle of least privilege. If your script only needs to create folders and run Subversion updates, granting full sudo access opens the door for potential lateral movement if the web application is compromised. A more robust architecture isolates the execution context without elevating the entire user account.
The most secure approach involves creating a dedicated system user or group specifically for this task and using sudo with a restrictive command specification. Instead of allowing all commands, you can define an alias in /etc/sudoers that restricts www-data to only your specific script path. For example:
www-data ALL=(ALL) NOPASSWD:/path/to/myscript.sh
This ensures the web server can execute the file without needing a password, but it cannot run any other arbitrary commands or access unrelated system resources. You must also ensure the script has the executable bit set (chmod +x /var/www/myscript.sh) and that the PHP process can read the file.
Furthermore, you need to address the working directory problem. When shell_exec runs the command, it inherits the current working directory of the PHP script, which is likely your web root (e.g., /var/www/html). If your shell script relies on relative paths like svn update without a full path, it might fail if the SVN repository isn't located in that specific directory or if the user doesn't have permissions to traverse from /var/www/html to /var/www/myscript.sh.
A better implementation pattern is to pass an absolute path to the script and explicitly set the working directory within the PHP call. This removes ambiguity about where the shell starts looking for resources:
$output = shell_exec('sudo /path/to/myscript.sh');
Alternatively, if you prefer not to use sudo at all, you can modify the ownership of the target directories and the script itself to belong to www-data. If the folders being created are in a location writable by the web server and the SVN repository is accessible by that user, the script will execute successfully without needing any special privileges. This eliminates the security risk associated with sudo entirely while maintaining functionality.
Another consideration is error handling. shell_exec returns output as a string, but it does not throw exceptions on failure. To debug issues similar to the ones you encountered, capture both the output and the exit status:
$output = shell_exec('sudo /path/to/myscript.sh 2>&1');
$exit_code = shell_exec('echo $?'); // Note: capturing exit code requires careful parsing or using exec()
// Better approach for checking exit codes
exec('/usr/bin/sudo /path/to/myscript.sh 2>&1', $output, $return_var);
if ($return_var !== 0) {
// Handle error
}
Using exec() with the $return_var variable allows you to inspect whether the script ran successfully before deciding how to proceed. This is critical for production environments where a failed SVN update could leave your codebase in an inconsistent state.
If you are working within a Laravel application, consider utilizing Laravel's built-in tools for running artisan commands or queue jobs if the script logic can be adapted. However, for raw shell scripts, sticking to shell_exec with proper path resolution and restrictive sudo configurations remains the standard best practice. Always verify that the web server user has the necessary read permissions on the script itself before attempting execution.
