Hero image!

How to install apache on windows

April 08, 2024 - Drygast
Apache How To Install Web Server

Considering installing Apache on your Windows setup? It might just be the smart move you've been looking for. Apache HTTP Server (Apache), stands tall as one of the most trusted web servers worldwide. Its reputation for reliability, security, and adaptability precedes it, making it a top choice for website hosting and web application development.

Why Apache?

Apache stands as a cornerstone among web servers, boasting widespread adoption and a rich feature set. Incorporating Apache into your local windows environment can significantly streamline website development processes. Additionally, ensuring alignment between your local setup and your web hosting provider's server software is generally recommended. Given its robust capabilities, Apache emerges as a sensible and widely-supported choice for web site development.

Options

While MacOS users already have Apache and PHP bundled with their operating system, many Linux distributions also come with Apache pre-installed. However, for Windows users, installing Apache is necessary. Instead of installing it like I'm about to describe later in this article - here are a few options:

  • XAMPP: XAMPP is a popular cross-platform web server solution that includes Apache, MySQL, PHP, and Perl. It provides an easy-to-install package for Windows users, making it suitable for local development environments.
  • WampServer: WampServer is another all-in-one solution designed for Windows users. Similar to XAMPP, it includes Apache, MySQL, and PHP, providing a simple setup process for local web development.
  • Docker: Docker is a containerization platform that allows users to run applications in isolated environments called containers. By utilizing Docker, developers can easily set up Apache (or any other web server) within a containerized environment on Windows, ensuring consistency across different development environments.
  • Virtual Machines: Windows users can create virtual machines using software like VirtualBox or VMware and install Apache within these virtual environments. This approach provides a sandboxed environment for web development and allows users to mimic different server configurations as needed.
  • Windows Subsystem for Linux (WSL) is another viable option for running Apache on a Windows system. WSL allows users to run a Linux distribution natively on Windows, providing access to a full-fledged Linux environment directly within Windows.

Installing Apache

Keep in mind that the Apache HTTP Server Project itself does not provide binary releases of software, only source code.

Configure IIS

Apache listens for TCP/IP requests on ports 80/8080 and you need to uninstall or disable any program that uses those ports. If you have a Professional or Server version of Windows, you may already have IIS installed. If you would prefer Apache, either remove IIS as a Windows component or disable its services.

Download

Download the unofficial Windows binary from Apache Lounge from https://www.apachelounge.com/download/. It’s provided as a manually installable ZIP file.

Extract

Extract the files from the ZIP file to C:\Apache24. Make sure that there is no extra subfolders or anything. If you have a file at this location C:\Apache24\bin\httpd.exe, you are good. Obviously Apache can be installed anywhere on the system, but if so - you have to modify the SVROOT configuration parameter.

Configure

Apache is configured by editing the file C:\Apache24\conf\httpd.conf in any text-editor. Note that all file path settings use a forward slash (/) rather than the Windows backslash.

If you feel a bit nervous about editing this file, just create a backup and save it somewhere safe. I usually create a httpd.conf.bkp file and leave it in the same directory just in case I have to go back at some point.

Line 60, listen to all requests on port 8080:

#Listen 12.34.56.78:80
Listen 8080

Line 162, enable mod-rewrite by removing the #:

LoadModule rewrite_module modules/mod_rewrite.so

Line 227 #ServerName www.example.com:80 -> ServerName localhost:8080

ServerName localhost:8080

Line 234: AllowOverride None -> AllowOverride All.

<Directory />
    AllowOverride All
    Require all denied
</Directory>

Line 251 and 252 change root (optional, but needed if you do not want to use default path C:\Apache24\htdocs).

DocumentRoot "D:/dev/my-fancy-project/web"
<Directory "D:/dev/my-fancy-project/web">

Test the configuration file. Open a cmd prompt (WIN key, then write CMD and press enter).

cd c:\Apache24\bin
httpd -t
If you get the response: "Syntax OK" then everything is fine. If you get an error, fix the httpd.conf file until the test successful.

Start and Run

Now all the files are in the correct location and the configuration is done so we need to start the server. We want to handle the Apache server as a service that starts automatically when the computer starts.

Open a new command prompt as an administrator (WIN key, write cmd in the search box, press CTRL+SHIFT+ENTER and click yes on the popup).

cd c:\Apache24\bin
httpd -k install

Then open the services window (WIN key, write services.msc in the search box, press enter), locate Apache2.4 and change start method to Automatic. Or you could leave it as manual but then you have to manually press the START button in that services settings window every time you want to run the web server. That could be a valid option if you do not plan on using it very often.

Services (In a Swedish OS)

Test

To test if everything is working, first restart your computer so that the automatic service start is performed. Next, create a simple html document, save it as index.html and place it in the folder that you specified above (I used D:/dev/my-fancy-project/web but yours is probably different).

Next - open a browser and navigate to the URL: http://localhost:8080. If all goes well - your test webpage should appear.

Multiple Sites

Now, that is great but what if you have multiple fancy projects and want to have a dev-host for them as well? Enter virtual hosts.

Here is how to configure a virtual host for the D:/dev/my-fancy-project2/moreweb project.

Start with the C:\Apache24\conf\httpd.conf file and edit it. You need to add another port that the server listens to by adding a new row below the one we edited above (listen to port 8081 now as well).

#Listen 12.34.56.78:80
Listen 8080
Listen 8081

Next, open the file C:\Apache24\conf\extra\http-vhosts.conf file and edit it. The file contains examples of what to add and here is what I added:

<VirtualHost *:8081>
    ServerName myfancyproject2.com
    DocumentRoot "D:/dev/my-fancy-project2/moreweb"
    DirectoryIndex index.php index.html
    <Directory "D:/dev/my-fancy-project2/moreweb">
        AllowOverride All
        Options -Indexes
        Require all granted
    </Directory>
    ErrorLog "logs/myfancyproject2.com-error.log"
    CustomLog "logs/myfancyproject2.com-access.log" common
</VirtualHost>

Next, restart the service and test the new site. Add a basic index.html page in the directory you specified and navigate to http://localhost:8081 in any browser.

PHP

If you have installed Apache, you probably want to install PHP as well. It's not difficult, but requires a few extra steps.

Start by downloading the latest PHP x64 Thread Safe ZIP from https://windows.php.net/download/.

Extract the files to a new folder C:\php

PHPs configuration file is C:\php\php.ini. This doesn’t exist initially, so copy C:\php\php.ini-development to C:\php\php.ini. This default configuration provides a development setup.

First, enable any required extensions according to the libraries you want to use by removing the semi-colon like this:

extension=pdo_mysql

You might want to add the directory to the PATH environment as well. Click WIN key, type environment and press enter. Select the "Path" row in the system variables box and then click the Edit button. Add a new row C:\php. Sometimes you have to reboot for the changes to take place.

Now we have to configure PHP as a Apache module and for that we have to edit C:\Apache24\conf\httpd.conf file again. Shut down the Apache service and add the following to the bottom of the file:

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

If you want the index.php file to be preferred over the index.html file, then change "DirectoryIndex index.html" to the following:

DirectoryIndex index.php index.html

Test the install/config by creating a simple index.php file with the content:

<?php
phpinfo();
?>

Open a web browser and enter URL: http://localhost:8080/. A PHP version page should appear, showing all PHP and Apache configuration settings.

Summary

And that is how it is done. I really like to have things installed this way and will probably do it again for my next development computer. There are multiple valid alternatives, but I got used to have it installed this way and found it pretty useful. I get why people use the faster all-in-one packages and that is perhaps a smarter choice, but I'm old and slow moving :)

If I were to do anything different, I probably would use WSL2 instead. That thing is great and I use it for many other things already.