📜 ⬆️ ⬇️

PHP on Windows and IIS7

Many good applications are written in PHP. Not even
very many and some of them are very good, so why not use these
applications on windows? Especially if the internal portal runs on Windows, and
on the Unix machine to spin the company's external website - then
you can save on infrastructure and place on Windows
server is also an external site. Or, if there is a desire to standardize
infrastructure and host sites on Windows
platform as developers and users work on
Windows platform.
On the website www.iis.net
can find
list
popular php applications
with installation instructions on IIS. To launch them
no change required on IIS
Php code

Install and configure PHP for use with the FastCGI module.


For starters, to successfully use PHP on
Windows would be nice php
to install.
Step 1. Download PHP
On the PHP.net site you need to download
The latest version of PHP for Windows . Recommended for use with FastCGI
install PHP version without thread safety control, since the module itself
FastCGI ensures that execution occurs in one thread and support
thread safety control in PHP itself introduces unnecessary checks and
locks resulting in a significant performance drop. therefore
choose non-thread-safe Win32 binaries (version 5.2.6 is relevant at the time of writing
of this message) .
It is worth noting that we believe Non-thread-safe was designed specifically for
work with FastCGI on IIS (the first release was in version 5.2.1) and used in
other environments are not recommended. By the way, since version 5.2.2 Zend is serious
working on optimizing php performance under windows that can not
to please If we compare versions 5.2.1 and 5.2.2, then the difference in processing speed
requests can be easily seen using a simple load test.
Step 2. Install PHP
Installation is quite simple: since we downloaded the archive from
executable files, just deploy this archive, for example, in
directory C: \ Web \ PHP.
As a basic configuration, we use the recommended settings:
Make a copy of the php.ini-recommended file in php.ini in the same directory and open it.
edit it, and then go through the file above, uncommenting the following
lines to ensure security and compatibility with most PHP
applications:

Step 3. Test PHP functionality
While we have not configured IIS, check the functionality of the interpreter.
You can simply, for example, run the command c: \ web \ php \ php.exe -info> c: \ test.txt

Install and configure the FastCGI module on IIS7.


If you have IIS7, then something tells me about the name of your operating system.
Systems - Windows Vista? I did not guess, then Windows Server 2008! Either you are a hacker and
put IIS7 somewhere else, but this is a non-standard solution and we are not
support;).
Step 1. Install FastCGI
I want to please immediately - in IIS7, coming with Windows Server 2008 and Windows Vista
Service Pack 1 FastCGI module is already included. It only needs to be connected to
settings. To do this, on Vista you need to open the Control Panel -> Programs and select
"Turn Windows Features On or Off":
ControlPanel - Programs

After that, you need to install the feature in IIS: Internet Information Services
-> World Wide Web Services -> Application Development Features -> CGI. Wherein
both CGI and FastCGI support will be installed.
IIS_CGI_Feature

On Windows Server 2008, the process is similar: Server Manager -> Roles -> Add
Role Services -> Web Server -> Application Development -> CGI.
Actually all that is required to enable the FastCGI module.
Step 2. IIS7 Configuration
1. Open IIS Manager, select the node (server) for which you want to configure
PHP support. And then choose Handler Mappings.
HandlerMappings
HandlerMappingsOpened
')
2. Select the Add Module Mapping link on the Handler Mappings page and
fill the window with the following values:
Request path: * .php (processing all files with the extension .php)
Module: FastCgiModule (FastCGI module)
Executable: C: \ Web \ PHP \ php-cgi.exe (path to PHP)
Name: PHP (name for convenience)
PHPHandler

After adding this setting, a registration question window will appear.
FastCGI applications for this handler. We confirm.
The above steps led to the creation of the following directory in the PhpSites directory:
web.config file:

<? xml version = "1.0" encoding = "UTF-8" ? >
< configuration >
< system.webServer >
< handlers >
< add name = "PHP" path = "*. php" verb = "*"
modules = "FastCgiModule" scriptProcessor = " C: \ Web \ PHP \ php-cgi.exe "
resourceType = "Unspecified" />
</ handlers >
</ system.webServer >
</ configuration >
* This source code was highlighted with Source Code Highlighter .


Now you can proceed to test the performance of PHP.
Step 3. We check the correctness of the settings
In the node directory for which we have configured PHP create a file
index.php:
  <? <br /> phpinfo (); <br />?> 

And access this file via an HTTP request. As a result, if all is well and
our karma is not corrupted, the request will be processed correctly:
phpinfo

Recommendations for configuring PHP on
IIS7


Of course, when using PHP on
IIS7 can occur pitfalls with which to
fight to achieve the expected result (great job
PHP applications on Windows).
Hammers for breaking common stones are listed below.

PHP restart frequency



Since when using PHP on
IIS7 using FastCGI
module, the FastCGI module itself takes over
managing processes and resources, you need to make sure that the restart mechanism
processes (recycling) in PHP
will not interfere with FastCGI. This is easy to do if
configure fastcgi so that it always restarts
processes earlier than PHP does.
There is a setting in the FastCGI settings
instanceMaxRequests, which determines after processing how many requests,
The process will be restarted. In PHP, a similar parameter
set by the value of the variable PHP_FCGI_MAX_REQUESTS. Obviously to give
FastCGI ability to steer the process, enough
set instanceMaxRequests <= PHP_FCGI_MAX_REQUEST.
This is convenient to do by editing the file.
applicationHost.config (hiding in the directory C: \ windows \ system32 \ inetsrv \ config \).
The configuration should contain the following information:
< fastCgi >
< application fullPath = " C: \ inetpub \ php \ php-cgi.exe "
maxInstances = "4" instanceMaxRequests = "10,000" >
< environmentVariables >
< environmentVariable name = "PHP_FCGI_MAX_REQUESTS" value = "10000" >
</ environmentVariables >
</ application >
</ fastCgi >
* This source code was highlighted with Source Code Highlighter .

Using multiple PHP versions


Since different versions of PHP can be used in
applications that are hosted on the server, it would be good to give
use different versions for different sites.
In the applicationHost.config configuration file
it is enough to define sections for different versions of PHP:
< fastCgi >
< application fullPath = "C: \ inetpub \ php \ php-cgi.exe" >
...
</ application >
< application fullPath = "C: \ inetpub \ php4 \ php4.exe" >
...
</ application >
< application fullPath = "C: \ inetpub \ php41 \ php41.exe" >
...
</ application >
</ fastCgi >
* This source code was highlighted with Source Code Highlighter .
And for each of the sites a module is configured that uses one or another
version (you can use the interface described above, or you can
edit the configuration in the text):
< handlers >
< add name = "PHP4" path = "*. php" verb = "*" modules = "FastCgiModule"
scriptProcessor = " C: \ inetpub \ php \ php41.exe "
resourceType = "Unspecified" />
</ handlers >
* This source code was highlighted with Source Code Highlighter .

Using different sets of PHP settings


If you want to customize PHP differently for
different sites, then again, all this can be described through the configuration settings in
applicationHost.config.
< fastCgi >

< application fullPath = " C: \ inetpub \ php \ php-cgi.exe "

arguments = "- d my.website = wordpress" >

< environmentVariables >

< environmentVariable name = "PHPRC" value = "C: \ inetpub \ wordpress" />

</ environmentVariables >

</ application >

< application fullPath = " C: \ inetpub \ php \ php-cgi.exe "

arguments = "- d my.website = phpsite" >

< environmentVariables >

< environmentVariable name = "PHPRC" value = "C: \ inetpub \ phpsite" />

</ environmentVariables >

</ application >

</ fastCgi >
* This source code was highlighted with Source Code Highlighter .

After that, the settings are associated with the corresponding sites in
web.config:
< system.webServer >

< handlers accessPolicy = "Read, Script" >

< add name = "PHP" path = "*. php" verb = "*" modules = "FastCgiModule"

scriptProcessor = " C: \ inetpub \ php \ php-cgi.exe | -d my.website = wordpress "

resourceType = "Unspecified" requireAccess = "Script" />

</ handlers >

</ system.webServer >
* This source code was highlighted with Source Code Highlighter .

According to the above configuration, php.ini
need to be placed in the directory of each site.
When editing settings, it is worth strictly observing the coincidence of the paths to
appropriate php version and with
applicationHost.config and in web.config to
Avoid unexpected results if the paths are confused.
At first glance, editing the configuration may seem complicated and
inconvenient process, but as soon as you get used to the configuration in
XML and distribution settings method
Ctrl + C, Ctrl + V, you will be surprised by the presence of others
ways to configure :)

PHP security settings


In php.ini there are many different settings, many of which
affect the security of using PHP. Tune
all appropriately, worthy deed.
Set allow_url_fopen = Off
; use URL for file operations
Set allow_url_include = Off
register_globals = Off
; unregistering global variables
open_basedir = "c: \ inetpub \";
limitation on the directory in which PHP works

max_execution_time = 30; restriction
runtime scripts
max_input_time = 60
memory_limit = 16M;
memory size limit
upload_max_filesize = 2M
post_max_size = 8M
max_input_nesting_levels = 64
display_errors = Off
; disabling error messages
log_errors = On
error_log = "C: \ error.log"
expose_php = Off
; hide PHP presence

Conclusion


PHP on Windows is not
just interesting and convenient, the main thing is that it works. And the team
IIS is working to make PHP
worked on windows no worse than
Unix / Linux (of course, try to do better).
Since this is a new theme for Microsoft, we can
to make some mistakes, we may not notice or understand something, therefore we
It is very important to receive comments from you - developers and administrators.
Write in the comments your wishes and problems that you see now in
Php on windows and we will
try to solve problems, and wishes to realize.

Source: https://habr.com/ru/post/30154/


All Articles