Configuring Apache 2.0.X - Virtual Hosting

February 5th, 2008 General

What is virtual hosting? Well, as we discussed in setting up dns and how dns works, you might be wondering that if every domain and subdomain has it’s own ip address then, wouldn’t we run out of ips? Yes. Of course! But, the cool thing about virtual hosting is that you can place multiple domains on the same ip. The only exception to this is SSL which requires it’s own ip address for each certificate. So, how do you setup virtual hosting? First thing, make sure all your domains or subdomains point to the same ip address in your dns settings.

So for example if your ip was 127.0.0.1 then: tech.mydomain.com , www.mydomain.com and blog.mydomain.com should all point 127.0.0.1 .

Next, let’s go to your apache configuration (httpd.conf) file.

In httpd.conf let’s go to the bottom of the file an add the following,

NameVirtualHost *

<VirtualHost *>
ServerAdmin webmaster@mydomain.com
DocumentRoot c:/home/webpub/tech.mydomain.com/
ServerName tech.mydomain.com
ErrorLog c:/home/logs/tech.mydomain.com-error.log
CustomLog c:/home/logs/tech.mydomain.com-access.log common
</VirtualHost>

<VirtualHost *>
ServerAdmin webmaster@mydomain.com
DocumentRoot c:/home/webpub/blog.mydomain.com/
ServerName blog.mydomain.com
ErrorLog c:/home/logs/blog.mydomain.com-error.log
CustomLog c:/home/logs/blog.mydomain.com-access.log common
</VirtualHost>

<VirtualHost *>
ServerAdmin webmaster@mydomain.com
DocumentRoot c:/home/webpub/www.mydomain.com/
ServerName mydomain.com
ServerAlias *.mydomain.com
ErrorLog c:/home/logs/www.mydomain.com-error.log
CustomLog c:/home/logs/www.mydomain.com-access.log common
</VirtualHost>

Let’s review over what we did.

NameVirtualHost *

This tells us we will run virtual hosts on all ips and ports.

<VirtualHost *>
ServerAdmin webmaster@mydomain.com
DocumentRoot c:/home/webpub/tech.mydomain.com/
ServerName tech.mydomain.com
ErrorLog c:/home/logs/tech.mydomain.com-error.log
CustomLog c:/home/logs/tech.mydomain.com-access.log common
</VirtualHost>

This snippet tells apache to setup a virtual host that answers to the name tech.mydomain.com with the path for the documents  being c:/home/webpub/tech.mydomain.com/. If you look at the end of the configuration changes you will see an additional parameter for the virtual host mydomain.com.

The parameter ServerAlias allows you to assign domain aliases so that multiple domains or subdomainscan point to the same virtual host setup. So, for example, by placing this snippet last:

<VirtualHost *>
ServerAdmin webmaster@mydomain.com
DocumentRoot c:/home/webpub/www.mydomain.com/
ServerName mydomain.com
ServerAlias *.mydomain.com
ErrorLog c:/home/logs/www.mydomain.com-error.log
CustomLog c:/home/logs/www.mydomain.com-access.log common
</VirtualHost>

This will tell apache that after directing people to blog.mydomain.com or tech.mydomain.com, everything else should be directed to www.mydomain.com. In this case the * is a wildcard. You could also setup ServerAlias to be ServerAlias *.mydomain.com *.myotherdomain.com which would allow anything from either domain to use the same document root c:/home/webpub/www.mydomain.com/ while tech and blog use a different one.

As you can see, this is a very powerful tool for setting up large numbers of websites, each with their own path on your system.

Configuring Apache 2.0.X on Windows

January 31st, 2008 Apache

In my last article we talked about installing the Apache server on windows. Now let’s talk about configuring it and making use of it.

Now, by default, if you followed my directions before, you should have a directory on your computer called: C:/Program Files/Apache Group/Apache2/htdocs . This is where your current website (www.mydomain.com) lives. Any files you place in here will appear if you go to this site.

A couple of key things to remember before we continue

  1. Configuration changes only take hold after you restart Apache.
  2. You can test a configuration change before you restart Apache by clicking on Start->Programs->Apache HTTP Server 2.0->Configure Apache Server->Test Configuration
  3. There is a link to the main configuration file in Start->Programs->Apache HTTP Server 2.0->Configure Apache Server
  4. You can Stop/Start and Restart Apache easily from the Start->Programs->Apache HTTP Server 2.0->Control Apache Server

Often, you will want to host multiple websites (or subdomains). I have found it easier to break them down by domains and or subdomains and that it’s easier if the web content was in a seperate folder away from Apache.

So the first thing I do is create a directory structure such as this:

C:\home
C:\home\webpub\ (this is where all sites will live)
C:\home\webpub\www.mydomain.com (this is where www.mydomain.com will live)
C:\home\logs\ (this is where your logs will live)

By doing the above we can then easily breakout log files and web content away from the apache directory so future changes won’t require moving files in and out of C:/Program Files/Apache Group/Apache2/htdocs (the default location)

So, now how do we tell Apache to get your (www.mydomain.com) website from the new directory C:\home\webpub\www.mydomain.com?It’s pretty straight forward.

First open the apache configuration file: httpd.conf

Find the line(s) that say C:/Program Files/Apache Group/Apache2/htdocs and replace them with C:\home\webpub\www.mydomain.com . Save the file and restart apache. That was it. Now, any files placed in C:\home\webpub\www.mydomain.com will appear on your site. Easy? Yes, yes it was.

Next, lets change the location of the log file. In apache you have 2 main types of logs. Access logs which will log all visitors coming to the apache server and Error logs which log any type of errors such as an invalid page, coding errors etc. Typically if someone tries to go to http://www.mydomain.com/madeuppage.html and madeuppage.html doesn’t exist, well then that error will get logged to the Error log.

So, for ease of use, let’s stick the access and error logs into c:\home\logs\

Open youre httpd.conf file and find the lines that say:

ErrorLog logs/error.log
CustomLog logs/access.log common

And change them to:

ErrorLog c:/home/logs/mydomain.com-error.log
CustomLog c:/home/logs/mydomain.com-access.log combined

This will tell apache that the logs are now called mydomain.com-error.log and mydomain.com-access.log and now live in c:/home/logs . The word combined at the end tell apache that it should use the combined format which logs more information (such as the webbrowser of the visitor). As usual, save and restart apache.

Installing Apache 2.0.X on Windows

January 31st, 2008 Apache

Here’s a quick and dirty out of the box way of getting Apache 2.0.x running on windows.

  1. First, let’s go to http://httpd.apache.org
  2. Next, find the version you want (in this case 2.0.63) and clickdownload
  3. On the download screen you have two choices for Windows. Win32 with SSL or Win32 without SSL. Choose with OpenSSL since you might at one time in the future want to use SSL certificates for encryption.
  4. Ok, since we’re using 2.0.63 click on apache_2.0.63-win32-x86-openssl-0.9.7m.msi and download it
  5. Open the file by double-clicking on it.
  6. ap-inst1.JPG
    Click NEXT
  7. ap-inst2.JPG
    Select”I accept” and click NEXT
  8. ap-inst3.JPG
    Click NEXT
  9. ap-inst4.JPG
    In the server information you will the basics for your site. You can easily change these later. When finished, click NEXT

    • Network domain is the hostname of your site.
    • Server name is the website. Usually www.
    • Administrators email is where email should be directed if there’s a problem or question
    • You can run Apache on ANY port and ANY ip address, but the default port that all webbrowers know about is port 80. So choose this.
  10. ap-inst5.JPG
    Choose typical and click NEXT
  11. ap-inst6.JPG
    Click NEXT
  12. ap-inst7.JPG
    Click INSTALL
  13. ap-inst8.JPG
    Click FINISH

And that’s it! You’ve installed Apache! Congrats. Now, open your webbrowser and test it out by going to: http://localhost

ap-inst9.JPG

Why Apache webserver?

January 31st, 2008 Apache, General

Even if you’re running windows as your hosting platform, I’ve always like Apache as webserver for the pure reason that it’s incredibly flexible and powerful. Granted IIS is as well but I’ve spent the majority of my time on configuring and stabilizing Apache so that’s what I will focus on in these articles. Also if you learn to configure and work with Apache, you will have an easier time in the future if you switch to a non-windows platform such as RedHat Linux.

Apache is a key ingredient in configuring your hosting platform.  It will serve up pages from your domains and will allow you to build dynamic sites.  Out of the box it will let you password protect directories, setup virtual hosts, specify multiple paths, etc. There are hundreds of plugins for it allowing for different languages such as Java/PHP/Python/Perl and even .NET applications to run on it. You can use Apache as proxy for web surfing as well. So, that’s why in a nutshell I lean toward Apache as a webserver.

The importance of DNS -part 2

January 29th, 2008 Misc Software

So now you understand what DNS is. How do you use it?

The first thing you need to do for each domain is to put in the correct name server at the registrar (ex. godaddy).

If you are using your own DNS servers then the name server will probably be ns1.mydomain.com and ns2.mydomain.com where mydomain is the domain you registered. If you are going to be running your own DNS servers then I recommend at this point to contact your registrar and let them know you will be running you’re own DNS servers. They will probably ask you what your static ip will be. This will be given to you by your colocation facility or isp that you are using. You CANNOT run your own dns servers on a cablemodem/dialup or anything that your ip changes on, EVER. You need a staic ip that changes very rarely (once every few years if possible)

If you use a free DNS service, then you would input the servers used by that services (ex. ns1.zoneedit.com ns5.zoneedit.com).

Now that you have setup the name servers fo your domain, it’s time to learn some basic DNS type records. We’re going use mydomain.com as an example.

mydomain.com is the official hostname. Not www.mydomain.com, and not anything else. The prefix www. or ftp. or mail. or blog. are all just subdomains.

Below are a few of the most common dns record types:

  • A
  • CNAMES
  • MX

A records are the most common and typically point to the ip address of the location.

example 1: You want mydomain.com to point to 192.168.1.2

example 2: You want www.mydomain.com to point to 192.168.1.2

You can assign multiple A records to the same ip address or point different records to different ips. For example dev.mydomain.com might point to a different ip on a different machine since it will be for development work

CNAME records can be treated like A records except they can also forward to another address. So dev.mydomain.com might actually forward to www.google.com

If you will not be forwarding to anything else, then I suggest using A name records since it will save on DNS queries.

MX records. These records tell mail servers where to deliver mail to.

example: You want to run a mail server on ip 192.168.1.2

  1. Setup the mail address A record (mail.mydomain.com) to point to 192.168.1.2
  2. Setup the MX record with to point to mail.mydomain.com

The idea behind MX records is that you could have several mail servers running on different ips that could receive mail for your domain. Each MX record can have a weight which will determine in order which mail server to try if one in unreachable.

Another good example is that you might want to use someone else’s mail server if you do not wish to run your own. An example is the google email system.