Tuesday, October 18, 2016

Apache Tutorial: natives of the Southwest deserts?!

This is why we shouldn't look to Wikipedia for all of our daily questions. Don't let the first wiki link fool you, we aren't referring to the Apache tribe but instead the Apache HTTP server, aka the world's most used web server software! Want to know how to install Apache and configure a virtual host? Check out the tutorial below!


Step 1: Let's go download Apache! 

Before we download Apache we will need to get the latest C++ distributable, this can be acquired by going here. Upon  installation completion, please continue with tutorial.

First things first we go to: https://www.apachelounge.com/download/ and select which version we would like to download. For the purpose of this tutorial we will be using the Apache 2.4 VC14 Win64 binary. VC indicates the version of the compiler, VC 14 is the latest version and that is why we selected this version, 64 indicates it is for a 64 bit platform. To begin to download the .zip, click on the version you would like to download and it will begin automatically (See Figure 1.1).


Figure 1.1 Downloading Apache from Apache Lounge

Step 2. Extract Apache!

Once the download has finished you will extract the files, I extracted them and then placed them in the root of the C: drive for consistency across our organization (Figure 1.2) however it is worth noting Apache can be installed anywhere on your system so long as you change the configuration paths accordingly.
Figure 1.2 Extracting Apache to the root of the C: drive

Step 3. Configure!

Once you have Apache installed, you then need to configure it. To do this you need to open the httpd.conf file. Because we installed Apache in the root of the C: drive to find our httpd.conf file we will use the following path: C:\Apache24\conf, this may vary depending on the location of your installation. Open this file using your desired text editor and make the following modifications.
  1. Modify Listen to listen to all requests on port 80 by changing it to "Listen *:80" (Figure 1.3).
  2. Specify the server domain name (Figure 1.4)
  3. Allow .htaccess overrides, this controls what directives may be placed in .htaccess files (Figure 1.5).

Figure 1.3 Modifying Listen to listen to all requests on port 80
Figure 1.4 Specifying server domain name
Figure 1.5 Allowing .htaccess overrides

Step 4. Test our Apache installation & configuration 

We can test our installation in the command prompt. To open a prompt click the Start menu, type cmd and press enter. Ensure you are in the bin directory of your Apache installation, to do this you can use the cd command. Because I installed Apache in the root of the C: drive my path is: C:\Apache24\bin. Once in the bin directory run the command "httpd -t" until there are no configuration errors, you should receive the message "Syntax OK" (Figure 1.6).

Step 5. Install Apache as a windows service

Apache must now be installed as a windows service. You can do this from the command line in the exact location you were at for the previous command. This time you will run " httpd -k install" (Figure 1.6).

Figure 1.6 Testing Apache configuration and installing as a windows service

Step 6. Is Apache working?

To test if Apache is working, open up the Apache bin folder in Windows Explorer. From there double click on Apache Monitor, if it was successful you should see a green light on the symbol in the toolbar (Figure 1.8).
Figure 1.7 Locating the Apache Monitor Application
Figure 1.8 Successful starting of Apache Monitor

Step 7 Testing localhost

Navigate to C:\Apache24\htdocs and create a file called index.html, within this new file insert some HTML to verify localhost is working.

Hit localhost in your chosen web browser, if successful you should see the contents of the index.html you just created (Figure 1.10).

Figure 1.10 Successfully hitting localhost in a web browser

Let's take it one step further: Configuring two virtual hosts!!

Since we now have Apache working why don't we configure a couple of virtual hosts to go with it!?

Step 1: Add two new index files in separate folders

Navigate to C:\Apache24\htdocs and create two separate folders, name one folder hecklersguild and the other your first and last name(See Figure 1.11 for an example). Within each folder create two separate index.html files and make them distinct in some way, an example is shown in Figure 1.11.
Figure 1.11 Creating 2 separate folders in htdocs

Figure 1.11 Sample html code contained in index.html

Step 2: Add two new virtual hosts

Navigate to C:\Apache24\conf\extra and open httpd-vhosts.conf in a text editor. Copy the example virtual host twice, we will make alterations to each copy. Change each document root to the distinct folders you created in step one. Add a server alias to each new virtual host. Add a server name to each, this is what you will type in the web browser to access the virtual hosts. Ensure the directory indexes are set to index.html because that is what we named the files in step 1. Finally adjust ErrorLog and CustomLog to where you would like them saved. See figure 1.12 for all of the changes you should make.
Figure 1.12 Additions to httpd-vhosts.conf 

Step 3: Add entries to the host file

Navigate to C:\Windows\System32\drivers\etc. Open up the hosts file in a text editor. Add two entries below the comments as demonstrated in Figure 1.13
Figure 1.13 Adding entries to the host file

Step 4: Include virtual hosts configuration file

Navigate to C:\Apache24\conf and open httpd.conf in a text editor, search for Virtual hosts by pressing ctrl+f and typing it in the search, once this is located (as seen in line 495 of Figure 1.14) uncomment the line by removing the pound symbol. This will include virtual hosts in your configuration.
Figure 1.14 Uncommenting the line necessary to include virtual hosts configuration

Step 5: Does it work?!?

Open up your favorite browser and type hecklersguild.com, if your custom index.html appears you did it (see Figure 1.15)!! Type firstName+lastName.com as you configured it above and that too should be your index.html set in that folder(see Figure 1.16).
Figure 1.15 Checking the first virtual host

Figure 1.16 Checking the second virtual host


No comments:

Post a Comment