How to install and setup PHP with Apache on Windows Server 2022

Estimated reading: 6 minutes 1664 views

In this tutorial, you will learn how to install and configure Apache 2.4 and PHP 8 on a Windows machine. We all know Apache HTTP Server is an open-source cross-platform and free webserver to run web applications and similarly PHP is a free and open-source scripting language used to develop web applications.

To run the PHP code on a Windows machine, first, you’ll need to install and configure a web server (Apache) that executes the PHP application. And on this page, you will find the step-by-step guide on “How to install Apache 2.4 and PHP 8 on a Windows Server 2022”.

1. Install Apache on Windows

Step 1: Download Apache

The Apache project does not provide binaries or executable files for Windows. Instead, it offers source code to compile. If you are unable to do so, then the same page offers several binary distributions on the internet. In this case, we will use Apache Lounge. The first thing we have to do is go to the page and download the latest version. At the time of writing this post .it is 2.4.57

Next, create a folder named Apache24. This folder must be located in the root of the directory where Windows is installed. For example: C:\Apache24. Then unzip the content you just downloaded into the Apache24 folder. Please make sure that all folders are unzipped into the directory

Step 2: Install Apache

  • To install the Apache HTTP Server on a local Windows machine move the downloaded and extracted Apache 2.4 binary files to the C drive and follow the below steps:

  • Go to the Apache 2.4 bin directory path C:\Apache24\bin (it might be different in your case) and set it into your system environment variable path.

  • Open the command prompt with admin privileges and run the httpd -k install command to install the Apache services.

I encountered this error message when running the install command

What is the error VCRUNTIME140.dll is Missing?

Error VCRUNTIME140.dll is Missing is a message emitted by Windows to help users quickly recognize that the computer is missing folders with the .dll extension. Usually, the computer is missing these folders because when users update to new versions of Windows or when they fail to install a certain program or software.

To fix this problem you can follow below steps

Click here to go to the dll-files homepage > Find and download the file that matches the 32bit or 64bit version of your computer and download it

Next,you go to the Downloads folder > Right-click the vcruntime.zip folder > Select Extract files… to choose where to save or click Extract to vcruntime140\ save the extracted file in the Downloads folder.

Next,open the Run dialog box> Copy the path C:\Windows\System32 > Paste in the Open bar> Press Enter.

The System32 folder interface appears > Select and drag the vcruntime140.dll file into System32 > Turn off all open programs and restart the computer.

  • Then you can go back to running httpd -k install and check again

Step 3: Start the Apache service with the command httpd -k start or directly through the Task Manager » Services » Search for Apache2.4 » Right-click on the service » Start.

Note: By default, Apache HTTP Server runs on port 80, make sure no other services/servers running on this port.

Step 4: Open the web browser and hit the default Apache localhost URL localhost or localhost:80. If everything is configured correctly, you will see the default index.html web page contents and it is located inside the C:\Apache24\htdocs directory.

2.Install PHP 8 on Windows

Step 1: Download PHP

The first thing we have to do is download the latest version of PHP. With this in mind, please go to the download section and select the latest version (VC15 x64).

Step 2: Install PHP

Once you have downloaded the file, unzip it to the root of the Windows installation. For ease of use rename the folder as php. Finally, the route would be as follows: C:\php8 . Finally, we have to add the environment variable for php to run from any Command Prompt. For instructions on how to do this, please read here. Please edit the Path variable and add the following address: C:\php8

You can prove that the configuration is successful by running any php command on a CMD, such as php -v

3. Configure Apache to run PHP

  • Next, we are going to configure the Apache server to correctly interpret the php language. With this in mind, we are going to edit the httpd.conf file located in the following path: C:\Apache24\conf. Once the file is selected, open it with your favorite text editor.

  • After opening the file, please locate the DirectoryIndex line. Just leave a space after index.html and add index.php.

  • Go to the end of the file and add the below configuration because, in Apache, PHP is loaded as a module.
LoadModule php_module "C:\php8\php8apache2_4.dll"
AddHandler application/x-httpd-php .php
PHPIniDir "C:\php8"

Note: Repalce php_module location with your PHP path and

# if the php8apache2_4.dll is not available


# download the non thread safe version of PHP.

  • Go to the PHP home directory C:\php-8.1.4, and you will find two configuration files php.ini-development and php.ini-production. Create a copy of php.ini-development and rename it to php.ini

 

  • Again open the httpd.conf file and search for ServerName, uncomment and edit the ServerName with localhost.

  • Finally, save the file. Please note that to make the changes effective on the server, you have to stop and restart the service from Windows Services. The following command runs services.msc, once the window is open it locates the Apache server. Then I restart, clicking on the icon that I show you in the image:

Open the command prompt and run the httpd -t  command to validate whether everything is configured correctly, it will return Syntax OK if everything is configured correctly.

4. Running PHP

In order for the Apache server to correctly interpret the PHP language, it is necessary to rename the index.html file located in the following path: C:\Apache24\htdocs. Once the file is selected, change its extension to .php. In this way, the server will correctly interpret the PHP language.

To test the Apache 2.4 and PHP 8 configuration, restart the Apache2.4 service from the Task Manager and add the below piece of code from the C:\Apache24\htdocs directory

php
<?php phpinfo(); ?>

We save the file and open localhost in the web browser. If we have done everything correctly, then we should see this result

 

Summary

Finally, we have seen how to install and setup PHP with Apache on Windows Server 2022. This language is very popular worldwide, so being able to count on it is a great advantage for our projects. This is all for now, good luck!