Apache problem: all virtual hosts point to same document root
Linux - ServerThis forum is for the discussion of Linux Software used in a server related context.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
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
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
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>
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.
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.
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
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!
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.
*
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:
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
Quote:
IP_address canonical_hostname [aliases...]
Oh, so those were actually interpreted as aliases. OK, moved them to separate lines like this:
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!
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.