Installing PHP is relatively simple. Follow the steps in this guide to install PHP version 8 on a Windows operating system (I am using Windows 10). These instructions are for a dev box; no considerations have been made for security. You will typically be doing this as part of the process of setting up a WAMP dev box which is discussed in the WAMP (&LAMP) tutorial.

Not installed

If PHP is installed, there will be a PHP directory (most likely on the root).

cmd prompt

  1. Open the command prompt in the php directory
  2. Enter the command PHP -version

Browser

Screenshot showing PHP code displayed when PHP is not configured on the server.
  1. Open a PHP file in your browser
  2. The script is output as plain text instead of being processed

Download

  1. Download PHP from its official website.
  2. Select and download the .zip file from the respective section depending on your system architecture (x86 or x64).
  3. For Windows 10 select php-8.2.4-Win32-vs16-x64.zip

Install

  1. Extract the .zip file
  2. Move the resulting folder — php-8.2.4-Win32-vs16-x64 — to your C-drive
  3. Rename the folder to php (i.e. C:\php).

PATH

Add the folder (C:\php) to the Environment Variable Path so that it becomes accessible from the command line:

  1. Right-click on My Computer or This PC icon, then select Properties from the context menu.
  2. Then click the Advanced system settings link, and then click Environment Variables.
  3. In the section System Variables, we must find the PATH environment variable and then select and Edit it.
  4. If the PATH environment variable does not exist, click New.
  5. In the Edit System Variable (or New System Variable) window, specify the value of the PATH environment variable (C:\php or the location of our extracted php files).
  6. Close all remaining windows by clicking OK.

Installed

  1. Open the command prompt to the PHP directory
  2. Enter the command: PHP -version

At this stage, you can run PHP locally from the command prompt. If that is your aim, head on over to “Hello World!” with PHP CLI. However, if you want to use PHP on a web page, read on.

Web

If you are setting up an environment for web development, you will most likely already have installed Apache HTTP Server. You will then need to configure Apache and PHP. Before you do that, it may be a good idea to run PHP’s built-in web server as a test. For this tutorial, I will assume you installed Apache at the following location: C:\Apache24

Create a simple index.php in your htdocs folder (C:\Apache24\htdocs); the following will do:

<?php
echo "Hello World!";

The syntax of the command to type at the command prompt to start the web server up is as follows:

php -S localhost:port -t your_folder/

If you have a standard set-up, the command will be:

php -S localhost:8000 -t C:/apache24/

Open your browser and enter localhost into the address bar.

Apache configuration

The final step is to configure Apache. Before Apache sends a web page to the client it checks to see if there is any PHP code that first needs to be processed by the PHP engine. For this to happen, Apache needs to know where the PHP processing module is. This is done by adding some info to the Apache configuration file which is at the following location by default: C:\Apache24\conf\httpd.conf

Open httpd.conf and paste the following on a new line at the end of the file:

# PHP8 module
PHPIniDir "C:/php"
LoadModule php_module "C:/php/php8apache2_4.dll"
AddType application/x-httpd-php .php

Save and close the config file.

Restart Apache

You will have to restart Apache once you have made and saved changes to your config.

A screenshot of the Apache Service Monitor.

View

Open your browser, type localhost/index.php in the address bar and press Enter.


References:

  1. GeeksforGeeks. (2019). How to execute PHP code using command line? Available at: https://www.geeksforgeeks.org/how-to-execute-php-code-using-command-line/ (Accessed: 28 March 2023).
  2. PHP: Hypertext Preprocessor. (no date) PHP For Windows: Binaries and sources Releases. Available at: https://windows.php.net/download/ (Accessed: 10 June 2024).

By MisterFoxOnline

Mister Fox AKA @MisterFoxOnline is an ICT, IT and CAT Teacher who has just finished training as a Young Engineers instructor. He has a passion for technology and loves to find solutions to problems using the skills he has learned in the course of his IT career.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.