PHP exec() with Iworx chroot

What I’m trying to do is create a PHP script for managing a BF2 (battlefield 2) server config. I created a user (using useradd / passwd, no siteworx account) for running this server called bf2server and it has a home directory of /home/bf2server. I am able from my main website on the server (a siteworx account, which of course is under a different home directory) to access the /home/bf2server folder and edit the config files. I can do this b/c I didn’t set a open_basedir for this account and also b/c web stuff runs as apache, so you just have to give apache rights to the files, a bit unsecure yes, but it works (still waiting for the MPMper_child mod).

But I also want to be able to restart the bf2server via PHP. I thought I could go ahead and use the exec() command to do this. I was thinking something like this:

exec(“pkill bf2”);
exec(“cd /home/bf2server/bf2; ./bf2”);

I have to do the cd part first because the start script use pwd as part of the start stuff.

The above doesn’t work via web browser, but if I got to command line and su – apache (I enabled shell temporarily to test) and do php bf2cp.php, it starts the bf2 server.

So I tried the following:

 /* Add redirection so we can get stderr. */
$handle = popen('cd /home/bf2server/bf2/; ./bf2 2>&1', 'r');
echo "'$handle'; " . gettype($handle) . "
";
$read = fread($handle, 2096);
echo $read;
pclose($handle);
exit;

From shell the above works fine, but the output from the web was:
‘Resource id #2’; resource Error opening terminal: unknown.

Below are the start scripts, the bf2 script just calls the default start script, but runs the program as a background process.

 nohup /home/bf2server/bf2/start.sh > /home/bf2server/bf2/log/bf2.log &
 #! /bin/sh

MACH=""

case `uname -m` in
        i[3456]86) MACH="ia-32"
        ;;
        x86_64) MACH="amd-64"
        ;;
esac

if ! [ -d pb ]
then
        ln -s pb_$MACH pb
fi

BINARY_DIR="$(pwd)/bin"
if [ -d "$BINARY_DIR/$MACH" ]
then
        BINARY_DIR="$BINARY_DIR/$MACH"
fi

# Make the OS give us .core-dumps if the server crashes
#ulimit -c unlimited

export LD_LIBRARY_PATH=$BINARY_DIR
exec $BINARY_DIR/bf2 "$@"

I’m not sure if this somehow has to do with Chroot or if I’m missing something, but its weird that I can run it from command line, but not via the web page.

Thanks for any advice you can give!