How to Install PHP on Apache in Windows?



PHP is one of the most popular general-purpose scripting languages that is especially suited for web development. Just like Java Script, PHP can be embedded into HTML, but unlike Java Script that can be interpreted both on client (browser) and server, PHP is specially designed to be interpreted on server only.


How to download PHP?


You can download PHP from its official website at https://windows.php.net/download#php-8.2.

In the download page you can find two types of PHP binary, the non thread safe and thread safe. If you want to install PHP on Apache web server, you will need to get the thread safe binary.





How to Install PHP?



Step 1:

Extract the downloaded file (php-8.2.7-Win32-vs16-x64.zip).



Step 2:

Rename the extracted folder to php82 or any name you wish. To prevent compatibility problems, do not use space in the folder name.



Step 3:

Copy the php82 folder to root path (c:\) or any location you wish.



Step 4:

Add C:\ php82 to the PATH environment variable.

In the search box on the taskbar, type environment, then select Edit the system environment variables.





System Properties dialog will be shown up. Then click the Environment Variables button.





Environment Variables dialog will be shown up. On the user variables section, select Path from the listview and click the Edit button.





The Edit environment variables dialog will be shown up. Click the New button to add a new entry. Then write path to the PHP folder. In our case we write C:\php82.






Step 5:

Configure PHP as an Apache module.

Go to Apache’s conf folder (in our case, the folder is located at C:\Apache24\conf). Open httpd.conf configuration file in a text editor (Notepad).

Add the following lines to the bottom of the file to set PHP as an Apache module:


# PHP8 module

PHPIniDir "C:/php82"

LoadModule php_module "C:/php82/php8apache2_4.dll"

AddType application/x-httpd-php .php


Note: remember to use forward slashes (/) rather than Windows backslashes (\) and change the c:/ php82 with your PHP folder location.

To register index.php as the default page, find DirectoryIndex variable and change as follow:

The initial value:





Change it to:


<IfModule dir_module>

 DirectoryIndex index.php index.html

</IfModule>


We already finished editing the configuration file. Now save the httpd.conf file.



Step 6:

Test configuration file.

To test if there is an error in the configuration file, go to C:\Apache24\bin and type httpd –t.





If no error is found, start Apache with the following command: httpd.

If the Apache server is already running, restart it with the following command: httpd.exe -k restart.


How to test your PHP installation?


Congratulations, your PHP installation is already finished. To check if everything is working properly, open Notepad and write the following script:





Name the file as index.php and save it in Apache’s web page root folder at C:\Apache24\htdocs.

Open a web browser and enter your server address: http://localhost/. If no error is found, you should see the PHP technical information page.