PHP is a widely used server-side scripting language favored by many developers for its flexibility and powerful features. Ensuring that PHP applications run correctly in an IIS (Internet Information Services) environment is crucial for website performance and stability. This article explains how to install and configure PHP on IIS, along with practical testing techniques.
First, make sure IIS is enabled on your Windows system. You can add the IIS service via "Turn Windows features on or off" in the Control Panel under "Programs and Features."
Download the thread-safe version of PHP suitable for your operating system from the official PHP website. After extracting PHP to your chosen directory, add the PHP handler in IIS Manager under "Handler Mappings" to integrate PHP with IIS.
The phpinfo() function quickly displays PHP configuration information to verify PHP is working properly. Create a file named info.php in your web root directory with the following content:
<?php
phpinfo();
?>
Then access the file via a browser (e.g., http://your-server/info.php) to view detailed PHP environment information.
Enable error logging in the php.ini file to capture and diagnose runtime errors. Key configuration examples are:
log_errors = On
error_log = "C:\path\to\your\php-error.log"
display_errors = Off
After saving, restart IIS to apply changes. Test logging by creating a PHP script with an error and checking the log file.
Testing the database connection is an important part of PHP application verification. The following example uses the MySQLi extension to test the connection:
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$conn = new mysqli($servername, $username, $password);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connection successful";
$conn->close();
?>
Save and access this script via browser to verify database connectivity.
Installing and testing PHP on IIS is essential to ensure web application stability. By setting up a phpinfo page, enabling error logging, and creating functional test scripts, you can effectively monitor and optimize PHP’s operation. It is recommended to keep PHP updated regularly and review error logs to improve security and reliability.