Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

Apache newb here. I've read a few tutorials and I'm not sure what I'm doing wrong.

I have my default site enabled, and that one works fine:

<VirtualHost *:80>
    ServerAdmin webmaster@localhost

    DocumentRoot /var/www
    <Directory />
        Options FollowSymLinks
        AllowOverride None
    </Directory>
    <Directory /var/www/>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride None
        Order allow,deny
        allow from all
    </Directory>

    ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
    <Directory "/usr/lib/cgi-bin">
        AllowOverride None
        Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
        Order allow,deny
        Allow from all
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/error.log

    # Possible values include: debug, info, notice, warn, error, crit,
    # alert, emerg.
    LogLevel warn

    CustomLog ${APACHE_LOG_DIR}/access.log combined

    Alias /doc/ "/usr/share/doc/"
    <Directory "/usr/share/doc/">
        Options Indexes MultiViews FollowSymLinks
        AllowOverride None
        Order deny,allow
        Deny from all
        Allow from 127.0.0.0/255.0.0.0 ::1/128
    </Directory>

</VirtualHost>

So I wanted to add a second site, www.example.com. I updated my hosts file:

192.168.1.148   sam-NV53    # Added by NetworkManager
127.0.0.1   localhost.localdomain   localhost
::1 sam-NV53    localhost6.localdomain6 localhost6
127.0.1.1   sam-NV53
www.example.com localhost.localdomain localhost

# The following lines are desirable for IPv6 capable hosts
::1     localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
ff02::3 ip6-allhosts

And I defined my new website

<VirtualHost *>
    ServerAdmin sam@foo.com
    ServerName www.example.com
    ServerAlias example.com

    DirectoryIndex index.html
    DocumentRoot /var/www/
</VirtualHost>

Since the DocumentRoot for my new site is the same as the one for my default site, I'd expect to see the index.html from my default site. But instead, I see the example.com from the web. What am I missing here?

Edit

Apparently nobody has noticed that my entry in my hosts file is backwards. I changed it to:

127.0.0.1 geekspeak.dev

Now it works. Thanks everyone for your input :)

share|improve this question

migrated from stackoverflow.com Sep 11 '11 at 2:35

This question came from our site for professional and enthusiast programmers.

    
Thanks for the tip Jim. –  Samo Sep 11 '11 at 2:27
add comment

4 Answers

Try using something other than example.com, www.example.com, or other derivatives. Those names are reserved by IANA for use in documentation. I used my own Apache server to try configuring a virtual host at www.example.com, and I ALWAYS got redirected to the IANA page, but then I changed it to www.someserver.dev and everything was fine.

Best of luck

share|improve this answer
    
Thanks for the tip. Unfortunately, that didn't do the trick. Can you think of anything I might be doing wrong? –  Samo Sep 11 '11 at 2:33
    
@Samo your /etc/hosts looks fishy. Is 127.0.1.1 an actual IP of a machine on your network? Also, as 127.0.0.1 is associated with your machine's loopback network adapter, you may not get the behavior you wanted. I suggest you change 127.0.1.1 to your machine's IP address (most likely 192.168.1.148 according to your posted /etc/hosts). –  rjacks Sep 11 '11 at 3:28
    
@Samo, also, as @uesp suggested, having the document root on both hosts set to the same directory might be causing an issue. Why don't you point the second host at /var/www/test and set up a trivial index.html in that directory? It will certainly make debugging a bit easier –  rjacks Sep 11 '11 at 3:35
add comment

A few things you can try:

  1. As rjacks mentioned, don't use example.com for your site's name.
  2. Don't add an entry to your hosts file. This may be useful in some cases but I don't see how it is needed in this case.
  3. Update your DNS to point the new server name to your web server's IP. For example, if you own somesite.com you can freely create a sub-domain test.somesite.com or any number of other ones as desired.
  4. Make sure you restart Apache after any changes to its configuration file.
  5. I would serve the new web site to another directory than the default site, at least temporarily, in order to confirm which VirtualHosts was actually matching and serving it.
share|improve this answer
    
Editing /etc/hosts can be useful for people like me who are just playing around with a server in their house before purchasing a domain, hosting, etc. For example, I edited /etc/hosts on my laptop to point www.mysite.dev to my development server on my home LAN. I assume the poster is in a similar position. –  rjacks Sep 11 '11 at 3:31
add comment

whatever is in your

<VirtualHost>

must match with what you have listed for

 NameVirtualHost

So if you have

 NameVirtualHost *:80

All your virtualhost entries for port 80 must look like

<VirtualHost *:80>

So switch * to *:80 like is in your first example and it should go to the correct place

share|improve this answer
add comment

I've tried this using virtualbox. Host OS - Win7, Guest OS - Ubuntu Server.

1) create www.yourdomain.com in /etc/apache2/sites-available/

<virtualhost *:80> # i recommend adding your interface IP here
    ServerAdmin webmaster@yourdomain.com
    ServerName yourdomain.com
    ServerAlias www.yourdomain.com
    DirectoryIndex index.html
    DocumentRoot /var/www/main/htdocs
    LogLevel warn
    ErrorLog /var/www/main/logs/error.log
    CustomLog /var/www/main/logs/access.log combined
</virtualhost>

2)create directories inside /var/www

mkdir -p /var/www/main/htdocs
mkdir -p /var/www/main/logs

modify permissions for those directories

3) enable site in apache2

a2ensite www.yourdomain.com

4) edit /etc/apache2/sites-available/default and remove all lines between DocumentRoot and the last line.

5) restart or reload apache2

6) edit your hosts file www.yourdomain.com then try it out.

This worked on my test server.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.