Jump to content

How does Apache know what website to pull up?


HoibE

Recommended Posts

Hello - first time poster.  I have a /localhost/ set up to test a couple of websites I'm trying to work on and learn from.  The first one is all set - it's a WordPress blog.  I had to do some real hard digging to get it to work because one directive was slightly out of tune.  Anyway, I need to work on a 2nd one, one that has a much different mission/purpose.

 

I need to find out how to amend httpd.conf so that the server can look at 2 different websites (or maybe more) and handle more than just the one. 

 

I don't know if httpd.com can contain two <Document Roots> because to date, the second one isn't coming up.  I don't think you're allowed to have 2 <Document Roots> in the .conf file.  And again, when I invoke http://localhost/ what I get is the WordPress site.  I don't know how to fix it so that the second website is attended by Apache - if that's the right term.

 

Anyone have a direction I can go in?

 

Thanks.

Link to comment
Share on other sites

Each website in apache is defined by a virtual host container.  You can simply copy and paste the first one to make a second. Then modify the directive accordingly. You may also want to update the machine's host file. On linux this is /etc/hosts usually.  Doing this will also you to create test domains.  If you made that file look like this.

localhost      127.0.0.1
website1      127.0.0.1
website2      127.0.0.0

 

You would be able to browse to

http://localhost/

http://website1/

http://website2/

 

In apache the directive ServerName can be set to website1 in the first container and website2 in the second to make each site serve respectively.

 

Link to comment
Share on other sites

Excellent - and some things I would've had a hard time finding (I think).  I should have said my platform is WindowsXP but Dave, I assume the hosts file in Win looks about the same.  I get the idea from your statement of "may" that modifying the hosts file isn't mandatory.

 

I think I need more information.  Because I really don't have the concepts firmly in hand (like "host containers")  I may try to get over to B&N today and try to pick up a SAMS book on this.  Good idea?  Bad idea?

Link to comment
Share on other sites

I am always and advocate for good reference material. Reason being it takes a long time to sift through all the junk that's out on the internet. A good book puts all the information you need in one place. 

 

About the hosts file.  windows has the same file.  It's purpose on both windows and linux is to resolve a domain name locally by storing the ip address and preventing a DNS look up. 

 

To create 2 distinctly separated websites in apache with only one ip address you'll need to using what is called name based hosting.

 

Just before the first virtual host contain you would need to add this.

 

NameVirtualHost 127.0.0.1

 

Name based hosting relies on the requesting domain name to pull up the correct site.  The bottom of your httpd.conf file usually where the definitions for each website is stored. I say usually because often apache will be figured with include files to separate the vhosts from the config sections. Right now you most like only have one virtual host contain. It would like like this:

 

<VirtualHost 127.0.0.1:80>
    ServerAdmin webmaster@dummy-host2.example.com
    DocumentRoot /www/docs/dummy-host2.example.com
    ServerName dummy-host2.example.com
    ErrorLog @rel_logfiledir@/dummy-host2.example.com-error_log
    CustomLog @rel_logfiledir@/dummy-host2.example.com-access_log common
</VirtualHost>

 

To make a second one just copy and paste the existing one then change the DocumentRoot and ServerName data. Note the NameVirtualHost is the same as the ip found in the opening tag of each Virtual Host containers.

 

Once you have 2 sites apache needs some way to tell the difference between the two.  This is where using different domain names is important and to create domain names without registering them and without configuring DNS you need to edit your host file. On windows it is usually located here:

 

C:\WINDOWS\system32\drivers\etc\hosts

 

The ServerName field needs to match a domain listing in your host file. This is how apache knows which container to use.

 

One last thing.  You have to restart apache before changes to the config file take place.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.