The Straight Forward Setup of Apache and PHP on Windows

Apache and PHP Configuration

Introduction

If you are a web developer wanting to get started using Apache and PHP on your Windows machine, but want a little more control than just using WAMPServer or WAMP, then this article is for you. This article will take you through some of the steps, in a straight forward manner, to get you up and running. If you are like me you might have already been doing some ASP.NET stuff through IIS. To that end we will also talk about how we can get Apache 2.4 to play nicely with that setup. We will show you how to setup PHP 7 to work with Apache 2.4, some of the pitfalls that you can steer clear of and keep the non-essential stuff to a minimum. We won’t be covering virtual hosts in this article, but there is plenty of articles on the web if you are interested. Just make sure to find address vhosts for Windows if you can. So lets get started!

Requirements

We will be using some of the latest releases at the time of this writing. I am using Windows 10, Apache 2.4 and PHP 7.1 but you can use pretty much any “modern” version of PHP for Windows. So start by downloading Apache and PHP using the links I have provided below:

  1. Windows 10 (but I would assume Windows 7 would work as well)
  2. Apache 2.4 (or the latest) – Here, for simplicity, we can get the recent version of Apache for Windows. This is a ZIP file and can help you skip past all the messy work of needing to compile from source code. This is a well respected download site and even pointed to from the official Apache website.
  3. PHP – For use with Apache on Windows, PHP.net recommends using the Thread Safe (TS) versions which will contain a file called “php7apache2_4.dll” in the root directory. You will know you have the wrong version if you open the downloaded ZIP file and it doesn’t have that file. So be sure to check for it before unzipping. Also be sure to get x86 if you are not on Windows 64-bit and x64 if you are on a 64-bit system. For most people they will be downloading the TS x64 version of PHP.

Installation Tips

  1. Installing Apache – Installation is pretty much extracting the files to a directory. I recommend unzipping this folder right onto the root drive (typically C:\). This will make the directory C:\Apache24 (or similar depending on the version). If you do decide to change this (which I don’t recommend) then take note of where this directory is. That is all that is needed for an installation of Apache.
  2. Installing PHP – I like to put PHP in a directory that is also easy to locate and access. Unzip the file anywhere, but if you have a drive you have dedicated to programming projects then put it there. I would set it up at C:\php or F:\php or whatever drive you want to use. One quick tip I suggest is a directory that doesn’t contain any spaces in the path or strange naming schemes. C:\php is short and sweet. Be sure to remember the name of this location.

Configuration

Configuring Apache – Go back to your folder where you installed Apache. In that directory you will see a folder called “conf”. In that folder you will see a file called “httpd.conf”. This file is your configuration file for Apache. Just about anything critical to Apache can be set in there. We are going to set a few parameters and add a few lines to get it serving us some documents. NOTE: Path names are going to use a forward slash, not a backward slash!

httpd.conf is a rather long document but much of it is documentation on various settings. I encourage you read those as we go through this. The first setting we are going to modify is “ServerRoot” (around line 37). We want to make sure the path here is set to where Apache is installed. If we didn’t change anything from the default, this will be set to something like C:/Apache24 which is where we unzipped the files to. If you unzipped them elsewhere, you will have to change that line to the FULL path to where you unzipped Apache.

A little further down we will see a line that reads “Listen 80”. Now this is normally fine because we want to listen for any requests on port 80 (that is the standard port for HTTP). In most cases we won’t change it for normal site testing. But if you have used IIS for ASP.NET dev work, check out the note below.

NOTE ABOUT ASP.NET / IIS Users: The problem here arises if you are using IIS because most of the time ASP.NET is going to take control of that port and it is a bit tedious to get it off that port. So let Apache listen elsewhere. If we set this line to “Listen *:9000” we are asking that Apache listen to all requests coming in on port 9000 and answer those. Pick a port number that is easy to remember. Then when we ask for pages we will specify pages using URLs like http://localhost:9000/somepage.html.

Next is the modules we want to load. If you plan on using this site for WordPress or site that has “SEO friendly URLs” I would suggest uncommenting the line “LoadModule rewrite_module modules/mod_rewrite.so” so you can get URL rewriting loaded.

Scroll further and you will see “ServerName” which is commented out. For local development I typically uncomment this and set it to “localhost” along with the port number we specified above for the “Listen” setting. In that case I set it to “localhost:9000”. Now if you have a hostname defined that points to your local machine, then you could specify that here. Just make sure the ports match up with your “Listen” setting.

Further still we encounter another important setting “DocumentRoot”. Most of the time you can leave this as is because it will point to the htdocs folder of where you installed Apache. If you installed Apache anywhere but C:/Apache24, then be sure to change this to match that new location. You can however completely change this to another directory on your machine. If you think about ServerRoot, Listen, ServerName and DocumentRoot you can see how these might fit together during a request for a document. We go to C:/Apache24, listening on port 9000 using the localhost domain and serving documents out of C:/Apache24/htdocs.

Right below this “DocumentRoot” setting is a “Directory” setting. Make sure this matches your “DocumentRoot” and this tells Apache how to handle the directory with permissions and settings. If you plan on running WordPress or a site that uses .htaccess, I suggest making “AllowOverride” set to “All” instead of “None”.

QUICK TIP: In general for any paths you find that point to C:/Apache24 should be changed to wherever you unzipped Apache. If you used C:/Apache24 then you don’t have to change anything (the benefits of my words of wisdom to not change this at the start).

Last thing for this Apache config is to add in some lines to tell Apache where to find and handle PHP files. At the bottom of the file just add these lines…


AddHandler application/x-httpd-php .php 
AddType application/x-httpd-php .php .html 
LoadModule php7_module "F:/php/php7apache2_4.dll" 
PHPIniDir "F:/php"

Now of course change the paths to wherever you unzipped PHP. In the example above I unzipped PHP to my F:\ drive. These lines tell Apache to load a module located at F:/php/php7apache2_4.dll and that PHP is in the directory F:/php. Then when a request for a file that has an extension .php comes in, Apacahe will use that dll to handle processing the file. This is why we needed to make sure that we had that dll file in the package we downloaded.

Save the “httpd.conf” file. That should be all we need for a straight forward config. We can always come back and edit more if need be. Just remember that each time you edit the file you should restart the httpd service to load in the new changes. We will talk a bit more about that in a few.

Configuring PHP 7 – I am going to assume that you already know a bit about configuring PHP 7 using the php.ini file. If not, look in your PHP directory and open the php-development.ini in a standard text area. Then read the comments. You can customize PHP 7 settings in the php.ini file and decide what modules you want to load etc. In the PHP folder you unzipped, you can take the php-development.ini and just rename it php.ini and be off to the races. I typically like to load a few additional modules by uncommenting the lines related to the modules that I need. I typically uncomment lines related to openSSL, PDO MySQL, MySQLi, mbstring, gd2 and fileinfo but you can choose what you need. Again, start with something minimal at first and you can go back later to uncomment some more.

Other important settings to consider are paths to your log files, log levels and file uploading and size limits. Again you can always go back and enable what you need later. Take note that some of these paths use the backslash unlike httpd.conf did.

One last thing to configure is an environment variable path to the PHP binary. On Windows 10 you can follow these steps…

  1. Click the search icon (or ask Cortana) and type “Control Panel”
  2. In control panel click “System”
  3. From the menu on the left click “Advanced System Settings”
  4. Click the “Advanced” tab at the top if you are not already there
  5. Click “Environment Variables” button at the bottom of that dialog box
  6. The top box contains your user specific variables, the bottom is system variables. In the top box select “Path” and click “Edit”
  7. Click the “New” button on the right and enter in the path to your PHP folder (This is really easy if you followed my advice above about making the path simple). So for example you can enter “F:\php” if you put PHP on your F:\ drive
  8. This should add an entry to the list so click OK
  9. You are done!

Testing Apache Setup

Ok so it has taken a bit of time to install and configure everything. If you had invested a few minutes of prep at the start then the configuration probably took you 10-15 minutes to run through. If you didn’t, then it might have taken you a bit longer. Now we are at the moment of truth. Follow these steps to run a simple test of Apache 2.4…

  1. Open a text editor and start by putting a standard HTML document in there that reads “Hello World”
  2. Save the file as index.html and put it into C:\Apache24\htdocs or the folder you told the httpd.conf that you would be serving documents out of. The default was the htdocs folder
  3. Open a command prompt
  4. Navigate to C:\Apache24\bin (or again wherever you unzipped Apache and its bin folder)
  5. Type httpd.exe which will run that server. No output will show, it will just sit there as if it is a hung app. Leave it like that
  6. Open a browser and navigate to http://localhost:9000/index.html (or whatever ServerName you specified) to see if we can get that “Hello World” message to show. If you see it, congrats! Apache is running. If not, go back and review the configuration and make sure that all your paths and server names are correct. Look at ServerName, Listen, DocumentRoot etc. Changes will need to be reloaded so press Ctrl + C in the prompt window to stop Apache. Then retype “httpd.exe” to start it again.

Now Test PHP 7

  1. In your text editor create a new file called “index.php”
  2. Add to the file the line: <?php phpinfo(); ?>
  3. Save the file and put it into your C:\Apache24\htdocs folder
  4. Open a browser and navigate to http://localhost:9000/index.php and see if it shows you the PHP configuration info page. If so, congrats PHP and Apache are working together! If not, first make sure you can see the index.html from the Apache test above. If you can see that, then you know that perhaps PHP isn’t configured correctly. Make sure to review your php.ini setting changes. If you make changes to php.ini restart Apache using the instructions above.

Conclusion

At this point you should see HTML and PHP files being executed on Windows and despite IIS being installed (if that was a problem). Apache configuration can be a bit daunting so start simply with a few key setting changes and as many defaults as you can. If you unzip the files into default paths, as I have instructed at the start, then you should have minimal efforts. Once you get things running, it would be a good time to take backup copies of httpd.conf and php.ini. You can revert to them again if you misconfigure and break things.

Where to Go Next?

Well we are 3/4 of the way through a full WAMP stack so if you want to continue, the next process step would be to setup MySQL and I will leave that for another article. However, with these things set up, you should be able to test local websites (that don’t use a database… so don’t try WordPress sites until you have MySQL) on the latest Apache 2.4 and PHP 7.1. I hope you found this article easy enough to read and follow. If you would like to work with me, be sure to send me a message on my homepage at www.timhurd.com and use the contact form. Thank you for reading!

Tim Hurd

About Tim Hurd

Tim Hurd is a veteran programmer with over 25 years experience developing for a plethora of industries from travel to education to software and beyond. He has managed junior developers and continues to mentor others through his work on dreamincode.net and coderslexicon.com. He is available for freelance work and can handle full stack development. To reach him please contact him through timhurd.com.