Install Apache and PHP on Windows

Introduction

In this post, I will go over details of how to install Apache HTTPD webserver and PHP on Windows 10, 11 or Windows Server. I will show where to download and how to do configuration of Apache and integration with PHP.

I will also discuss some advanced configuration options of the setup as well.

Installing Apache HTTPD Webserver

Apache webserver files and all related information is located on the HTTP Sever Project website.

You can download the Apache source code and browse user, admin and developer documentation on the Apache website.

Other than Netware, Apache foundation seems to have stopped hosting downloadable binaries. Since this post is all about installing on the Windows platform I will focus on getting executable files for Windows.

Installing Apache Server Pre-Requisites on Windows

Apache requires that Microsoft Visual Studio (MSVC) runtime be installed for the HTTPD webserver to run. If you already have this downloaded and install then you can skip rest of this section.

If you don’t already have the runtime installed then download the latest redistributable runtime from Microsoft website.

Once on the MSVC runtime download webpage, you will find that there are different versions available to download. Find the correct version for your operating system and download and install it.

Shown below are direct links for downloading the MSVC runtime version 17.

ArchitectureLink
X86https://aka.ms/vs/17/release/vc_redist.arm64.exe
X64https://aka.ms/vs/17/release/vc_redist.x64.exe

Note: You may have to restart Windows after installing the MSVC runtime.

Download Apache Webserver

ApacheHaus and ApacheLounge are two sources where you can download pre-built binaries. I don’t really have a preference for either one of these sources as I have tested both and they work without issues.

One difference I have noted is that on the ApacheHaus website, they offer two versions of Apache with one supporting OpenSSL 1.x and the other build supporting OpenSSL 3.x. Both of the OpenSSL versions will work. Although some have reported that with the OpenSSL 3.x they had to have PHP 8.2 or higher.

I am going to use Apache with OpenSSL 3.x build from ApacheHaus. Download the zip file from here.

Installing Apache Webserver

Once your download is complete, you can unzip the files in any location on your computer. I am going to install all the files in the C:\apps\apache\httpd folder.

The structure of the folder will be:

C:\apps\apache\httpd>dir
 Volume in drive C is Windows
 Volume Serial Number is 5827-2245

 Directory of C:\apps\apache\httpd

01/15/2023  11:36 AM    <DIR>          .
01/15/2023  11:36 AM    <DIR>          ..
01/15/2023  11:36 AM            13,741 ABOUT_APACHE.txt
01/15/2023  11:36 AM    <DIR>          bin
01/15/2023  11:36 AM    <DIR>          cgi-bin
01/15/2023  11:36 AM           338,731 CHANGES.txt
01/15/2023  11:36 AM    <DIR>          conf
01/15/2023  11:36 AM    <DIR>          error
01/15/2023  11:55 AM    <DIR>          htdocs
01/15/2023  11:36 AM    <DIR>          icons
01/15/2023  11:36 AM    <DIR>          include
01/15/2023  11:36 AM             3,869 INSTALL.txt
01/15/2023  11:36 AM    <DIR>          lib
01/15/2023  11:36 AM            28,442 LICENSE.txt
01/15/2023  11:36 AM            21,187 Licenses.txt
01/18/2023  07:19 PM    <DIR>          logs
01/15/2023  11:36 AM    <DIR>          modules
01/15/2023  11:36 AM             2,486 NOTICE.txt
01/15/2023  11:36 AM               742 OPENSSL-NEWS.txt
01/15/2023  11:36 AM             1,387 OPENSSL-README.txt
01/15/2023  11:36 AM             4,752 README.txt
               9 File(s)        415,337 bytes
              12 Dir(s)  742,002,892,800 bytes free

Important folders to note here are bin, conf and htdocs for now.

BIN Folder

Bin folder has all the executables included with the Apache build. Here you will find the httpd.exe, the webserver executable.

Some of the other executables are:

  • AB and ABS: Performance testing
  • ApacheMonitor: Monitoring and managing the Apache webserver.
  • HtDBM, HtDigest and HtPassword: To create and manage Apache based security.
  • OpenSSL: Create SSL certificates.
  • RotateLog: For log management.

    CONF Folder

    The conf or configuration folder contains all configuration files used for the operation and setup of Apache functionality. There is also a sub-folder named extra which maintains configuration settings for SSL and VirtualHost configurations.

    There is also a folder named original which provides original configuration files in case you misconfigure the server configuration files and need to restart from zero.

    HTDOCS Folder

    htdocs folder contains the default index.html file that comes with the webserver. By default, the webserver serves all files from this folder.

    Running Apache From Command Line

    There are two ways to run Apache from the command line.

    # First
    C:\apps\apache\httpd\bin>httpd.exe
    
    # Second
    C:\apps\apache\httpd\bin>start httpd.exe

    The first method will run the executable as a child process of the existing shell and will block until you exit the process by typing CTRL+C.

    The second method uses the start command and will create a new command window and run the Apache webserver within it. The command window where will be unblocked and ready to accept new user input. To stop the Apache webserver you will need to close the new Window by clicking on the X.

    Apache Webserver Running on Windows in a Command Prompt Windows
    Apache Webserver Running on Windows in a Command Prompt Windows

    Validating Apache Webserver Install

    After Installing Apache and running it from command prompt you can go to the browser and open the URL http://localhost. The default index.html file will should be viewable if all install steps were correctly executed.

    Apache Haus default web page
    Apache Haus default web page

    Installing Apache as a Windows Service

    Although you can run the Apache webserver from the command line it is a better practice to install it as a service. This makes it easy to manage the server and ensure that it starts up after a system reboot.

    To install Apache as a Windows service you need to open a command prompt with Administration access. To open an Admin command prompt press the Windows+X keys together and in the popup dialog click on the Windows PowerShell(Admin) or Windows Command(Admin) menu item, to start the admin prompt.

    Windows+X

    To install Apache Webserver as a Windows service type the following:

    C:\apps\apache\httpd\bin>httpd.exe -k install

    To remove the Apache service use the following:

    C:\apps\apache\httpd\bin>httpd.exe -k uninstall

    Other available options with -k are start, stop and shutdown.

    Now that our Apache Webserver is installed in the next section I will go over details of installing PHP.

    Installing PHP on Windows

    To install PHP go to the website and download the latest version of PHP. Do note that if you have downloaded Apache with OpenSSL 3.x then you need to have at least PHP 8.2 version.

    On the download page you will find two different types of PHP downloads.

    • NTS or Not thread-safe
    • Thread-safe

    I will be doing an install of PHP as an Apache module, therefore I will download the thread-safe version.

    Install PHP Locally

    After downloading PHP zip file, unzip and copy the contents to a folder. I will install it at location C:\apps\lang\php. The folder contents will be:

    C:\apps\lang\php>dir
    
     Directory of C:\apps\lang\php
    
    01/15/2023  11:58 AM    <DIR>          .
    01/15/2023  11:58 AM    <DIR>          ..
    01/15/2023  11:51 AM           137,728 deplister.exe
    01/15/2023  11:51 AM    <DIR>          dev
    01/15/2023  11:51 AM    <DIR>          ext
    01/15/2023  11:51 AM    <DIR>          extras
    01/15/2023  11:51 AM         1,609,728 glib-2.dll
    01/15/2023  11:51 AM            18,944 gmodule-2.dll
    01/15/2023  11:51 AM        30,422,016 icudt71.dll
    01/15/2023  11:51 AM         3,031,552 icuin71.dll
    01/15/2023  11:51 AM            60,928 icuio71.dll
    01/15/2023  11:51 AM         2,253,312 icuuc71.dll
    01/15/2023  11:51 AM    <DIR>          lib
    01/15/2023  11:51 AM         5,192,704 libcrypto-3-x64.dll
    01/15/2023  11:51 AM            42,496 libenchant2.dll
    01/15/2023  11:51 AM           289,792 libpq.dll
    01/15/2023  11:51 AM           209,920 libsasl.dll
    01/15/2023  11:51 AM           303,616 libsodium.dll
    01/15/2023  11:51 AM         1,646,080 libsqlite3.dll
    01/15/2023  11:51 AM           380,928 libssh2.dll
    01/15/2023  11:51 AM           776,192 libssl-3-x64.dll
    01/15/2023  11:51 AM             3,272 license.txt
    01/15/2023  11:51 AM            20,972 news.txt
    01/15/2023  11:51 AM           228,352 nghttp2.dll
    01/15/2023  11:51 AM                43 phar.phar.bat
    01/15/2023  11:51 AM            65,969 pharcommand.phar
    01/15/2023  11:51 AM            69,120 php-cgi.exe
    01/15/2023  11:51 AM            38,400 php-win.exe
    01/15/2023  11:51 AM           142,336 php.exe
    01/15/2023  11:51 AM            75,124 php.ini-development
    01/15/2023  11:51 AM            75,272 php.ini-production
    01/15/2023  11:51 AM            35,840 php8apache2_4.dll
    01/15/2023  11:51 AM           932,824 php8embed.lib
    01/15/2023  11:51 AM           278,016 php8phpdbg.dll
    01/15/2023  11:51 AM         9,049,088 php8ts.dll
    01/15/2023  11:51 AM           280,064 phpdbg.exe
    01/15/2023  11:51 AM            30,888 readme-redist-bins.txt
    01/15/2023  11:51 AM             5,364 README.md
    01/15/2023  11:51 AM             2,285 snapshot.txt

    Following files and folders are important to note:

    EXT Folder

    The ext folder includes all modules provided with PHP. Modules provided additional features which can be added to PHP by enabling them through config files.

    PHP.INI* Files

    Multiple sample config files are provided with PHP. We will use the config file to manipulate configuration settings as well as enabled various features.

    Note: Please make a copy of the file php.ini-development and rename it to php.ini. This will be used later.

    PHP.exe

    This is a command line executable of PHP used to run command line scripts.

    php8apache2_4.dll

    This is the dynamic PHP Apache module, loaded using Apache configuration to process .php files.

    PHP install really require nothing more than just unzipping PHP files and copying them to a local folder.

    Add PHP to Windows Path (Optional)

    There is one additional step you will need to complete to ensure that all PHP modules will correctly work. For example if you are going to enable PHP CURL module, used with WordPress installs, then you need to add the PHP install folder to Windows path.

    Edit the system environment variables by going to Settings and type env in the search box and open the “Edit the system” option. Click on Environment Variables button and update the Path variable and add the PHP folder path.

    Using my folder path, the setting will look like the following:

    Edit Windows Path Environment Variable
    Edit Windows Path Environment Variable

    Once you have added the path. Save all changes.

    Configure Apache Webserver to Use PHP

    With both Apache webserver and PHP installed, I am now ready to setup the integration of PHP with Apache.

    Make changes to Apache Configuration

    Open the Apache configuration file httpd.conf file in a text editor located at C:\apps\apache\httpd\conf.

    Add the following lines to the end of the httpd.conf file.

    # PHP 8.2
    AddHandler application/x-httpd-php .php
    AddType application/x-httpd-php .php .html
    LoadModule php_module "C:/apps/lang/php/php8apache2_4.dll"
    PHPiniDir "c:/apps/lang/php"

    Using the config options I am doing the following.

    • AddHandler: adds the PHP mimetype
    • AddType: Let’s Apache know that .php and .html files can be processed by the PHP module
    • LoadModule: Path of the PHP dynamic module to be loaded by Apache
    • PHPiniDir: Path to the php.ini file.

    Create PHP Test File

    With the changes in place, let’s create a PHP test file.

    Open a text editor and copy the following text to it.

    <?php
    phpinfo();
    

    In this file I am using a built in PHP funtion phpinfo, which prints PHP configuration as HTML text.

    Save this file to the location C:\apps\apache\httpd\htdocs and name it index.php.

    Testing Complete Apache & PHP Install

    Now that both Apache and PHP have been installed and configured, to test the setup start by starting Apache webserver.

    if installed as a service you can go to service manager and start the Apache server. If it is already running make sure to stop and and start it again so it can pickup configuration changes.

    You can also start the server from command prompt as I have shown earlier in this post.

    Once the server is up and running, then go to the browser and enter the url, http://localhost/index.php.

    If everything is configured correctly a web page similar to the one below display PHP configuration will be shown.

    PHPInfo Output on Windows
    PHPINFO() Output on Windows