Can I reload asterisk through php script?


I want to write a php script that can configure asterisk. For example, to add SIP user, to configure extensions.conf. But when sip.conf or extensions.conf is changed, the asterisk have to be reloaded. How can I reload asterisk service in php script?


just exec(’/path/to/asterisk -rx “sip reload”’);
or chance sip reload to what ever command you would type on the console

my php script prompt “Unable to connect to remote asterisk”. Why?

is asterisk running and if it is are you running the script as the same user that is running asterisk this is a must

Yes, my asterisk is runing. But how to check if my asterisk and php script runing as the same user.

ps auxw should show you want user asterisk is running as… most likely its root (which is the default)

Flowing is the regarding part of ps auxw:

root 16734 1.2 3.7 45760 4388 pts/1 S Apr04 12:25 /usr/sbin/asterisk -v
nobody 18463 0.0 3.1 10200 3748 ? S 10:15 0:00 /usr/local/apache/bin/httpd -k start

Do it mean I have to chang httpd from nobody to root for support php running as same user of asterisk? Could you please tell me how to let php script run as same user of asterisk? Thanks!!!

no I am not saying to have to make http run as root… assuming HTTP is what is going to be firing the script, you could set up asterisk to run as the same user as httpd… or you suexec the script… you need to make the decision based on your security model tho

How to setup asterisk run as other user? And if i change runing user of asterisk as nobody, will it infect other service of asterisk? … k+non-root

you should start here… this wiki also has a ton of information…

I have recompiled the asterisk and the asterisk can be reloaded through php script. But I find my PRI can not start up normally. How to solve it?

Generally * runs as root. This is good and intended. You’ll have some troubles running as another user, and really should know what you are doing if you choose to do that.

On the other hand, PHP is run as the User the WebServer is running as. Typically apache or nobody.

That presents a problem for your reload wish. But not one that can’t be solved easily.

  1. Download phpagi (Just google it)
  2. Use it to connect to the Manager interface
  3. Use it to issue:
    Action: Command
    Command: reload

phpagi actually has a method in the manager class to do this. Just use the “Command” function with a parameter of “reload”

This way you dont’t have to change any users. And you are mor secure, since you can restrict the manager interface to only listen on localhost.

Greetings, Philipp

This is provided fom one Issue up, courtesy of MuppetMaster:

[quote=“MuppetMaster”]How to integrate to Asterisk is a frequent question on this forum. Asterisk has multiple interfaces for integration that allow for a myriad of third party applications to be built leveraging Asterisk.


The first interface is the Application Gateway Interface (AGI), which allows for an external application to be called to provide access to external data/systems and external dialplan logic on calls (incoming or outgoing). The AGI aware application may be written in virtually any language (Perl, PHP, C/C++, Java, Pascal, Bourne Shell, etc) and access almost any database (ODBC, MySQL, Postgres, Oracle, etc). Examples of using the AGI may be to trigger the opening of a new case for a helpdesk automatically when a call arrives, to providing Least Cost Routing (LCR) capabilities from a custom database application, to integrating Text To Speech (TTS) applications.

Those familiar with an Avaya Communication Manager will know this type of functionality as a call to an adjunct link from a vector/VDN, while those familiar with a Nortel Meridian background will know this as the ability to call a host from a CDN. A key difference is that the Asterisk AGI allows for much greater control of the channel (ie - playback messages, capture digits, etc) than an Avaya or Nortel does.

The cousin of the AGI is the FastAGI, which allows one to call a remote AGI aware application via a TCP socket connection. Allowing the developer to offload the AGI resource load onto another server.

[size=134]Manager API[/size]

The Asterisk Manager API is an interface that allows for third-party call control. This may be thought of as a Computer Telephony Integration (CTI) interface such as CSTA/TSAPI. This interface allows for a third-party application to connect via a telnet/TCP interface to do such actions as originate calls, call and extension event monitoring, call recording/monitoring, etc. Examples of uses for this interface would be autodialing (in lieu of using call files), call recording applications, inbound call screen-pops to desktop applications, etc.

[size=134]Opensource Libraries[/size]

While connecting to an AGI/Manager API interface is straightforward, there are several libraries available to do the dirty work for you. These include:

[size=134]Example Application[/size]

Here is an example of an application that was developed using Asterisk and Asterisk integration capabilities:

Just one example of the powerful web services that may be built leveraging Asterisk and its various integration interfaces.[/quote]

Asterisk does not have to run as root… and in a production environment shoud not nbe run as as root IMHO its is not good…

check the permissions on your stuff in /dev/zap the non-root user just cant get read/write access to it… in your zap source there is a document on using udev and having it set the permissions for a non roto user like an asterisk user or the http/apache user to have r/w access to it

I have configure the manager.conf file and edit a ManagerTest.php file as flow:

ManagerTest.php as follow:

<?php $socket = fsockopen("","5038", $errno, $errstr, $timeout); fputs($socket, "Action:Login\r\n"); fputs($socket, "UserName:test\r\n"); fputs($socket, "Secret:test\r\n\r\n"); fputs($socket, "Action:reload\r\n"); fclose($socket); ?>

manager.conf as follow:

enabled = yes
port = 5038
;bindaddr =
bindaddr =
;displayconnects = yes


But when i call the ManagerTest.php file in IE. The asterisk still can not be reloaded. Asterisk and Apache are installed in the same host.

<?php $socket = fsockopen("","5038", $errno, $errstr, $timeout); fputs($socket, "Action:Login\r\n"); fputs($socket, "UserName:test\r\n"); fputs($socket, "Secret:test\r\n"); fpusts($socket "\r\n"); fputs($socket, "Action: Command\r\n"); fputs($socket, "Command:Reload\r\n"); fpusts($socket "\r\n"); fclose($socket); ?>

Should be much more like it. Also the manager will answer you. Maybe you should check that answer?

Well, any case…

Sorry, how to check the answer in aststerisk system?


I had installed Asterisk in a remote server. I need to fetch some data from the Asterisk server using PHP. So i thought to go for PHPAGI.

But how can i connect remote asterisk from my local PHP script using this PHPAGI or the common AGI . Is there any other better way to get it connected.

Please suggest !!!

You want to use the Manager API as outlined in a post in this thread. Use the wiki for many mpre examples on how to do this.