Php as windows service

Установка в системах Windows

Содержание

Установка PHP в современных операционных системах Microsoft Windows и рекомендуемая конфигурация под распространённые веб-серверы.

Если вы ищете информацию о старых версиях операционных систем: Windows XP, 2003, 98 или Apache 1.x, обратитесь к разделу руководства Установка на старых версиях ОС Windows.

Официальные релизы PHP для Windows рекомендованы для использования в промышленной эксплуатации. Однако, вы также можете собрать PHP из исходных кодов. Вам потребуется окружение Visual Studio. Обратитесь к разделу » Пошаговое руководство по сборке для получения более полной информации.

Установка PHP на Azure App Services (он же Microsoft Azure, Windows Azure, или (Windows) Azure Web Apps).

User Contributed Notes 12 notes

If you make changes to your PHP.ini file, consider the following.

(I’m running IIS5 on W2K server. I don’t know about 2K3)

PHP will not «take» the changes until the webserver is restarted, and that doesn’t mean through the MMC. Usually folks just reboot. But you can also use the following commands, for a much faster «turnaround». At a command line prompt, type:

and that will stop the webserver service. Then type:

net start w3svc

and that will start the webserver service again. MUCH faster than a reboot, and you can check your changes faster as a result with the old:

in your page somewhere.

I wish I could remember where I read this tip; it isn’t anything I came up with.

You can have multiple versions of PHP running on the same Apache server. I have seen many different solutions pointing at achieving this, but most of them required installing additional instances of Apache, redirecting ports/hosts, etc., which was not satisfying for me.
Finally, I have come up with the simplest solution I’ve seen so far, limited to reconfiguring Apache’s httpd.conf.

My goal is to have PHP5 as the default scripting language for .php files in my DocumentRoot (which is in my case d:/htdocs), and PHP4 for specified DocumentRoot subdirectories.

Here it is (Apache’s httpd.conf contents):

—————————
# replace with your PHP4 directory
ScriptAlias /php4/ «c:/usr/php4/»
# replace with your PHP5 directory
ScriptAlias /php5/ «c:/usr/php5/»

AddType application/x-httpd-php .php
Action application/x-httpd-php «/php5/php-cgi.exe»

# populate this for every directory with PHP4 code

Action application/x-httpd-php «/php4/php.exe»
# directory where your PHP4 php.ini file is located at
SetEnv PHPRC «c:/usr/php4»

# remember to put this section below the above

# directory where your PHP5 php.ini file is located at
SetEnv PHPRC «c:/usr/php5»

—————————

This solution is not limited to having only two parallel versions of PHP. You can play with httpd.conf contents to have as many PHP versions configured as you want.
You can also use multiple php.ini configuration files for the same PHP version (but for different DocumentRoot subfolders), which might be useful in some cases.

Remember to put your php.ini files in directories specified in lines «SetEnv PHPRC. «, and make sure that there’s no php.ini files in other directories (such as c:\windows in Windows).

And finally, as you can see, I run PHP in CGI mode. This has its advantages and limitations. If you have to run PHP as Apache module, then. sorry — you have to use other solution (the best advice as always is: Google it!).

Hope this helps someone.

Читайте также:  Как посмотреть модель видеокарты windows 10

If you are installing PHP on Vista just go to David Wang’s blog. http://blogs.msdn.com/david.wang/
archive/2006/06/21/HOWTO-Install-and-Run-PHP-on-IIS7-Part-2.aspx

I made the mistake of setting a ‘wildcard application map’ for PHP on a Windows 2003 / IIS 6.0 / PHP ISAPI installation.

This resulted in «No input file specified» errors whenever I tried to load the default page in my site’s directories. I don’t know why this broke things, but it did.

If anyone has the same problem, this may be the cause.

IIS setup: 403 forbidden error.

We had installed two separate different PHP versions — PHP 5.1.4 followed by 5.2.5.

We configured 5.2.5 php5isapi.dll to be loaded as the .php file type extension.

Despite this, php version 5.1.4 was being loaded. We renamed 5.1.4’s folder and then PHP was not loading at all.

There were no visible references to 5.1.4 in the IIS configuration, but in the file \webConfig.xml, there was a reference to 5.1.4’s isapi under IISFilters.

To fix this problem, we added version 5.2.5’s php5isapi.dll to the ISAPI Filter category for the web site, in the IIS control panel.

I installed by Microsoft Installer, manually, whatever I always received de same error from IIS7.

HTTP Error 404.3 — Not Found
The page you are requesting cannot be served because of the extension configuration. If the page is a script, add a handler. If the file should be downloaded, add a MIME map.

The IIS7 interface is quite diferent and are not all together like IIS6

The 5.3 version have not any of those files: php5stdll, php5isapi.dll. etc.

The installer puts others files in handlers and I decided to use them as substitutes. Nothing done!

After that, I discovered that installer do not install these files within the sites, but in the root default site configuration of IIS7.

So, I copied the root configuration to my site and them it worked (all others procedures were done e.g. copy php.ini to windows folder)

If you get 404 page not found on Windows/IIS5, have a look at C:\SYSTEM32\INETSRV\URLSCAN

There is a .ini file there that prevents some files from being served by IIS, even if they exist, instead IIS will give a 404. The urlscan logfile (same place) should give you some insight into what parameter is preventing a page from loading, if any.

Here’s how to run dual PHP instances with PHP 5.2 and any previous PHP on Windows 2003:

1. Right-click My Computer, go to Advanced tab, and click on Environment Variables.

Add the two installations and their EXT directories to the Path variable. For example, add:
c:\php;c:\php\ext;c:\TMAS\php;c:\tmas\php\ext;

Then, add the newer PHP version’s directory as a variable called PHPRC. For example:
Variable:PHPRC
Value: C:\PHP

Click OK to close the Environment Variables window, and click OK to close System Properties.

2. In registry, under HKEY_LOCAL_MACHINE>SOFTWARE>PHP, add a REG_SZ key called iniFilePath and give it a value
of the directory where the older PHP is installed. For example:
C:\TMAS\PHP

3. In IIS, go to the Web Service Extensions. Add both versions’ ISAPI module separately to the extensions
list, and allow both.

4. In IIS, go to each website utilizing the PHP versions. Set an ISAPI filter if needed. On the Home Directory
tab, click Configuration, and add .php, .php3, .phtml, and any other extensions needed (perhaps .html?) to
be filtered through PHP, and specify the ISAPI module version needed for each website.

You can now run two versions of PHP. This is because the order of where to look for the .ini file changed
between previous PHP versions and PHP 5.2, as documented at http://us2.php.net/ini:

Читайте также:  Не могу загрузиться с диска windows

* SAPI module specific location (PHPIniDir directive in Apache 2, -c command line option in CGI and CLI, php_ini parameter in NSAPI, PHP_INI_PATH environment variable in THTTPD)
* The PHPRC environment variable. Before PHP 5.2.0 this was checked after the registry key mentioned below.
* HKEY_LOCAL_MACHINE\SOFTWARE\PHP\IniFilePath (Windows Registry location)
* Current working directory (for CLI)
* The web server’s directory (for SAPI modules), or directory of PHP (otherwise in Windows)
* Windows directory (C:\windows or C:\winnt) (for Windows), or —with-config-file-path compile time option
—————————————————-

PHP 5.2.9.2 Install on XP Pro IIS 5.1 — phpinfo( ) results incorrect

Testing Date: 05.15.09

Background:
For several days now I, as a newbie, have been unsure if I had installed PHP correctly, or not. No matter what I did phpinfo( ) reported «Configuratin File Path» as: “C:\WINDOWS”. I was left to wonder what was wrong.

To help resolve the phpinfo() “issue”, I conducted a series of tests using two scripts:

The first is “test-php-ini-loaded.php”; it is stored in c:\inetpub\wwwroot, and has the following code:

if ( $inipath ) <
echo ‘Loaded php.ini: ‘ . $inipath ;
> else <
echo ‘A php.ini file is not loaded’ ;
>
?>

The second script is simply calls phpinfo( ). It is named test.php, is stored in “c:\inetpub\wwroot”, and has the following code:

( ); ?>

My Dev Environment:
1. Windows XP Pro SP3
2. IIS 5.1 / MMC 3.0
3. PHP 5.2.9.2 – phpMyAdmin not yet installed
4. (plus MySQL 5.1, etc.)
5. Install location is on my local E: drive

How to Run any PHP Script as a Windows Service with AlwaysUp

Start your PHP script in the background when your PC boots. Keep it running 24/7 or schedule it to restart several times a day

PHP is a popular programming language. It is often integrated with web servers but works just as well when creating standalone applications.

To set up a PHP script to run as a background windows service with AlwaysUp:

Download and install AlwaysUp, if necessary.

Download and manually install the PHP package, if necessary.

We have installed PHP in C:\PHP and we’ll reference that location throughout this tutorial.

Next, let’s ensure that your PHP script can run normally from the command line.

Start a command prompt (Start button > Run > cmd.exe)

In the window that appears, type the full path to the PHP executable (php.exe) followed by the full path to the script you wish to run as a windows service. Be sure to quote each component containing at least one space!

Since we have placed PHP in C:\PHP, and our script is C:\PHP\sample-php-script.php, our command line is:

Hit the Enter key to execute the command line. If your script doesn’t work as expected please resolve the issues now. For example, if you get the MSVCR110.dll is missing error, please install the Visual C++ Redistributable package for Visual Studio 2012.

Note that if your script doesn’t work here then it won’t work with AlwaysUp!

Here is the code of our simple script:

And here is the result from running it on the command line:

Everything looks good!

Next, start AlwaysUp.

Select Application > Add to open the Add Application window:

On the General tab:

In the Application field, enter the full path to the PHP executable (php.exe). Again, this is C:\PHP\php.exe for our installation.

In the Arguments field, enter the full path to your PHP script. We want to run our sample script, C:\PHP\sample-php-script.php.

In the Name field, enter the name that you will call this application in AlwaysUp. We have used My PHP Script but you can specify virtually anything you like.

Читайте также:  Как запустить приложение от имени другого пользователя linux

If you only want your script to run periodically (that is, not 24×7), click over to the Restart tab and choose when to run. For example, if you wish to kick off the script at 6 AM every day, check the Not immediately and At boxes and enter that time in the corresponding field. However we’ll stick with the default of restarting immediately to keep our script running continuously.

Click the Save button. In a couple of seconds, an application called My PHP Script will show up in the AlwaysUp window. It is not yet running though.

To start the service, which will kick off the PHP script, choose Application > Start «My PHP Script». In a few seconds, the state should transition to Running:

On Windows 8/7/Vista and Server 2012/2008, your PHP script will be running in the background on the isolated Session 0. Select Tools > Switch to Session 0 to move to Session 0 where you should see a CMD window hosting your script:

That’s it! Next time your computer boots, your PHP service be launched immediately, before anyone logs on. We encourage you to explore the many other AlwaysUp settings that may be appropriate for your environment.

PHP script not working properly as a Windows Service?

  • From AlwaysUp, select Application > Report Activity > Today. to bring up a HTML report detailing the interaction between AlwaysUp and your application. The AlwaysUp Event Log Messages page explains the more obscure messages.
  • Some PHP scripts need to be run in a user account to have access to their settings and resources. In that situation, edit your application in AlwaysUp, switch to the Logon tab and enter the user name and password of the account where you can run your PHP script normally (the one you were logged in to for step 3).
  • Consult the AlwaysUp FAQ for answers to commonly asked questions and troubleshooting tips.
  • Contact us and we will be happy to help!
  • Setting a PHP script as a Windows Service

    I need to set up a PHP script as a windows service.

    I need it to run regardless of which user is logged in, and on system start up — so it sounds like a windows service is best, but am happy to hear other suggestions.

    (This script runs continuously, it’s not a «run every 5 mins» thing I could use the Scheduled Task Manager for.)

    http://support.microsoft.com/kb/251192 covers using the sc.exe program to install your service.

    But from what I’ve read, I need to have a wrapper round the PHP script to accept the special commands from the windows service manager. Can anyone help with this?

    7 Answers 7

    Maybe the Resource Kit Tools (specifically srvany.exe) can help you here. MSDN: How To Create A User-Defined Service and possibly this hint for 2008 Server should help you setup any executable as a service. (I’ve successfully used this on Windows 2003 Server, Windows 2008 Server and on Windows XP Professional [other Resource Kit, though])

    You’d create a bat containing php your-script.php , wrap that with srvany.exe and voila, the script is started once the machine loads the services.

    srvany.exe should handle those start/stop/restart calls you’d expect a daemon to execute. It would load your executable on start, kill the process on stop, do both on restart. So you don’t have to worry about this part. You might want to check if a register_shutdown_function() can help identify when your service process is killed.

    You can even define dependencies to other services (say some database or some such).

    Оцените статью