LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices

Reply
 
Search this Thread
Old 06-02-2010, 09:29 PM   #1
jsepia
LQ Newbie
 
Registered: Oct 2009
Posts: 14

Rep: Reputation: 1
Exclamation Apache problem: all virtual hosts point to same document root


I'm using CentOS 5.3. I want to run two virtualhosts, each pointing to its own folder in my home directory:
  • domain.dev
  • intranet.domain.dev

However, when trying to access any of them, I only get the files located in my /var/www/html directory (which is my DocumentRoot). I can access those files through http://localhost/ too.

Both domain names are in my /etc/hosts. I have configured the virtual hosts like this:

Code:
NameVirtualHost *:80

<VirtualHost domain.dev:80>
    DocumentRoot "/home/username/Dropbox/domain.net"
    ServerName domain.dev
    ServerAlias www.domain.dev
    ErrorLog error_log
    <Directory "/home/username/Dropbox/domain.net">
        Allow from all
    </Directory>
</VirtualHost>

<VirtualHost intranet.domain.dev:80>
    DocumentRoot "/home/username/Dropbox/intranet.domain.net"
    ServerName intranet.domain.dev
    ErrorLog error_log
    <Directory "/home/username/Dropbox/intranet.domain.net">
        Allow from all
    </Directory>
</VirtualHost>
These directories have 775 permissions and belong to the apache group (to which I added my own user account). I don't see any related errors in the error/access logs but I could post them if needed.

Thanks in advance!

Edit: I tried removing these two domain names from my /etc/hosts, but then they yielded 404s.

Last edited by jsepia; 06-02-2010 at 09:32 PM. Reason: Last minute addition
 
Old 06-02-2010, 11:02 PM   #2
smoker
Senior Member
 
Registered: Oct 2004
Distribution: Fedora Core 4, 12, 13, 14, 15, 17
Posts: 2,279

Rep: Reputation: 248Reputation: 248Reputation: 248
Quote:
Originally Posted by jsepia View Post

Code:
NameVirtualHost *:80

<VirtualHost domain.dev:80>
    DocumentRoot "/home/username/Dropbox/domain.net"
    ServerName domain.dev
    ServerAlias www.domain.dev
    ErrorLog error_log
    <Directory "/home/username/Dropbox/domain.net">
        Allow from all
    </Directory>
</VirtualHost>

<VirtualHost intranet.domain.dev:80>
    DocumentRoot "/home/username/Dropbox/intranet.domain.net"
    ServerName intranet.domain.dev
    ErrorLog error_log
    <Directory "/home/username/Dropbox/intranet.domain.net">
        Allow from all
    </Directory>
</VirtualHost>
This is wrong. They should be :

Code:
<VirtualHost *:80>
    ServerName www.domain.dev
    ServerAlias domain.dev
    DocumentRoot /home/username/Dropbox/domain.dev/html
        
    <Directory /home/username/Dropbox/domain.dev/html/>
        Allow from all
    </Directory>

    SetEnv SITE_ROOT /home/username/Dropbox/domain.dev
    SetEnv SITE_HTMLROOT /home/username/Dropbox/domain.dev/html
</VirtualHost>

<VirtualHost *:80>
    ServerName intranet.domain.dev
    ServerAlias intranet
    DocumentRoot /home/username/Dropbox/intranet.domain.dev/html
        
    <Directory /home/username/Dropbox/intranet.domain.dev/html/>
        Allow from all
    </Directory>

    SetEnv SITE_ROOT /home/username/Dropbox/intranet.domain.dev
    SetEnv SITE_HTMLROOT /home/username/Dropbox/intranet.domain.dev/html
</VirtualHost>
The files don't have to be owned by apache, as this makes editing a pain.
 
Old 06-02-2010, 11:50 PM   #3
jsepia
LQ Newbie
 
Registered: Oct 2009
Posts: 14

Original Poster
Rep: Reputation: 1
Thanks. I changed my settings to reflect your example as closely as possible, down to the slashes and the html subdirectory. I also fixed the .dev / .net difference and learned about SetEnv. Then I restarted httpd, but I'm still having the same problem. And I found out that www.domain.dev is not accessible (probably because it's not in /etc/hosts?)

I'm still trying variations, though. By the way, what purpose does the html directory serve? Does Apache recognize it somehow or is it just good practice?

And the files are not owned by apache, they are owned by me as part of the
apache group.

Code:
drwxrwxr-x 3 username apache 4096 Jun  3 06:28 domain.dev
drwxrwxr-x 3 username apache 4096 Jun  3 06:30 intranet.domain.dev
The best of both worlds, I guess.
 
Old 06-03-2010, 11:06 AM   #4
smoker
Senior Member
 
Registered: Oct 2004
Distribution: Fedora Core 4, 12, 13, 14, 15, 17
Posts: 2,279

Rep: Reputation: 248Reputation: 248Reputation: 248
You don't strictly need an html directory but it is good practice. If you later want a cgi-bin, then that is put at the same level as the html directory for security reasons. That is why there are seperate SITE_ROOT and SITE_HTMLROOT in httpd.conf

You site root is the directory holding all the domains components (including the html directory) and the html root is where all pages are served from.

But this is working from apache 2.0.x not apache 2.2, so I don't know what your version is.

Also in the <Directory> directive, it is usual to have some more rules.
i.e.
Code:
<Directory /home/username/Dropbox/intranet.domain.dev/html/>
        Allow from all
        AllowOverride All
        Order allow,deny
</Directory>
Visit apache and read the docs for your version.


Even if you own an actual domain, you need 2 entries in /etc/hosts if you want to access these sites. This is because you can't access your own machine using an external ip address, by going through your own router. I have all mine set up using the LAN ip of my machine,
i.e.

Code:
192.168.1.35       intranet.domain.dev
192.168.1.35       domain.dev
My sites are still accessible from outside the LAN using the domain names as well. You have to set the router up to forward all http requests to the correct machine, and apache takes care of serving the correct site, according to the domain/site requested.

Last edited by smoker; 06-03-2010 at 11:22 AM.
 
Old 06-03-2010, 11:07 PM   #5
jsepia
LQ Newbie
 
Registered: Oct 2009
Posts: 14

Original Poster
Rep: Reputation: 1
Quote:
Originally Posted by smoker View Post
Visit apache and read the docs for your version.
Thanks, but I already tried doing what the documentation says, to no avail. I guess the problem must be something else.

Quote:
Originally Posted by smoker View Post
Even if you own an actual domain, you need 2 entries in /etc/hosts if you want to access these sites.
Whether I put them as separate entries or on the same line in /etc/hosts doesn't seem to make a difference, if that's what you mean. ):

Code:
127.0.0.1 localhost domain.dev intranet.domain.dev www.domain.dev
Quote:
Originally Posted by smoker View Post
You have to set the router up to forward all http requests to the correct machine, and apache takes care of serving the correct site, according to the domain/site requested.
The domains definitely direct to the correct machine. Just the wrong directory.

Last edited by jsepia; 06-03-2010 at 11:08 PM. Reason: Added link to the appropriate documentation article
 
Old 06-04-2010, 11:46 AM   #6
smoker
Senior Member
 
Registered: Oct 2004
Distribution: Fedora Core 4, 12, 13, 14, 15, 17
Posts: 2,279

Rep: Reputation: 248Reputation: 248Reputation: 248
That line from /etc/hosts is garbage.

each host should be on a seperate line with the ip address at the beginning.

Judging by your original post you didn't follow the documentation at all. The link you just gave looks nothing like your configuration.

Last edited by smoker; 06-04-2010 at 11:48 AM.
 
Old 06-05-2010, 12:51 AM   #7
jabaker
LQ Newbie
 
Registered: Jun 2010
Distribution: Ubuntu, (well... primarily)
Posts: 3

Rep: Reputation: 0
hosts, quotes, slashes, and restarts

Quote:
Originally Posted by smoker View Post
That line from /etc/hosts is garbage.

each host should be on a seperate line with the ip address at the beginning.
@smoker
Don't be so quick to call it garbage, smoker... 'Cause you in turn are wrong. His hosts file is perfectly fine. I have hosts files on many machines with multiple names per line, just like his, and they work just swimmingly.

@jsepia
Moving on, however, I'd note that I've seen problems with quoting of paths give problems before. Have you made sure to quote the directory paths in the Directory value, but not in the DocumentRoot directive? I believe that is the canonical way to list them. (Although, quotes may work in both, I believe in following the documentation very closely when you have problems like yours. ;-) ) -- And don't forget either that DocumentRoot DOES NOT use trailing slashes, but Directory can. ... In point of fact, I believe Directory can use quotes or not, but I believe DocumentRoot may actually interpret them as part of the path, if I'm not mistaken.

And while you're at it, make sure none of the path segments of the Directory path are symlinks. I believe that Apache still doesn't accept that.

Also, it may be obvious or it may not, but you have been restarting Apache with "stop/start", and not with "graceful", yes? The "graceful" method first checks the config and only restarts if things look good to apachectl -- silently ignoring the restart if not. Really, "graceful" is there for when you don't want server downtime accidentally on a production server... in development when making config changes, I've actually had it bite me more often than any downtime in a dev system ever would. ... Thinking you've restarted Apache 38 times in a row only to learn you'd introduced a config error right at the start and it NEVER went down is always a head-slapping, wall-banging, desk-pounding moment for me. ;-)

For production, graceful is great. For dev... ehh.

Just be sure Apache is REALLY restarting. -- Obviously, you *should* in theory be able to check using "apachectl configtest" to see if it will choose to restart when told to. But even if it doesn't report any errors, try the full "stop"/"start" arguments anyway. And then check that all the apache/httpd PID's have actually changed if you really want to make sure. -- 'Cause I have once or twice seen Apache refuse to restart (for completely unknown reasons) even with a clean config unless I actually stopped it cold and made sure it was actually dead. (Pretty rarely, but... worth checking.)

Next, or maybe first, check your logs to see if you are actually getting the expected Host headers in your requests. And if your logs don't include the Host field ("%v" or "%{Host}i" depending on what exactly you want, probably the latter), then change your config to include it, 'cause you probably will want it anyway if you ever need to parse your logs based on site name. -- Or if you aren't logging to begin with, since I don't see a logging directive except ErrorLog, then turn access logs on for goodness sakes. You'll be thankful, I promise!

Anyway, doing that will allow you see what host Apache is being asked to serve data from. After all, your browser might not be using the correct name, or if it's old enough (VERY old!!) might not even use the header. (That's almost unheard of these days though. But it could *easily* be sending something you aren't expecting.)

Try those, and see if any of them make a difference. Good luck!

-James
 
Old 06-05-2010, 06:40 AM   #8
smoker
Senior Member
 
Registered: Oct 2004
Distribution: Fedora Core 4, 12, 13, 14, 15, 17
Posts: 2,279

Rep: Reputation: 248Reputation: 248Reputation: 248
@jabaker

Quote:
/etc/hosts

Each node in the /etc/hosts file has a one line entry. Each entry in the file must be in the following form:
Syntax

IP_address host_name [alias]...

Parameters

IP_address

The IP address that uniquely identifies the node. IP_address must be in internet "dot" notation. Refer to
Chapter 6 “ Network Addressing” for details on IP addresses.
host_name

Name of the node. Host names can contain any printable character except spaces, newline, or the comment character (#). Naming Convention: the first nine characters should be unique for each network host.
alias

Common name or names for the node. An alias is a substitute for host_name. Alias names are optional. Naming Convention: the first nine characters should be unique for each network host.

/etc/hosts Format

When creating the /etc/hosts file, follow these rules:

*

Lines cannot start with a blank or tab character.
*

Fields can have any number of blanks or tab characters separating them.
*

Comments are allowed and designated by a pound sign (#) preceding the comment text.
*

Trailing blank and tab characters are allowed.
*

Blank line entries are allowed.
*

Only one host entry per line is allowed.
http://docs.hp.com/en/B2355-90748/ch03s06.html

Quote:
This manual page describes the format of the /etc/hosts file. This file
is a simple text file that associates IP addresses with hostnames, one
line per IP address. For each host a single line should be present with
the following information:


IP_address canonical_hostname [aliases...]
http://unixhelp.ed.ac.uk/CGI/man-cgi?hosts

Last edited by smoker; 06-05-2010 at 06:44 AM.
 
Old 06-06-2010, 03:38 PM   #9
jsepia
LQ Newbie
 
Registered: Oct 2009
Posts: 14

Original Poster
Rep: Reputation: 1
Quote:
Originally Posted by jabaker View Post
Also, it may be obvious or it may not, but you have been restarting Apache with "stop/start", and not with "graceful", yes? The "graceful" method first checks the config and only restarts if things look good to apachectl -- silently ignoring the restart if not. Really, "graceful" is there for when you don't want server downtime accidentally on a production server... in development when making config changes, I've actually had it bite me more often than any downtime in a dev system ever would. ... Thinking you've restarted Apache 38 times in a row only to learn you'd introduced a config error right at the start and it NEVER went down is always a head-slapping, wall-banging, desk-pounding moment for me. ;-)
Wow, I didn't know that. Huge difference. Didn't solve my problem, but it's still nice to know.

Quote:
Originally Posted by smoker View Post
Quote:
IP_address canonical_hostname [aliases...]
Oh, so those were actually interpreted as aliases. OK, moved them to separate lines like this:

Code:
127.0.0.1 localhost
127.0.0.1 domain.dev www.domain.dev
127.0.0.1 intranet.domain.dev
Then restarted my machine. No difference. ):

Quote:
Originally Posted by jabaker View Post
Next, or maybe first, check your logs to see if you are actually getting the expected Host headers in your requests. And if your logs don't include the Host field ("%v" or "%{Host}i" depending on what exactly you want, probably the latter), then change your config to include it, 'cause you probably will want it anyway if you ever need to parse your logs based on site name. -- Or if you aren't logging to begin with, since I don't see a logging directive except ErrorLog, then turn access logs on for goodness sakes. You'll be thankful, I promise!
OK, here's how my vhosts file looks like now:

Code:
NameVirtualHost *:80

#LogFormat %v %h %l %u %t \"%r\" %>s %b full_vhost
LogFormat %{Host}i %h %l %u %t \"%r\" %>s %b full_vhost

<VirtualHost *:80>
    ServerName www.domain.dev
    ServerAlias domain.dev
    ErrorLog error_log
    CustomLog access_log full_vhost
    <Directory /home/username/Dropbox/domain.dev/html/>
        AllowOverride All
        Order allow,deny
        Allow from all
    </Directory>
    DocumentRoot /home/username/Dropbox/domain.dev/html
    SetEnv SITE_ROOT /home/username/Dropbox/domain.dev
    SetEnv SITE_HTMLROOT /home/username/Dropbox/domain.dev/html
</VirtualHost>

<VirtualHost *:80>
    ServerName intranet.domain.dev
    ServerAlias intranet
    ErrorLog error_log
    CustomLog access_log full_vhost
    <Directory /home/username/Dropbox/intranet.domain.dev/html/>
        AllowOverride All
        Order allow,deny
        Allow from all
    </Directory>
    DocumentRoot /home/username/Dropbox/intranet.domain.dev/html
    SetEnv SITE_ROOT /home/username/Dropbox/intranet.domain.dev
    SetEnv SITE_HTMLROOT /home/username/Dropbox/intranet.domain.dev/html
</VirtualHost>
Where are error_log and access_log supposed to show up when I access the sites? In the SITE_ROOT directory? DocumentRoot? (they're not there)

My /var/log/httpd/error_log shows this, though:

Code:
[Sun Jun 06 22:25:30 2010] [error] [client 127.0.0.1] File does not exist: /var/www/html/favicon.ico, referer: http://www.domain.dev/
[Sun Jun 06 22:25:34 2010] [error] [client 127.0.0.1] File does not exist: /var/www/html/favicon.ico, referer: http://domain.dev/
[Sun Jun 06 22:25:37 2010] [error] [client 127.0.0.1] File does not exist: /var/www/html/favicon.ico, referer: http://intranet.domain.dev/
However the addresses still yield the localhost page.
 
  


Reply

Tags
apache, centos, virtualhosts


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Apache Virtual Host Document Root vertigo12369 Linux - Newbie 4 09-10-2009 08:19 AM
Apache 2.2 - can not access virtual host document root bobnw Linux - Newbie 8 06-11-2009 06:16 PM
Apache virtual hosts problem alienDog Slackware 9 01-30-2008 12:54 PM
Virtual hosts refer to the same document root folder kumu Linux - Software 2 09-04-2004 07:19 PM
Problem with Apache Virtual Hosts jaberwooky Linux - Software 4 09-23-2003 01:37 PM


All times are GMT -5. The time now is 06:37 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration