Apache Virtual Domains for staging - apache

We're trying to set up a staging service for domains configured on a server.
At present we have the following in our DNS and it is pointing to our server correctly.
*.server001.stage.ourdomain.com.au
This serves the default site located at:
/Server/http/_default
What I would like it to do is load a site based by the information in place of the wildcard.
Example;
test.com.server001.stage.ourdomain.com.au
would return the contents of:
/Server/http/test.com
Remembering that we might be using .com.au domain names too, so there'd be a requirement for anything before the "server001" part.

You should define another <VirtualHost> block for test.com.server001.stage.ourdomain.com.au with its DocumentRoot pointing to /Server/http/test.com.
Something like this
<VirtualHost *:80>
ServerAdmin webmaster#test.com
DocumentRoot /Server/http/test.com
ServerName test.com.server001.stage.ourdomain.com.au
ErrorLog /var/logs/httpd/test.com/error_log
CustomLog /var/logs/httpd/test.com/access_log common
</VirtualHost>
<VirtualHost *:80>
ServerAdmin webmaster#server001.stage.ourdomain.com.au
DocumentRoot /Server/http/_default
ServerName *.server001.stage.ourdomain.com.au
ErrorLog /var/logs/httpd/server001.stage.ourdomain.com.au/error_log
CustomLog /var/logs/httpd/server001.stage.ourdomain.com.au/access_log common
</VirtualHost>
The wildcard VirtualHost should be the last one since apache picks up the first VirtualHost that matches.

Have a look at the Dynamically Configured Mass Virtual Hosting chapter in the manual. It basically explains how to use the VirtualDocumentRoot directive.

Related

Domains crossed over on digital ocean

I have two domains. exampleone.com and exampletwo.com.
I followed this guide for adding multiple domains on the same server:
https://www.digitalocean.com/community/tutorials/how-to-set-up-apache-virtual-hosts-on-ubuntu-16-04
They are both on the same server. Somehow, blog.exampleone.com was created (out of nowhere). and it points to exampletwo.com.
If I click on the site in google, it shows blog.exampleone.com as the domain, but shows the content for exampletwo.com
How is this happening?
I've looked into the vhost files and everything seems correct.
Here is a sample vhost file:
<VirtualHost *:80>
ServerAdmin dave#example.com
DocumentRoot /var/www/html/exampleone.com/public_html
ErrorLog ${APACHE_LOG_DIR}/error.lognano
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
This should result in 2 separate sites on the same server. Instead, a blog subdomain was created on one site that points to the content on another site.
What should I do?
Thanks!
You neeed to specify the "ServerName" in your vhost file, something like :
<VirtualHost *:80>
ServerAdmin dave#example.com
ServerName blog.exampleone.com
DocumentRoot /var/www/html/exampleone.com/public_html
ErrorLog ${APACHE_LOG_DIR}/error.lognano
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
in fact, for each vhost file, you need to specify the ServerName to work properly with your subdomains!

Virtualhost configuration not manage correctly requests

I have the following Virtualhost config in httlp-vhosts.conf:
<VirtualHost *:80>
ServerName rest.budgettracker.loc
DocumentRoot "C:/xampp/htdocs/budget-develop/budget-develop/api/public"
ErrorLog "logs/rest.budgettracker.loc-error.log"
CustomLog "logs/rest.budgettracker.loc-access.log" common
</VirtualHost>
<VirtualHost *:80>
ServerName dev.budgettracker.loc
DocumentRoot "C:/xampp/htdocs/budget-develop/budget-develop"
ErrorLog "logs/budgettracker.loc-error.log"
CustomLog "logs/budgettracker.loc-access.log" common
</VirtualHost>
When I enter dev.budgettrackerpro.com in the browser it goes to the rest.budgettrackerpro.loc virtualhost container.
If I remove the Virtualhost container for the rest request it directs correctly to the correct html/javascript code. Obviously I need the rest call to make it work correctly. I have researched this until I am blue in the face, what am I doing wrong? Please help
You are asking for dev.budgettrackerpro.com. Your configuration is for dev.budgettrackerpro.loc.
What happens is:
Apache sees that your request is on port 80 (http://...)
It checks which VirtualHost are configured to take traffic from port 80.
Here it finds 2. 1) rest.budgettracker.loc 2) dev.budgettracker.loc
Since the domain you asked (the .com) does not match either 1) or 2), Apache assumes that the VirtualHost it should use is the first one it found.
Apache when finding multiple VirtualHost or finding none that match uses the first one in the file (top to bottom).
To solve this:
Request http://dev.budgettracker.loc
Modify your VirtualHost to accept traffic form .com as well, like so:
<VirtualHost *:80>
ServerName dev.budgettracker.loc
ServerAlias dev.budgettracker.com
DocumentRoot "C:/xampp/htdocs/budget-develop/budget-develop"
ErrorLog "logs/budgettracker.loc-error.log"
CustomLog "logs/budgettracker.loc-access.log" common
</VirtualHost>
Notice the new line, ServerAlias dev.budgettracker.com. You can have multiple ServerAlias in a VirtualHost, but only one ServerName.

Dynamic server aliasing in apache webserver

I have a usecase where I need to setup wildcard subdomains with condition so that for http://xyz.example.com type of request it should choose document root as /var/www/html/web and for http://xyz-portal.example.com it should choose document root as /var/www/html/admin. How this can be achieved?
Generate different virtualhosts with a default name resembling each case and then use ServerAlias for the wildcard. This is a example, adjust to your needs considering the following explanation.
Example:
<VirtualHost *:80>
ServerName www-portal.example.com
ServerAlias *-portal.example.com
DocumentRoot /var/www/html/admin
...
</VirtualHost>
<VirtualHost *:80>
ServerName www.example.com
ServerAlias *.example.com
DocumentRoot /var/www/html/web
...
</VirtualHost>
Explanation:
ServerName does not take in wildcards, so you must define it with the main "default" virtualhost name matching the scheme you want to use, then you use ServerAlias with wildcards or several entries to match all those domain requests that have to land in the same virtualhost
Note the xyz-portal.example.com must be defined first. Why? Because the generic wildcard of the other virtualhost serveralias "*.example.com" would match and grab the request if defined first. Apache selects virtualhost to reply based on Host header requested and first match in the order of loaded virtualhost wins.

Apache Virtual Host using mod_vhost_alias

I am trying to set up my apache module to dynamically direct all requests to a specific folder and then match the name to a folder of the same name.
To do this I set the following in my 000-default.conf file in the sites-available folder.
UseCanonicalName Off
VirtualDocumentRoot /var/www/example/%2
This worked great.
Then I wanted to setup a couple of different domains to not point to the example folder, but somewhere else, so I added a couple of these before the VirtualDocumentRoot line:
<VirtualHost *:80>
ServerName sub1.example.com
VirtualDocumentRoot /var/www/sub1.example.com
</VirtualHost>
However, now the dynamic pointing does not work anymore and all the URL's are redirected to the first -> VirtualDocumentRoot location.
Can someone please indicate to me what I am doing wrong?
Full Code Example In apache2/sites-available/000-default.conf:
<VirtualHost *:80>
ServerName sub1.example.com
VirtualDocumentRoot /var/www/sub1.example.com
</VirtualHost>
<VirtualHost *:80>
ServerName sub2.example.com
VirtualDocumentRoot /var/www/sub2.example.com
</VirtualHost>
<VirtualHost *:80>
ServerName sub3.example.com
VirtualDocumentRoot /var/www/sub3.example.com
</VirtualHost>
UseCanonicalName Off
VirtualDocumentRoot /var/www/example/%2
Do not use VirtualDocumentRoot for simple Virtualhosts, use only DocumentRoot.
VirtualDocumentRoot defines the mass-virtualhost catch-all, and by definition you can only have one mass-virtualhost (else how could apache knows which VH a given hostname should match).
Edit:
Now you need some other changes:
- ensure you have NameVirtualHost *:80 somewhere in apache configuration (unless you use Apache 2.4).
- Move the Mass-Virtualhost as first, so it will become the default virtualhost. The default virtualhost is used when the request host name does not match any ServerName directive. (You can check the default VH by running apache with -S option).
I have figured out how to do this, and decided to post the solution here for anyone else sitting with a similar problem:
SO to setup apache2, using mod_vhost_alias to have all domains point to a generic folder with the same name, but specific domains to point elsewhere, this is what you need to do.
In your 000-default.conf site config file, write the following code:
UseCanonicalName Off
Then add the following block for each specific domain you want to point to a specific folder, replacing example.com with your domain name:
<VirtualHost *:80>
ServerName example.com
ServerAlias www.*
DocumentRoot path/to/your/folder
</VirtualHost>
Then add the next block to point all other generic domains to a generic folder:
<VirtualHost *:80>
ServerName vhosts.fqdn
ServerAlias www.*
VirtualDocumentRoot path/to/your/folder/%2+
</VirtualHost>
<VirtualHost *:80>
ServerName vhosts.fqdn
ServerAlias *
VirtualDocumentRoot path/to/your/folder/%1+
</VirtualHost>
The first block will direct all domains, starting with www. to a folder matching the name after the www.
The second block is to direct the same domains, when no www. is specified, to the same folder.
For more information on the dynamic mass virtual host options to use in the document root, go to: http://httpd.apache.org/docs/2.2/mod/mod_vhost_alias.html

Why does http://localhost redirect to my default virtual host once I setup virtual hosts in Apache?

This is probably an easy question, but I want to understand better how Apache works with virtual hosts. I am setting up virtual hosts because I work on multiple websites at once and I don't want to use subdirectories. I was pretty much using the default Apache httpd.conf file with the DocumentRoot pointing to something like "/www". I uncommented the virtual hosts include and added the following:
NameVirtualHost *:80
<VirtualHost *:80>
ServerName site1.dev
DocumentRoot /www/site1
</VirtualHost>
<VirtualHost *:80>
ServerName site2.dev
DocumentRoot /www/site2
</VirtualHost>
Now when I go to http://localhost I get the default page for site1.
I'm sure there is a reason why this makes sense, but I don't quite understand it. I would've thought that only requests that were explicitly to http://site1.test would get routed through that directive and it wouldn't just become the default. Can someone explain why it becomes the default.
http://httpd.apache.org/docs/1.3/vhosts/name-based.html
(Should be true for 2.x also)
"If no matching virtual host is found, then the first listed virtual host that matches the IP address will be used.
As a consequence, the first listed virtual host is the default virtual host. The DocumentRoot from the main server will never be used when an IP address matches the NameVirtualHost directive. If you would like to have a special configuration for requests that do not match any particular virtual host, simply put that configuration in a container and list it first in the configuration file."
answer 1 is correct
and i'd add with namevirtualhosts as the first entry
essentially matches any not-named elsewhere virtualhost
it should ONLY be used to catch unintentional mal-formed and broken traffic
ie a machene with one ip called john.domain.com running www.domain.com and www.domain2.com as valid webservers on ip www.xxx.yyy.zzz might have an optimal config like thus
<VirtualHost *:80>
DocumentRoot /var/webserver/static-sites/unknown/
# a directory readable by apache with only a robots.txt denying everything
ServerName bogus
ErrorDocument 404 "/errordocuments/unknown-name.html"
#custom 404 describing how/what they might have done wrong try pointing a browser {with a hosts file at http://bogus/ on 193.120.238.109 to see mine#
ErrorLog /var/log/httpd/unknown-error.log
CustomLog /var/log/httpd/unknown-access.log combined
</VirtualHost>
<VirtualHost *:80>
DocumentRoot /var/webserver/static-sites/unknown/
# a possibly different directory readable by apache with only a robots.txt denying everything
ServerName www.xxx.yyy.zzz
ServerAlias john.domain.com
ErrorDocument 404 "/errordocuments/ip-name.html"
ErrorDocument 403 "/errordocuments/ip-name.html"
#custom 404 telling them as a likely hacker/bot you wish to have nothing to do with them see mine at http://193.120.238.109/
ErrorLog /var/log/httpd/ip-error.log
CustomLog /var/log/httpd/ip-access.log combined
</VirtualHost>
<VirtualHost *:80>
ServerName domain.com
RedirectPermanent / http://www.domain.com/
ErrorLog logs/www.domain.com-error.log
CustomLog logs/www.domain.com-access.log combined
</VirtualHost>
<VirtualHost *:80>
DocumentRoot /var/webserver/ftpusers/domain
ServerName www.domain.com
ServerPath /domain
ErrorLog logs/www.domain.com-error.log
CustomLog logs/www.domain.com-access.log combined
</VirtualHost>
<VirtualHost *:80>
ServerName domain2.com
RedirectPermanent / http://www.domain2.com/
ErrorLog logs/www.domain2.com-error.log
CustomLog logs/www.domain2.com-access.log combined
</VirtualHost>
<VirtualHost *:80>
DocumentRoot /var/webserver/ftpusers/domain2
ServerName www.domain2.com
ServerPath /domain2
ErrorLog logs/www.domain2.com-error.log
CustomLog logs/www.domain2.com-access.log combined
</VirtualHost>
Confirming that for Apache 2.x, the first virtual host (with the same port number) will be used if a matching virtual host is not found.
http://httpd.apache.org/docs/2.2/vhosts/details.html
"If no matching vhost could be found the request is served from the first vhost with a matching port number that is on the list for the IP to which the client connected"
You can always add this code below, put it right below NameVirtualHost *:80 so that your default document root is served by default if no other virtual hosts found.
<VirtualHost *:80>
ServerName localhost
DocumentRoot /my/default/document/root
</VirtualHost>
Simply put this code at top in httpd-vhosts.conf
<VirtualHost localhost:80>
ServerName localhost
DocumentRoot d:/xampp/htdocs
<Directory "d:/xampp/htdocs/">
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Require local
</Directory>
</VirtualHost>
One way to do this is:
In your VirtualHosts configuration, enter the specific local site name you want to enable instead of using a wildcard:
<VirtualHost site1.dev:80> instead of <VirtualHost *:80>
Switch off NameVirtualHost *:80 which can be done by commenting it out in your vhosts.conf file
In your /etc/hosts file mention both aliases for the loopback IP:
127.0.0.1 localhost site1.dev
That's it. You should see that localhost goes to the default DocumentRoot as usual and the site1.dev goes to the site you've setup as virtual host.