Why does apache main site default not get recognized in httpd.conf

I have an apache server with the following settings:

ServerName trekfederation.com:80
DocumentRoot "/var/www/html/trekfed"
<Directory />
    Options FollowSymLinks
    AllowOverride None
</Directory>
<Directory "/var/www/html/trekfed">
    Options Indexes FollowSymLinks
    AllowOverride All
    Order allow,deny
    Allow from all
</Directory>

Open in new window

Later on, I have Virtual Hosts:

NameVirtualHost *:80

Open in new window


<VirtualHost *:80>
    ServerName ss51.trekfederation.com
        RedirectPermanent / http://spacestation51.wordpress.com/
</VirtualHost>

<VirtualHost *:80>
ServerName supportchat.trekfederation.com
DocumentRoot /var/www/html/livezilla
<Directory /var/www/html/livezilla>
      AllowOverride All
       Allow from all
</Directory>
</VirtualHost>


<VirtualHost *:80>
ServerName promenade.trekfederation.com
ServerAlias promenade.trekfederation.net
DocumentRoot /var/www/html/promenade
<Directory /var/www/html/promenade>
      AllowOverride All
       Allow from all
</Directory>
</VirtualHost>


<VirtualHost *:80>
ServerName ussgeorgewashington.trekfederation.com
ServerAlias ussgeorgewashington.trekfederation.net
ServerAlias nfc1773a.trekfederation.com
ServerAlias nfc1773a.trekfederation.net
DocumentRoot /var/www/html/NOVA/USS_GEORGE_WASHINGTON
<Directory /var/www/html/NOVA/USS_GEORGE_WASHINGTON>
      AllowOverride All
       Allow from all
</Directory>
</VirtualHost>


<VirtualHost *:80>
ServerName cargobay.trekfederation.com
ServerAlias cargobay.trekfederation.net
DocumentRoot /var/www/html/cargobay
<Directory /var/www/html/cargobay>
    Options Indexes FollowSymLinks MultiViews
    DirectoryIndex index.php
    DirectoryIndex index.html
    AllowOverride All
    Order allow,deny
    allow from all
</Directory>
</VirtualHost>

Open in new window

Inside
/var/www/html/trekfed

Open in new window

sites a wordpress installation. it has a .htaccess file so AllowOverride is set to all.

My problem is my virtual hosts are overriding the primary settings. If I go to the initial site http://trekfederation.com or http://www.trekfederation.com it bypasses the server settings and goes to the ss51 pages.

I have also tried to create a Virtual host:

<VirtualHost *:80>
ServerName www.trekfederation.com
ServerAlias www.trekfederation.net
DocumentRoot /var/www/html/trekfed
<Directory /var/www/html/trekfed>
      AllowOverride All
       Allow from all
</Directory>
</VirtualHost>

Open in new window

and put it at the top of the list, and this does not work either. It just keeps going to the next list.

How do I lock it down so this works? Thanks much.
LVL 9
Evan CutlerVolunteer Chief Information OfficerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

stgreenwaltCommented:
Here is a possibility. When I use VirtualHost, it looks more like this:

<VirtualHost *:80>
    # The default location for all requests, any domain, port 80.
</VirtualHost>
<VirtualHost subdomain1.mydomain.com:80>
    # The location of subdomain1 for mydomain.com. A folder, a hard redirect, or whatever.
</VirtualHost>
<VirtualHost subdomain2.mydomain.com:80>
    # The location of subdomain2 for mydomain.com. A folder, a hard redirect, or whatever.
</VirtualHost>

Open in new window


Maybe your repeated "*:80" VirtualHost elements are overriding each other, because they are each respecify the default handling of all http traffic on port 80.  Just a guess.
stgreenwaltCommented:
Also, if you have the latest version of apache, I think that

"NameVirtualHost *:80"

is no longer required.
Evan CutlerVolunteer Chief Information OfficerAuthor Commented:
Hi, Thanks much for the options,
but when I changed my virtual hosts to:
<VirtualHost *:80>
ServerAdmin cheng@trekfederation.com
ServerName www.trekfederation.com
ServerAlias trekfederation.com
ServerAlias trekfederation.net
DocumentRoot /var/www/html/trekfed
<Directory /var/www/html/trekfed>
    Options Indexes +FollowSymLinks +SymLinksIfOwnerMatch MultiViews
    DirectoryIndex index.php
    DirectoryIndex index.html
    AllowOverride All
    Order allow,deny
    allow from all
</Directory>
ErrorLog /var/log/trekfederror.log
</VirtualHost>


<VirtualHost supportchat.trekfederation.com:80>
ServerName supportchat.trekfederation.com
DocumentRoot /var/www/html/livezilla
<Directory /var/www/html/livezilla>
      AllowOverride All
       Allow from all
</Directory>
</VirtualHost>


<VirtualHost promenade.trekfederation.com:80>
ServerName promenade.trekfederation.com
ServerAlias promenade.trekfederation.net
DocumentRoot /var/www/html/promenade
<Directory /var/www/html/promenade>
      AllowOverride All
       Allow from all
</Directory>
</VirtualHost>


<VirtualHost ussgeorgewashington.trekfederation.com:80>
ServerName ussgeorgewashington.trekfederation.com
ServerAlias ussgeorgewashington.trekfederation.net
ServerAlias nfc1773a.trekfederation.com
ServerAlias nfc1773a.trekfederation.net
DocumentRoot /var/www/html/NOVA/USS_GEORGE_WASHINGTON
<Directory /var/www/html/NOVA/USS_GEORGE_WASHINGTON>
      AllowOverride All
       Allow from all
</Directory>
</VirtualHost>

Open in new window


and commented out: "NameVirtualHost *:80"

I started getting these errors:
[root@aurora2 trekfed]# service httpd restart
Stopping httpd:                                            [  OK  ]
Starting httpd: [Fri Jul 10 19:47:22 2015] [warn] VirtualHost cargobay.trekfederation.com:80 overlaps with VirtualHost ss51.trekfederation.com:80, the first has precedence, perhaps you need a NameVirtualHost directive
[Fri Jul 10 19:47:22 2015] [warn] VirtualHost ussgeorgewashington.trekfederation.com:80 overlaps with VirtualHost cargobay.trekfederation.com:80, the first has precedence, perhaps you need a NameVirtualHost directive
[Fri Jul 10 19:47:22 2015] [warn] VirtualHost promenade.trekfederation.com:80 overlaps with VirtualHost ussgeorgewashington.trekfederation.com:80, the first has precedence, perhaps you need a NameVirtualHost directive
[Fri Jul 10 19:47:22 2015] [warn] VirtualHost supportchat.trekfederation.com:80 overlaps with VirtualHost promenade.trekfederation.com:80, the first has precedence, perhaps you need a NameVirtualHost directive
                                                           [  OK  ]

Open in new window


I added the hostnames to /etc/hosts, but it's not doing anything.  What should I do now?
Thanks much.
Active Protection takes the fight to cryptojacking

While there were several headline-grabbing ransomware attacks during in 2017, another big threat started appearing at the same time that didn’t get the same coverage – illicit cryptomining.

Evan CutlerVolunteer Chief Information OfficerAuthor Commented:
of note, I'm using apache 2.2.15.
stgreenwaltCommented:
Ignore what I said about no needed the NameVirtualHost directive.  Memory lapse.  Also, revert back to your original configuration to that we know our starting point for the following.

I think I know the problem.  But first, let's check the organization of some things.  Some of this is just personal preference.

First, I want to make sure that all of your VirtualHost directives are in a file at /etc/apache2/sites-available.  The filename I usually use is "000.default" or similar.  Then you can use command "a2ensite 000.default" to activate all the virtual hosts in "000.default", or "a2dissite 000.default" to deactivate them.

Next, move "NameVirtualHost *:80" to to be the first line in "000.default".  Also make sure it does not exist anywhere else.

Now for the most important thing, and probably the source of your problem.  The very first VirtualHost directive that appears in "000.default" becomes the global default for your entire server for all IP addresses that match what is in the NameVirtualHost directive.  In your case, that is all IP addresses assigned to your NIC on port 80.

The very first VirtualHost directive even overrides the free-floating ServerName and Directory statements that you have at the first two lines of httpd.conf.  Those lines should be put into their own VirtualHost block inside "000.default".

Whenever the server cannot exactly match the host name being requested on the given IP address and protocol, it will always deliver whatever is defined in the very first VirtualHost statement in "000.default".  Right now,  your first VirtualHost statement is the one referencing "ss51.trekfederation.com" which is why things are defaulting to that.

I think that your other VirtualHost blocks are being ignored (not matched) because they contain Directory blocks that fall after ServerName and DocumentRoot.  My bet is that the Directory blocks must come first.

Even better, move Directory blocks to httpd.conf to simplify things when possible.  You seem to have repetitive Directory blocks that could be handled by just one Directory block in httpd.conf like this:
<code>
  <Directory /var/www/html>
    AllowOverride All
    Allow from all
  </Directory>
</code>
(But this is just a preference issue).

If this response does not help, then zip up all of /etc/apache2 and upload it.  My server is doing much the same as yours, and I can compare to what I have.  Good luck.
Evan CutlerVolunteer Chief Information OfficerAuthor Commented:
Thanks so much for looking at this:
I'm running centos 6, not any of the other linux distros.  To that end, I don't have e2ensite.

In addition, all of virtual https are in the bottom of my httpd.conf file.

and for the record, here's my .htaccess for the primary site:


Options +FollowSymLinks
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

Open in new window



and all of my sites are subfolders of /var/www/html

Thanks again.
httpd.txt
stgreenwaltCommented:
It is ok to have your "NameVirtualHost *:80", "VirtualHost", and even "Directory" directives in httpd.conf if you have everything in the right order.

1.

"NameVirtualHost *:80" must appear ahead of all your "VirtualHost" and "Directory" directives

2.

"ServerName", "DocumentRoot", "ServerAlias", etc. must not appear outside of a "VirtualHost" block because the very first "VirtualHost" in httpd.conf will become the default for your server, and that first "VirtualHost" will override anything not in a "VirtualHost" block.

3.

If you have a "Directory" directive inside a "VirtualHost" block, it should appear ahead of "ServerName", "DocumentRoot", "ServerAlias" and all other sections.

4.

If you can, try to get your "Directory" directives out of your "VirtualHost" blocks, and instead consolidate them into standalone "Directory" blocks appearing just under "NameVirtualHost *:80"
I am fairly sure your problem will go away if you do the above.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
gheistCommented:
There are no shoulds and maybes.
Once NameVirtualHost gets configured the first defined VirtualHost is default site for that listener (i.e IP:port)
stgreenwaltCommented:
That's what I have been saying.  His first defined VirtualHost is overriding earlier stated ServerName, DocumentRoot, etc. directives that appear outside of a VirtualHost directive.
gheistCommented:
No - nothing is overriden - old global names apply to all listeners that are not NameVirtualHost enabled....
Evan CutlerVolunteer Chief Information OfficerAuthor Commented:
I'm sorry, thought I finished this.
I used your rules and re-wrote httpd.conf from scratch.
I think I had some lines screwed up and it was failing.
I did it from scratch.

Thanks for the rules.  They helped me set it up.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Apache Web Server

From novice to tech pro — start learning today.