How to Install PHP on Apache in Windows?
Updated on July 07, 2024
PHP is a popular general-purpose scripting language that is well-suited for web development. Similar to JavaScript, PHP can be embedded into HTML. However, unlike JavaScript, which can be interpreted on both the client (browser) and server, PHP is specifically designed to be interpreted only on the server.
How to download PHP?
You can download PHP from its official website at https://windows.php.net/download/.
On the download page, you will find two types of PHP binaries: non-thread-safe and thread-safe. If you want to install PHP on the Apache web server, you should choose the thread-safe binary.
How to Install PHP?
Step 1:
Extract the downloaded file (in our case: php-8.3.9-Win32-vs16-x64.zip).
Step 2:
Rename the extracted folder to php83 or any name you wish. To prevent compatibility problems, do not use space in the folder name.
Step 3:
Copy the php83 folder to root path (c:\) or any location you wish.
Step 4:
Add C:\ php83 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:\php83).
Step 5:
Configure PHP as an Apache module.
Navigate to the 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:/php83"
LoadModule php_module "C:/php83/php8apache2_4.dll"
AddType application/x-httpd-php .php
Note: remember to use forward slashes (/) rather than Windows backslashes (\) and change the c:/ php83 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 check for errors in the configuration file, open Command Prompt in administrator mode and navigate to C:\Apache24\bin and type httpd –t.
If no errors are found, start Apache with the following command: httpd.
If the Apache server is already running, restart it with the following command: httpd -k restart.
How to test your PHP installation?
Congratulations! Your PHP installation is complete. To verify that everything is working correctly, open Notepad and write the following script:
<?php phpinfo(); ?>
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.