is there a way how to easily run a PHP application as from command line on Windows Azure?
I have a standard Web Application (on Azure) and I want to communicate using WebSockets.
So I need to have a WebSocket Server running all the time on Azure.
I use Wrench project which I need to run "all the time" to listen on some port and deal with messages from JavaScript-sended WebSocket.
So again - how easily run a "persistent" PHP application on Azure?
Thank you in advance.
Sandrino's answer is fine, but I prefer ProgramEntryPoint for doing this sort of thing. The trouble with a background task is that (unless you build something on your own) nothing is monitoring it. Using ProgramEntryPoint, Windows Azure will monitor the process, and if it exits for any reason, the role instance will be restarted.
EDIT:
Sandrino points out that the PHP program isn't the only thing running. (There's also a website.) In that case, I'd recommend launching php.exe in Run() in WebRole.cs. Process.Start it and then do a .WaitForExit() on it. That way, if the process exits, the role itself will exit from Run(), causing the role instance to restart. See http://blog.smarx.com/posts/using-other-web-servers-on-windows-azure for an example.
In order to run your PHP script as a command line application you should use the PHP CLI (command line interface).
php.exe -f "yourWebSocketServce.php" -- -arg1 -arg2 -arg3
Now, in order to run this in Windows Azure you'll need to define a startup task that runs this command. You'll see that the default task type is simple, which means that the startup of your role will block until the task finishes. But in your case running the WebSocket in PHP will be a blocking process, that's why you should change the type to background (this will make sure the instance continues starting up while your WebSocket server is running).
Here is a WebSockets service on Azure. - Live XSockets.NET
Have a look at http://live.xsockets.net, an easy way of getting started, but it depends on what you are about to do on the "server side". This service i mention can be uses as a "message" dispatcher, to ntify "clients" on changes etc.. Hmm in other words it is a way of boosting "regular" web-apps..
Related
I want to set up a WebSocket server using PHP. I have many alternatives to do this, yes, but I wanted to ask people who have experienced which one is more reliable (strong, lightweight, and faultless). I also wrote some code, this code creates a socket server, but I'm not sure how to start it, do I need to open the page from the browser?
Usually a web socket server, as many servers, is started to operate as a daemon in the background and listen there for events coming in. On Linux (Ubuntu) you might create a unit file to be consumed by the system command systemctl, this way the daemon will be started with every boot of the system and you can start and stop it as you need to.
Next you can start it form a command shell like
nohup php websocketserver &
This will send your server into the background an stay there until the system is rebooted. Any output is logged to a file named nohup.
On Windows you better create a service, via a schedule task to have your server start at system boot.
I'm running in a controlled, xp, intranet only environment and I need to start external processes from a PHP applications. (Backups, Reports etc.)
I can get system or exec to start processes that work silently. Here's a simple example
<?php exec ("echo hello > hello.txt");?>
I can get it to execute a bat file that has no visible output.
I can't get any program that has a screen to run such as a report generator or notepad...
<?php exec ("explorer");?>
doesn't do anything. or same for system
Very late answer, but I was working on this myself and found that it is indeed possible to run a GUI program from PHP with the Apache server on Windows XP.
Start->Run, type "services.msc" to bring up Services control (other ways to get there, this is easiest IMO)
Locate your Apache service (mine was called "wampapache" using WampServer 2.0)
Open the service properties (double-click or right click->properties)
Flip to the Log On account and ensure the checkbox titled "Allow service to interact with Desktop" is checked
Flip back to the General tab, stop the service, start the service
Now, using the code below, you can spawn UI processes from PHP. In the first code snippet, the script will not wait for the application to close; the second snippet waits for the program to close before continuing (blocking).
Do not wait for application:
pclose(popen("start /B notepad.exe", "r"));
Wait for application:
system('start notepad.exe');
This has been tested on Windows XP. I have not tried it on any other Windows versions, your millage may vary.
Side note
On my particular installation, I was using the other option in the Log In tab of the service - Apache was running as a domain user so it could access several network shares with domain user permissions. The checkbox isn't available for that option, only when the service is running as Local System. After extensive research, I've found that there is simply no way for a single service to both interact with the current desktop AND utilize the credentials of a specific user. It's a one-or-the-other proposition, with the suggested remedy being to split your service into two components - one that uses the user account privs and one that interacts with the desktop. Not very practical when the service you're talking about is the web server. This note is probably pretty specific to my use case, but I wanted to put it out here in case I can save someone else the frustration in the future.
Another super late answer, but this comes up on Google when searching for "php run gui program"...
I have been able to launch a GUI app in Windows 8.1 by making, running and deleting a scheduled task:
shell_exec('SCHTASKS /F /Create /TN _notepad /TR "notepad.exe" /SC DAILY /RU INTERACTIVE');
shell_exec('SCHTASKS /RUN /TN "_notepad"');
shell_exec('SCHTASKS /DELETE /TN "_notepad" /F');
What behavior are you expecting? Calling system('notepad') works fine - it just doesn't display the GUI. It runs in the background, and PHP sits there patiently waiting for notepad to close itself (and only continues if you kill notepad from the process list).
If you're expecting it to pop up a GUI, I'm fairly certain that you can't do that. ;) An option might be to write out a batch script (file_put_contents('runme.bat', 'notepad hello.txt')) and have that batch script queued (with Windows scheduler or whatever the cron-equivalent is on Windows) to run in an async fashion (and clear itself at the end).
I'm planning the development of a server written in PHP that can service socket requests. I use a free host (Heliohost) for testing, and it has cPanel. So far the only thing I've been able to think of to have a PHP script always running is to write a cron job that runs a bash script to check ps to see if the PHP is already running, and if it isn't, start it.
Is there a better way? Perhaps a way for a PHP thread to be started on an HTTP request and continue to run in Apache after the request has been serviced?
You will almost certainly not have success running persistent processes from Apache. It is designed to prevent that scenario (though if you can get to the fork(2) system call, it is probably do-able). I wouldn't recommend trying it though.
What would make more sense is if you use a hosting provider that gives you the ability to write your own crontab(5) specifications and run the PHP interpreter directly. Then you could just add a line to your crontab(5) like:
#reboot /path/to/php /path/to/script.php
Your script should probably perform the usual daemonization tasks so that cron(8) isn't stuck waiting for your process to exit.
I'm running in a controlled, xp, intranet only environment and I need to start external processes from a PHP applications. (Backups, Reports etc.)
I can get system or exec to start processes that work silently. Here's a simple example
<?php exec ("echo hello > hello.txt");?>
I can get it to execute a bat file that has no visible output.
I can't get any program that has a screen to run such as a report generator or notepad...
<?php exec ("explorer");?>
doesn't do anything. or same for system
Very late answer, but I was working on this myself and found that it is indeed possible to run a GUI program from PHP with the Apache server on Windows XP.
Start->Run, type "services.msc" to bring up Services control (other ways to get there, this is easiest IMO)
Locate your Apache service (mine was called "wampapache" using WampServer 2.0)
Open the service properties (double-click or right click->properties)
Flip to the Log On account and ensure the checkbox titled "Allow service to interact with Desktop" is checked
Flip back to the General tab, stop the service, start the service
Now, using the code below, you can spawn UI processes from PHP. In the first code snippet, the script will not wait for the application to close; the second snippet waits for the program to close before continuing (blocking).
Do not wait for application:
pclose(popen("start /B notepad.exe", "r"));
Wait for application:
system('start notepad.exe');
This has been tested on Windows XP. I have not tried it on any other Windows versions, your millage may vary.
Side note
On my particular installation, I was using the other option in the Log In tab of the service - Apache was running as a domain user so it could access several network shares with domain user permissions. The checkbox isn't available for that option, only when the service is running as Local System. After extensive research, I've found that there is simply no way for a single service to both interact with the current desktop AND utilize the credentials of a specific user. It's a one-or-the-other proposition, with the suggested remedy being to split your service into two components - one that uses the user account privs and one that interacts with the desktop. Not very practical when the service you're talking about is the web server. This note is probably pretty specific to my use case, but I wanted to put it out here in case I can save someone else the frustration in the future.
Another super late answer, but this comes up on Google when searching for "php run gui program"...
I have been able to launch a GUI app in Windows 8.1 by making, running and deleting a scheduled task:
shell_exec('SCHTASKS /F /Create /TN _notepad /TR "notepad.exe" /SC DAILY /RU INTERACTIVE');
shell_exec('SCHTASKS /RUN /TN "_notepad"');
shell_exec('SCHTASKS /DELETE /TN "_notepad" /F');
What behavior are you expecting? Calling system('notepad') works fine - it just doesn't display the GUI. It runs in the background, and PHP sits there patiently waiting for notepad to close itself (and only continues if you kill notepad from the process list).
If you're expecting it to pop up a GUI, I'm fairly certain that you can't do that. ;) An option might be to write out a batch script (file_put_contents('runme.bat', 'notepad hello.txt')) and have that batch script queued (with Windows scheduler or whatever the cron-equivalent is on Windows) to run in an async fashion (and clear itself at the end).
I need an application to be running in the background on my web server, but I need to be able to start/stop the application with root privileges.
In order to do this, I want to have a service running that has root privileges, so that it can kill the application, and start it up again if need be.
Finally, I need to be able to send the start and kill commands to the service via Apache/PHP, so that it can be indirectly controlled through the web.
How do I create a Linux service?
How do I communicate with a Linux service in this manner?
Thanks in advance!
Use the exec command in your PHP script to call shell files. The shell files can be setup with the "setuser" bit so it will run as its owner (instead of running with the web server's permissions).
Of course, you'll need to be very careful--lots of testing, monitoring, etc.
Finally, think about the service running as a dedicated user, not as root. Eg like apache and most other well done services do.
Added: Re: running a service in Linux. Depends on your flavor of Linux. If you want to be sure that your app service will be automatically re-started if it fails, plus logging, checkout Runit:
https://web.archive.org/web/1/http://blogs.techrepublic%2ecom%2ecom/opensource/?p=202
http://smarden.org/runit
Added: Instead of setuid bit, I think Frank's suggestion (in comment) of using sudo system is better.
So, you have three pieces here :
Your web server without root privilege
An application
A daemon that is monitoring the application
Your problem is not launching the daemon, it is writing it, and communicating with it from the web server, without needing root privilege.
A daemon can be as simple as a non interactive application launched in the background :
# my_dameon &
I am not a php developper, but searching for message queue and php, I discovered beanstalkd
Looking at the example on the first page it seems you can use it to do the following :
The apache/php sends some message to beanstalkd
Your daemon reads the message from beanstalkd. Based on the command it starts or kill or reload the background application.
You can write your daemon in php, since there are client in many languages
You can also check this question
You can create a daemon which accepts the following commands:
daemon_name start
daemon_name stop
daemon_name restart
deamon_name reload
Starting the daemon should not be hard. Just executing daemon_name start from a PHP script should run it. After starting, you can write the PID of the process to a lock file (for stopping, restarting or reloading later). The daemon should handle signals.
In a PHP script, you can then invoke daemon_name stop. This should fire up a new daemon which would check the lock file and get the PID of the running daemon and send a signal which would be handled by the running daemon. The lock file should be removed/cleared and then the stop initiating daemon/process can quit.
I think you should look at inetd, which can be configured to run all sorts of services, and it runs as root. You could then write a relatively simple program that is not itself root privileged, but which when run by root does the tasks you need done.
As far as communicating with the service you did not say what type of service it is, however assuming you're writing it yourself the most common methods would be to comunicate via UNIX sockets or MMAP. Depends on your needs really.
Oh yeah, should point out there are already applications for web management of linux systems. Webmin is a really good one which can be configured to allow as much or as little control as you need.
As #shodanex suggests, using Beanstalkd would be an excellent way to disconnect a web-front-end from a running-as-root command line worker. It could trivially be set to only run exactly what was required.
To run the worker, Pear's System_Daemon can generate and run a daemon-running script, with start/stop/restart.
When doing this be very very careful. Never use any user submitted data from the web in the exec command. this could allow someone to arbitrarily execute commands on your machine.
Also I second Frank use a sudo rule so you can run that specific command with the permissions you need but nothing else. It will be more secure that way.
Of course with
sudo apt-get install openbsd-inetd
you can create a service you want