Up and running fast with PHP/Apache on Windows 10

With all the various server options available, such as NodeJS, IIS Express, .NET Core offerings, tons of cloud offerings, it’s important to remember that, on the internet today, PHP is still the king and Apache is its throne. This is not to say or enter into the argument if its the best choice, but it is definitely the most economic choice, being entirely free.

So there’s little reason you shouldn’t have a copy of it to run on your local computer instead of browsing through CPanel on a cheap hosting service and doing a Los Angeles freeway’s worth of traffic uploading file modifications. Just run it locally! It’s not that difficult.

There are a lot of vendors who including handy packages such as MAMP, but these don’t gain you much besides the setup I’m about to outline. Also, the experience of configuring and running a local server is worth the time.

Download PHP and Apache

For this guide, I’m using Apache 2.4.41, and PHP 7.3.7. Get the zips for these first:

Installing PHP

Extract the php zip into a convenient folder on your local drive. C:\php is a safe bet.

Now, in your new php folder, copy and rename the php.ini-development to php.ini. This will cause the php initialization to use the default settings for development, which is good for localhost work.

In the php.ini file, set the extension_dir to your php folder’s ext subfolder:

Enable the following extensions which will be needed by MySQL, in case you plan to use that. (Note: curl is a very common network request tool, and gd2 is a graphics library which comes in handy for manipulating/drawing images).

Open Control Panel and Go to System and Security, and then choose System (Control Panel\System and Security\System).

Once there, click on Advanced system settings. In the popup, under the Advanced Tab, click the [Environment Variables…] button at the bottom:

Edit the Path variable in your Environment Variables:

Click the New button to add to the environment variable list, and add your php folder:

Click OK to both pop-up windows to apply the change.

Install Apache

Now extract the Apache zip to your local drive. The zip I link above has two items in the root, a folder called Apache24, and a readme_first.html file, so we can just extract the whole thing to the C drive.

Go to the extract folder (C:\Apache24), open the conf subfolder, and edit the file httpd.conf

In the httpd.conf file, modify the DirectoryIndex configuration. This will tell Apache that index.php is the first file to look for under a folder it is serving up. Notice I’m keeping the index.html file too, but you don’t have to:

Then, at the end of the file add this:

PHPIniDir "C:/php"
LoadModule php7_module "C:/php/php7apache2_4.dll"
AddType application/x-httpd-php .php

Run Apache

Open CMD and run httpd, which you will find under your bin sub folder of the Apache folder (C:\Apache24\bin\httpd.exe)

You may have to allow it throw Windows Firewall on first run:

When this is running, it will won’t write any more to the command line. And if you need to stop it, do CTRL+C. And you will then be prompted if you want to “Terminate Batch Job (Y/N)?”, and the answer is Y.

Both the start-up and shut-down can take a moment. Be patient especially with the shut-down and wait for the terminate prompt.

Testing PHP

The hello world you can use to test your new PHP/Apache setup is the following dirt simple php file:

echo "Hello World!";

Put this in a text file and save it as index.php in your Apache htdocs folder (C:\Apache24\htdocs\index.php)

Now when you open “localhost” in your browser, if everything installed okay, you should see This:

And the best part is, once you know your PC’s network IP address, you can visit your hosted content from your phone, tablet, other computers etc. Just run ipconfig from the Command Prompt, and look for this piece of information:

So in this case if you browse to http://<Your IPv4 Address>/ on one of these devices or another computer, you should see “Hello World!”.