PHPAGI: Exec format error

AWippler picture AWippler · Mar 30, 2013 · Viewed 11.4k times · Source

Encountering a problem when running phpagi:

-- Executing [123@DLPN_C:1] AGI("SIP/1000-00000001", "hello_world.php") in new stack
-- Launched AGI Script /var/lib/asterisk/agi-bin/hello_world.php
hello_world.php: Failed to execute '/var/lib/asterisk/agi-bin/hello_world.php': Exec format error
-- Auto fallthrough, channel 'SIP/1000-00000001' status is 'UNKNOWN' Scheduling destruction of SIP dialog '343930130' in 32000 ms (Method: INVITE)

From command line:

root@asterisk-test:/var/lib/asterisk/agi-bin# php5 -q hello_world.php 
#!/usr/bin/php5 -q

Additional info:

-rwxr-xr-x  1 root     root       757 Mar 29 19:32 hello_world.php
drwxrwxr-x  4 root     root      4096 Mar 29 19:44 phpagi
-rwxr-xr-x  1 root     root     25079 Sep 30  2010 phpagi-asmanager.php
-rwxr-xr-x  1 root     root      2322 Sep 30  2010 phpagi-fastagi.php
-rwxr-xr-x  1 root     root     67615 Sep 30  2010 phpagi.php

Source of hello world: http://www.eder.us/projects/phpagi/phpagi/api-docs/__examplesource/exsource_home_html_projects_phpagi_phpagi_examples_dtmf.php_acb7257145e4a5249182c8373cd8e848.html

Answer

pce picture pce · Apr 1, 2013

The Exec Format Error is from /bin/bash, asterisk executes hello_world.php as a bash script.

shebang

If you add a correct shebang, the script get executed by the given PHP intepreter. The first Line tells the System which program should run the script.

#!/usr/bin/env php

To test your shebang, execute the script itself, not by PHP:
root@asterisk-test:/var/lib/asterisk/agi-bin# ./hello_world.php

Make sure it is executable with:
root@asterisk-test:/var/lib/asterisk/agi-bin# chmod +x hello_world.php

alternative wrapper

Create a bash script that executes the PHP script.

example hello_world.sh:
/usr/bin/php hello_world.php

and call it in the Dialplan AGI("hello_world.sh").

Make sure the shellscript is executable chmod +x hello_world.sh.