How to set up PHP in Windows

Updated: January 9, 2024 By: Guest Contributor Post a comment

Introduction

Setting up a PHP environment on Windows is a foundational step for developing web applications on this OS. This tutorial will guide you through the process, from installation to advanced configurations.

System Requirements

Before installing PHP, ensure your system meets the following requirements:

  • A supported version of Windows (7, 8, 10, or 11)
  • Administrative privileges
  • An internet connection for downloading necessary files

Installing PHP

To install PHP on a Windows environment, you may choose from various methods, but we’ll focus on the manual installation using the official PHP builds for Windows.

Step 1: Download PHP

Go to the official PHP downloads page at windows.php.net/download and download the latest PHP non-thread safe ZIP package for Windows. Ensure the version you download corresponds with your system architecture (x86 for 32-bit and x64 for 64-bit).

Step 2: Extract PHP

Extract the ZIP file to a preferred directory, such as: C:\php

Step 3: Configure php.ini

Rename the php.ini-development to php.ini for a development environment or php.ini-production for a production environment. Edit the php.ini file in a text editor to tailor the PHP environment to your needs.

Step 4: Add PHP to Path

To make PHP globally accessible from the command line, add the path to the PHP directory to the system’s PATH environment variable.

// Open System Properties > Advanced
// Click on Environment Variables...
// In the System variables window, find and select PATH
// Click Edit... and then New
// Add the path to your PHP directory (e.g., C:\php)
// Click OK to apply the changes

Step 5: Verify PHP installation

Open a new command prompt and type php -v to verify that PHP is installed correctly. You should see the PHP version information displayed.

Configuring a Web Server

PHP requires a web server such as Apache or Nginx to process PHP files in a local development environment.

Installing Apache

Download the Apache HTTP Server from the official Apache website or use a distribution like XAMPP, which includes Apache, PHP, and MySQL from apachefriends.org.

// Follow the installation instructions for Apache or XAMPP

Remember to configure Apache to use the PHP module by editing the Apache config file (httpd.conf) and add the following lines:

LoadModule php_module "C:/php/php7apache2_4.dll"
AddType application/x-httpd-php .php
PHPIniDir "C:/php"

Installing Nginx

For those who prefer Nginx, download the server from nginx.org and refer to the provided documentation for installation instructions.

Configuring Nginx to work with PHP

server {
    listen 80;
    server_name localhost;

    root   /path/to/your/root;
    index  index.php index.html index.htm;

    location / {
        try_files $uri $uri/ =404;
    }

    location ~ \.php$ {
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }
}

Remember to adjust the root directive to your actual document root path.

Testing Your PHP Installation

Once everything is configured, it’s essential to test that your PHP installation is working properly with the web server.

Create a test PHP file named info.php with the following content:

<?php phpinfo(); ?>

Place this file within the root directory of your web server and navigate to http://localhost/info.php. A PHP information page should appear, indicating a successful setup.

Advanced Configuration

Beyond the basics, configuring a full web server with PHP on Windows may include adjusting settings for performance, security, and more advanced features.

Enabling Extensions

You may want to enable additional PHP extensions. To do so, uncomment or add the relevant line in your php.ini file. For example:

extension=mysqli

Securing PHP

Security should be a top priority for any PHP setup. Always disable functions that are not needed, and consider security settings relevant to your application’s needs.

Conclusion

Setting up PHP on Windows involves a handful of clear steps, from downloading PHP to configuring a web server and making test runs. While the basics can get you started quickly, mastering the advanced configurations will help you customize and secure your PHP development setup.