Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run PHP script in background on Apache start/restart(Windows Server)

I've installed Apache 2.4 with PHP 5.4 on Windows Server 2008 following instructions from this manual: Apache installing manual. Apache runs as a service now.

My application requires a php websocket script to run in the background. I'm running it manually with:

php myscript.php

The question is: Is there a way to start a background script automatically on system(apache) restart?

I found the following topic, but I didn't know where I could find an apache startup script for Windows.

Any help will be much appriciated.

like image 556
gxx Avatar asked Feb 05 '13 23:02

gxx


1 Answers

I come up with a solution :)

  • Create an environment variable pointing to your Apache directory
    APACHE_HOME = C:/PATH/TO_APACHE
    

  • Rename %APACHE_HOME%\bin\httpd.exe to %APACHE_HOME%\bin\httpdVendor.exe
  • Create a batch file and put the following code :
    php myscript.php
    %APACHE_HOME%\bin\httpdVendor.exe -k runservice
    exit 0
    

  • Download/Install the free software BatToExeConverter (next, next, ...)
  • Open the installed converter and open your freshly created batch file
  • Click on the button Build EXE (let the default configuration)
  • Save the file : %APACHE_HOME%\bin\httpd.exe
  • Start your Apache Server
  • Tested on : Windows 7, Apache 2.4, Advanced Bat to Exe Converter 2.92

    like image 189
    Halayem Anis Avatar answered Sep 19 '22 12:09

    Halayem Anis