How can PHP developers ensure proper permissions and security when executing shell commands from a web server?

When executing shell commands from a web server, PHP developers should ensure proper permissions and security to prevent unauthorized access and potential security risks. One way to do this is by using the `escapeshellcmd()` and `escapeshellarg()` functions to sanitize user input and prevent command injection attacks.

// Sanitize user input before executing shell command
$command = 'ls ' . escapeshellarg($_POST['directory']);
$output = shell_exec(escapeshellcmd($command));

// Check if the command executed successfully
if ($output !== null) {
    echo "Command output: $output";
} else {
    echo "Error executing command.";
}