Apache defaulting to one virtual host instead of root folder of the other vhosts - apache

I have 3 vhosts setup on xampp on my local windows machine. (see config below)
Whenever I direct my browser to stage.local or host.local I get redirected to dev.local which is the vhost I set up first, but shouldn't be the default vhost.
However, when I go to host.local/index.php the corrrect file is shown, somehow it's just the root directory that is messed up.
I'm not sure how this is possible or how to fix this.
The configuration seems correct, here is the full file:
#
# Use name-based virtual hosting.
#
NameVirtualHost *
#
# VirtualHost example:
# Almost any Apache directive may go into a VirtualHost container.
# The first VirtualHost section is used for all requests that do not
# match a ##ServerName or ##ServerAlias in any <VirtualHost> block.
#
<VirtualHost *>
ServerAdmin webmaster#host.local
DocumentRoot "C:/xampp/htdocs/host.local/"
ServerName host.local
</VirtualHost>
<VirtualHost *>
ServerAdmin webmaster#stage.local
DocumentRoot "C:/xampp/htdocs/stage.local/"
ServerName stage.local
ErrorLog "logs/stage.local-error.log"
CustomLog "logs/stage.local-access.log" common
</VirtualHost>
<VirtualHost *>
ServerAdmin webmaster#dev.local
DocumentRoot "C:/xampp/htdocs/dev.local/"
ServerName dev.local
ErrorLog "logs/dev.local-error.log"
CustomLog "logs/dev.local-access.log" common
</VirtualHost>
And httpd -S says this:
*:* is a NameVirtualHost
default server host.local (C:/xampp/apache/conf/extra/httpd-vhosts.conf:29)
port * namevhost host.local (C:/xampp/apache/conf/extra/httpd-vhosts.conf:29)
port * namevhost stage.local (C:/xampp/apache/conf/extra/httpd-vhosts.conf:35)
port * namevhost dev.local (C:/xampp/apache/conf/extra/httpd-vhosts.conf:43)
(some ssl stuff after)
and my hosts file has the following entries:
127.0.0.1 localhost
127.0.0.1 host.local
127.0.0.1 stage.local
127.0.0.1 dev.local

For me it's working. You need to define port for eg:-
#
# Use name-based virtual hosting.
#
NameVirtualHost *:80
#
# VirtualHost example:
# Almost any Apache directive may go into a VirtualHost container.
# The first VirtualHost section is used for all requests that do not
# match a ##ServerName or ##ServerAlias in any <VirtualHost> block.
#
<VirtualHost *:80>
ServerAdmin webmaster#host.local
DocumentRoot "C:/xampp/htdocs/host.local/"
ServerName host.local
</VirtualHost>
<VirtualHost *:80>
ServerAdmin webmaster#stage.local
DocumentRoot "C:/xampp/htdocs/stage.local/"
ServerName stage.local
ErrorLog "logs/stage.local-error.log"
CustomLog "logs/stage.local-access.log" common
</VirtualHost>
<VirtualHost *:80>
ServerAdmin webmaster#dev.local
DocumentRoot "C:/xampp/htdocs/dev.local/"
ServerName dev.local
ErrorLog "logs/dev.local-error.log"
CustomLog "logs/dev.local-access.log" common
</VirtualHost>

Related

ordering VirtualHosts in Apache conf

I'm having issues with my apache configuration. The expected outcome is to allow all traffic to go to the default site trekfederation.com, UNLESS the subdomain is specified and matched otherwise. My problem is that some of my subdomains still default to the main site.
I have two files as part of my configuration:
httpd.conf
ships.conf
here's httpd.conf's portion for virtualhost:
#
# Use name-based virtual hosting.
#
NameVirtualHost *:80
#
# NOTE: NameVirtualHost cannot be used without a port specifier
# (e.g. :80) if mod_ssl is being used, due to the nature of the
# SSL protocol.
#
#
# VirtualHost example:
# Almost any Apache directive may go into a VirtualHost container.
# The first VirtualHost section is used for requests without a known
# server name.
#
#<VirtualHost *:80>
# ServerAdmin webmaster#dummy-host.example.com
# DocumentRoot /www/docs/dummy-host.example.com
# ServerName dummy-host.example.com
# ErrorLog logs/dummy-host.example.com-error_log
# CustomLog logs/dummy-host.example.com-access_log common
#</VirtualHost>
<VirtualHost *:80>
ServerName weather.trekfederation.com
ServerAlias weather.trekfederation.com
DocumentRoot "/var/www/html/weather"
</VirtualHost>
<VirtualHost *:80>
ServerName oldsite.trekfederation.com
ServerAlias oldstore.trekfederation.com
DocumentRoot "/var/www/html/trekfed"
</VirtualHost>
<VirtualHost *:80>
ServerName promenade.trekfederation.com
Redirect / http://trekfederation.com/shop
</VirtualHost>
<VirtualHost *:80>
ServerName donations.trekfederation.com
Redirect / http://trekfederation.com/product-category/donations/
</VirtualHost>
Include conf.d/ships.conf
<VirtualHost *:80>
ServerName trekfederation.com
ServerAlias trekfederation.com www.trekfederation.com
Alias /phpmyadmin "/usr/share/phpMyAdmin"
<Directory "/usr/share/phpMyAdmin">
DirectoryIndex index.php
AllowOverride All
Options FollowSymlinks
Order allow,deny
Allow from all
</Directory>
ServerAlias www.iftcommand.com
DocumentRoot "/var/www/html/trekweb"
</VirtualHost>
# Include conf.d/personal.conf
<VirtualHost *:80>
ServerName ccc.trekfederation.com
DocumentRoot "/var/www/html/ccc"
</VirtualHost>
Ships.conf:
<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
</VirtualHost>
<VirtualHost *:80>
ServerName ussfarragut.trekfederation.com
ServerAlias ussfarragut.trekfederation.net
DocumentRoot /var/www/html/NOVA/USS_FARRAGUT
</VirtualHost>
<VirtualHost *:80>
ServerName ussvortex.trekfederation.com
ServerAlias ussvortex.trekfederation.net
DocumentRoot /var/www/html/NOVA/USS_VORTEX
</VirtualHost>
<VirtualHost *:80>
ServerName ussvictory.trekfederation.com
ServerAlias ussvictory.trekfederation.net
DocumentRoot /var/www/html/NOVA/USS_VICTORY
</VirtualHost>
<VirtualHost *:80>
ServerName ussnavigator.trekfederation.com
ServerAlias ussnavigator.trekfederation.net
DocumentRoot /var/www/html/NOVA/USS_NAVIGATOR
</VirtualHost>
<VirtualHost *:80>
ServerName ussfirelace.trekfederation.com
ServerAlias ussfirelace.trekfederation.net
DocumentRoot /var/www/html/NOVA/USS_FIRELACE
</VirtualHost>
the site i'm working on is ussfirelace.trekfederation.com. it still routes to trekfederation.com.
it does register with apache -S:
port 80 namevhost ussfirelace.trekfederation.com (/etc/httpd/conf.d/ships.conf:34)
alias ussfirelace.trekfederation.net
what am I missing?
Thanks.
The configs look good to me. Possible solutions / problems:
a) Did you restart your apache to load the new virtual host?
b) Have you checked that the DNS record points as A Record (or CNAME) to your server and no redirect is configured with your NS provider?
c) Have you checked your code, that it does not trigger a redirect (e.g. 301, or javascript...)?
d) Clearing browser cache is also some thing that you should take care of.
e) Doublecheck the URL you type for typos.

Issue on creating a MAMP virtualhost

I'm trying to create a MAMP virtualhost so that I can use my laravel project easier, but whatever I do I get " Not Found The requested URL / was not found on this server " when entering localhost or my vh name (cms.dev)
I tried this : How to create virtual hosts in MAMP?
And this : http://eppz.eu/blog/virtual-host-for-mamp-on-osx/
Even reinstalled MAMP and I still have the same issue, I did everything the same way it's shown in these articles.
Here's my httpd-vhosts.conf config :
#
# Use name-based virtual hosting.
#
NameVirtualHost *:80
#
# VirtualHost example:
# Almost any Apache directive may go into a VirtualHost container.
# The first VirtualHost section is used for all requests that do not
# match a ServerName or ServerAlias in any <VirtualHost> block.
#
<VirtualHost *:80>
ServerAdmin webmaster#dummy-host.example.com
DocumentRoot "/Desktop/MAMP"
ServerName localhost
ServerAlias www.dummy-host.example.com
ErrorLog "logs/dummy-host.example.com-error_log"
CustomLog "logs/dummy-host.example.com-access_log" common
</VirtualHost>
<VirtualHost *:80>
ServerAdmin webmaster#dummy-host2.example.com
DocumentRoot "/Desktop/MAMP/cms/public"
ServerName cms.dev
ErrorLog "logs/dummy-host2.example.com-error_log"
CustomLog "logs/dummy-host2.example.com-access_log" common
</VirtualHost>
Do you guys know what's happening ???
Thanks
Your DocumentRoot paths are wrong. In MacOS, the Desktop folder is in /Users/[yourusername]/Desktop.

Editted Virtual Host ServerName in httpd.conf but it seems it did not take effect

So I have 4 Virtual Host. I made the 3 Virtual Hosts 2 days ago and it
all works fine. When I made another Virtual Host the next day, it
won't load up the newly created Virtual Host. So I tried renaming the Server Name of my 1st Virtual
Host which is running.
I changed the ServerName and Alias of www.test1.biboglobal.com to hogehoge.com with still the directory of test1. Saved the file and restarted apache. But when I tried to browse hogehoge.com,it won't load the contents.
I tried browsing the test1.biboglobal.com and it still worked. But
that Server Name does not exist anymore because I changed it to
hogehoge.com.
I've searched all about httpd.conf not taking changes but found no answer. I hope someone can help me.
I also tried removing all Virtual Hosts and restarted Apache. But still, it was able to browse test1, test2, test3.biboglobal.com. Although it only displayed the Apache Test Page, not the index.html inside their respective directory.
NameVirtualHost *:80
#
# NOTE: NameVirtualHost cannot be used without a port specifier
# (e.g. :80) if mod_ssl is being used, due to the nature of the
# SSL protocol.
#
#
# VirtualHost example:
# Almost any Apache directive may go into a VirtualHost container.
# The first VirtualHost section is used for requests without a known
# server name.
#
<VirtualHost *:80>
ServerAdmin webmaster#hogehoge.com //**This was supposed to be test1.biboglobal.com**
DocumentRoot /var/www/test2.biboglobal.com/public_html
ServerName www.hogehoge.com //**This was supposed to be test1.biboglobal.com**
ServerAlias hogehoge.com //**This was supposed to be test1.biboglobal.com**
ErrorLog /var/www/test1.biboglobal.com/error.log
# CustomLog /var/www/test1.biboglobal.com/requests.log
</VirtualHost>
<VirtualHost *:80>
ServerAdmin webmaster#test2.biboglobal.com
DocumentRoot /var/www/test2.biboglobal.com/public_html
ServerName www.test2.biboglobal.com
ServerAlias test2.biboglobal.com
ErrorLog /var/www/test2.biboglobal.com/error.log
# CustomLog /var/www/test2.biboglobal.com/requests.log
</VirtualHost>
<VirtualHost *:80>
ServerAdmin webmaster#test3.biboglobal.com
DocumentRoot /var/www/test3.biboglobal.com/public_html
ServerName www.test3.biboglobal.com
ServerAlias test3.biboglobal.com
ErrorLog /var/www/test3.biboglobal.com/error.log
# CustomLog /var/www/test3.biboglobal.com/requests.log
</VirtualHost>
<VirtualHost *:80>
ServerAdmin webmaster#test4.biboglobal.com
DocumentRoot /var/www/test4.biboglobal.com/public_html
ServerName www.test4.biboglobal.com
ServerAlias test4.biboglobal.com
ErrorLog /var/www/test4.biboglobal.com/error.log
CustomLog /var/www/test4.biboglobal.com/requests.log common
</VirtualHost>

Apache default VirtualHost

How can I set a default VirtualHost in Apache?
Preferably, I want the default host not to be the same as the IP address host. Now I have something like this:
NameVirtualHost *
<VirtualHost *>
ServerAdmin admin#example.com
DocumentRoot /someOtherDir/
ServerAlias ip.of.the.server
</VirtualHost>
<VirtualHost *>
ServerAdmin admin#example.com
DocumentRoot /someroot/
ServerAlias example.com *.example.com
</VirtualHost *>
If a domain is forwarded to my server, but isn't in this vhost.conf file, the files from /someOtherDir/ are loaded, as expected. But I want to be able to use a different root for the IP address itself and domains which aren't added to the vhost.conf file (yet). Is this possible?
I found the answer: I remembered that Apache uses the first block if no other matching block is found, so I've added a block without a serveralias at the top of the blocks:
NameVirtualHost *
<VirtualHost *>
DocumentRoot /defaultdir/
</VirtualHost>
<VirtualHost *>
ServerAdmin admin#example.com
DocumentRoot /someOtherDir/
ServerAlias ip.of.the.server
</VirtualHost>
<VirtualHost *>
ServerAdmin admin#example.com
DocumentRoot /someroot/
ServerAlias example.com *.example.com
</VirtualHost>
If you are using Debian style virtual host configuration (sites-available/sites-enabled), one way to set a Default VirtualHost is to include the specific configuration file first in httpd.conf or apache.conf (or what ever is your main configuration file).
# To set default VirtualHost, include it before anything else.
IncludeOptional sites-enabled/my.example.com.conf
# Load config files in the "/etc/httpd/conf.d" directory, if any.
IncludeOptional conf.d/*.conf
# Load virtual host config files from "/etc/httpd/sites-enabled/".
IncludeOptional sites-enabled/*.conf
The other answers here didn't work for me, but I found a pretty simple solution that did work.
I made the default one the last one listed, and I gave it ServerAlias *.
For example:
NameVirtualHost *:80
<VirtualHost *:80>
ServerName www.secondwebsite.example
ServerAlias secondwebsite.example *.secondwebsite.example
DocumentRoot /home/secondwebsite/web
</VirtualHost>
<VirtualHost *:80>
ServerName www.defaultwebsite.example
ServerAlias *
DocumentRoot /home/defaultwebsite/web
</VirtualHost>
If the visitor didn't explicitly choose to go to something ending in secondwebsite.example, they get the default website.
Actually, I'm using Virtual host configuration (sites-available / sites-enabled) on EC2 Linux AMI with Apache/2.4.39 (Amazon). So, I have 1 EC2 instance to serve many sites (domains).
Considering that you already have Virtual Host installed and working. In my folder /etc/httpd/sites-available, I have some files with domain names (suffix .conf), for example: example.com.conf. Create a new file like that.
sudo nano /etc/httpd/sites-available/example.com.conf
<VirtualHost *:80>
ServerName www.example.com
ServerAlias example.com
DocumentRoot /var/www/html/domain
</VirtualHost>
For each file.conf in sites-available, I create a symbolic link:
sudo ln -s /etc/httpd/sites-available/example.com.conf /etc/httpd/sites-enabled/example.com.conf
This is the default configuration, so, if access directly by IP of Server, you will be redirect to DocumentRoot of the first file (.conf) in sites-available folder, sorted by filename.
To have a default DocumentRoot folder when access by IP, you have to create a file named 0a.conf, then Apache will serve this site because this new file will be the first in sites-available folder.
You must create a symbolic link:
sudo ln -s /etc/httpd/sites-available/0a.conf /etc/httpd/sites-enabled/0a.conf
To check serving order, use it:
sudo apachectl -S
Now, restart Apache, and check out it.
Obligatory - none of the previous answers worked for me. I inherited a strange combination of IP address-based virtual hosts and * vhosts (not assigned/catch all IP addresses) based virtual hosts in this Apache configuration messed up by ISPConfig.
I wanted Apache to serve not configured hosts with the same page.
I had: not configured hosts went to the first vhost after 000-default.conf. No matter I had *:80 catch all defined as the first vhost, instead of default Apache would load first defined site:
<VirtualHost *:80>
ServerAdmin webmaster#localhost
DocumentRoot /var/www/html
</VirtualHost>
Although it's not completely valid configuration, what finally worked was adding an IP address-based virtualhost without ServerName/ServerAlias defined:
<VirtualHost 192.168.10.10:80>
ServerAdmin webmaster#localhost
DocumentRoot /var/www/html
</VirtualHost>
<VirtualHost 192.168.10.10:443>
ServerAdmin webmaster#localhost
DocumentRoot /var/www/html
SSLEngine On
...
</VirtualHost>
$ apachectl -S outputs IP address-based vhosts first, and * based vhosts later, and finally my default site is loaded before real site:
AH00548: NameVirtualHost has no effect and will be removed in the next release /etc/apache2/sites-enabled/000-default.conf:50
192.168.10.10:80 is a NameVirtualHost
default server server.tld (/etc/apache2/sites-enabled/000-default.conf:34)
port 80 namevhost server.tld (/etc/apache2/sites-enabled/000-default.conf:34)
port 80 namevhost some-site.tld (/etc/apache2/sites-enabled/100-some-site.tld.vhost:7)
...
46.23.86.103:443 is a NameVirtualHost
default server server.tld (/etc/apache2/sites-enabled/000-default.conf:38)
port 443 namevhost server.tld (/etc/apache2/sites-enabled/000-default.conf:38)
port 443 namevhost some-site.tld (/etc/apache2/sites-enabled/100-some-site.tld.vhost:182)
...
*:80 is a NameVirtualHost
default server server.tld (/etc/apache2/sites-enabled/000-default.conf:1)
port 80 namevhost server.tld (/etc/apache2/sites-enabled/000-default.conf:1)
Word of notice - in a configuration like this, * vhosts won't work, so you need to apply IP addresses to all vhosts.
An alternative setting is to have the default virtual host at the end of the config file rather than the beginning. This way, all alternative virtual hosts will be checked before being matched by the default virtual host.
Example:
NameVirtualHost *:80
Listen 80
...
<VirtualHost *:80>
ServerName host1
DocumentRoot /someDir
</VirtualHost>
<VirtualHost *:80>
ServerName host2
DocumentRoot /someOtherDir
</VirtualHost>
<VirtualHost *:80>
DocumentRoot /defaultDir
</VirtualHost>
I had the same issue. I could fix it by adding the following in httpd.conf itself before the IncludeOptional directives for virtual hosts. Now localhost and the IP 192.168.x.x both points to the default test page of Apache. All other virtual hosts are working as expected.
<VirtualHost *:80>
DocumentRoot /var/www/html
</VirtualHost>
Reference: https://httpd.apache.org/docs/2.4/vhosts/name-based.html#defaultvhost
Only supported and correct answer is:
<VirtualHost _default_:*>
DocumentRoot "/www/default"
</VirtualHost>
or my own version to return 403:
<VirtualHost _default_:*>
<Location />
Require all denied
</Location>
</VirtualHost>
The NameVirtualHost option would be a good option.
The solution is:
# apache2.conf
# #warning this is specific to apache 2.2
NameVirtualHost *:80
Listen 80
# ...
# aaaa.example.conf
<VirtualHost *:80>
ServerName aaaa.example
DocumentRoot /defaultDir
</VirtualHost>
# host1.example.conf
<VirtualHost *:80>
ServerName host1.example
DocumentRoot /someDir
</VirtualHost>
# host2.example.conf
<VirtualHost *:80>
ServerName host2.example
DocumentRoot /someOtherDir
</VirtualHost>
In my case, to work, I created a VirtualHost (n.e. VirtualHost per CNAME) called aaaa.example since I have different files for different VirtualHosts and knowing that Apache reads them in alphabetical order.

Strange problem with Apache Virtual Hosts

I have recently installed Apache 1.3.41 on a Windows Vista machine. I have not changed the default settings in httpd.conf aprart from trying to setup virtual hosts, as follows:
Added some host names in the hosts file:
127.0.0.1 localhost
#::1 localhost
127.0.0.1 mysite
127.0.0.1 mydomain
I made the following folders in C:/Users/Moukasp/
C:/Users/Moukasp/Apache
C:/Users/Moukasp/django/mysite
Then added some simple html pages in each of those folder and the c:/Users/Moukasp/pictures folder and, finally, I added the following settings at the end of httpd.conf:
NameVirtualHost 127.0.0.1
<VirtualHost localhost>
# ServerAdmin webmaster#dummy-host.example.com
DocumentRoot "C:/Users/Moukasp/Apache"
ServerName localhost
Alias "/pics" "c:/users/moukasp/pictures"
Alias "/ap" "C:/Program Files/Apache Group/Apache/htdocs"
Alias "/dj" "C:/Users/Moukasp/django/mysite"
# ErrorLog logs/dummy-host.example.com-error_log
# CustomLog logs/dummy-host.example.com-access_log common
</VirtualHost>
<VirtualHost mysite>
# ServerAdmin webmaster#dummy-host.example.com
DocumentRoot "C:/Users/Moukasp/django/mysite"
ServerName mysite
# ErrorLog logs/dummy-host.example.com-error_log
# CustomLog logs/dummy-host.example.com-access_log common
</VirtualHost>
<VirtualHost mydomain>
# ServerAdmin webmaster#dummy-host.example.com
DocumentRoot "C:/Users/Moukasp/django/mysite"
ServerName mydomain
# ErrorLog logs/dummy-host.example.com-error_log
# CustomLog logs/dummy-host.example.com-access_log common
</VirtualHost>
So, urls
http://localhost
http://localhost/pics
http://localhost/ap
http://localhost/dj
http://mydomain
work fine. But I there's no response from http://mysite which, as can be seen from the settings, serves from the same folder as http://mydomain. I have tried various names but no response. The hosts file is being read every time I start the Apache server. I even removed the mydomain server from the hosts file and the httpd.conf lest there's a limitation in the number of virtual hosts, but no luck.
Any help would be greatly appreciated, els I go mad! It is true, I have being trying this after complete failure to make Apache work with mod_python for django! I just hoped that a step at a time would lead to some success!
Try:
NameVirtualHost 127.0.0.1:80
In your httpd.conf. My config file includes a comment stating a port-specifier should be used.
The VirtualHost and NameVirtualHost directives should have IP addresses and ports:
NameVirtualHost 127.0.0.1:80
<VirtualHost 127.0.0.1:80>
# ServerAdmin webmaster#dummy-host.example.com
DocumentRoot "C:/Users/Moukasp/django/mysite"
ServerName mysite
# ErrorLog logs/dummy-host.example.com-error_log
# CustomLog logs/dummy-host.example.com-access_log common
</VirtualHost>
Tried NameVirtualHost 127.0.0.1:80 but it doesn't make any difference! The only servers that work are localhost and mydomain.
I wonder whether I need to do something more than just declare the server name of each virtual host in the hosts file.
Try adding a port declaration to the hosts. For example:
<VirtualHost mydomain:80>
DocumentRoot "C:/Users/Moukasp/django/mysite"
ServerName mydomain
</VirtualHost>