Is www included in the server name in apache virtual host - apache

I'm looking to set up VirtualHosts on my apache server and was looking for documentation that would tell me if these 2 entries are identical
<VirtualHost *:80>
DocumentRoot /www/example1
ServerName www.example1.com
</VirtualHost>
<VirtualHost *:80>
DocumentRoot /www/example1
ServerName example1.com
</VirtualHost>
Notice the lack of www. in the second one.
Thanks

This is from the Apache docs (http://httpd.apache.org/docs/2.0/mod/core.html#servername):
The ServerName directive sets the hostname and port that the server uses to identify itself. This is used when creating redirection URLs. For example, if the name of the machine hosting the web server is simple.example.com, but the machine also has the DNS alias www.example.com and you wish the web server to be so identified, the following directive should be used:
ServerName www.example.com:80
So I guess they are not identical.

Related

<VirtualHost> matches when missing ServerName directive

I am in the process of migrating some sites from a server running Apache 2.4.7 to a new installation (Ubuntu 18) running Apache 2.4.29 and running into some issues with VirtualHost matching.
On my old server I have a config like this:
<VirtualHost 12.34.56.78:80>
ServerAlias *.dev.example.com
VirtualDocumentRoot /var/www/dev/%1
</VirtualHost>
<VirtualHost 12.34.56.78:80>
ServerName example.com
ServerAlias www.example.com
DocumentRoot /var/www/example/
</VirtualHost>
As you can see, the ServerName directive is intentionally absent from the first host which uses a VirtualDocumentRoot to serve directories based on the subdomain. This has been working fine on the old server.
In my new environment everything worked fine at first, but today (no updates, nothing changed), oddly things changed. For some reason apache started matching example.com to the first vhost and after some amount of debugging I have determined that this is due to the lack of ServerName directive. When I add one - anything really - the problem goes away. So to be clear, a working config now looks like this:
<VirtualHost 12.34.56.78:80>
ServerName anything.dev.example.com
ServerAlias *.dev.example.com
VirtualDocumentRoot /var/www/dev/%1
</VirtualHost>
<VirtualHost 12.34.56.78:80>
ServerName example.com
ServerAlias www.example.com
DocumentRoot /var/www/example/
</VirtualHost>
The apache docs have this to say about omitting the ServerName directive:
If you omit the ServerName directive from any name-based virtual host,
the server will default to a fully qualified domain name (FQDN)
derived from the system hostname.
My system hostname is something like servername.example.com - which shouldn't match example.com.
My question:
Has there been some change in the apache's behaviour with this newer version that would break my previous configuration? I'm looking for an explanation about why this worked fine in the old environment and now suddenly does not. I'm also looking for any explanation at all about why apache would be matching my first vhost when ServerName is absent.

How to customise my URL which is having IP Address

Is it possible to configure my URL which has my IP address on it- like: "http://192.168.xx.yy/index.php". The situation is when I run Apache server in my PC, and load localhost in it. I know it is possible after hosting with external server, but is there any way we can configure within our localhost?
How to configure the Apache files to make this happen? I tried in my localhost, editing the "httpd.conf" by adding this inside like this - please tell me where I am getting the issue!
ServerName localhost:80
HostnameLookups Off
<VirtualHost *:80>
# This first-listed virtual host is also the default for *:80
ServerName www.example.com
ServerAlias example.com
DocumentRoot /www/domain
</VirtualHost>
<VirtualHost *:80>
ServerName other.example.com
DocumentRoot /www/otherdomain
</VirtualHost>
DocumentRoot "c:/wamp/www/"
Yes, you can play with multiple IP addresses on your machine. Configuration depends on your OS. Article Create Multiple IP Addresses to One Single Network Interface is for linux.
But, better way is to use VirtualHosts based on host names or (simplest) on ports. So you can get http://siteA.mycoputer.localhost, http://siteB.mycomputer.localhost in the first case and http://192.168.x.y:8000, http://192.168.x.y:9000 in the second case
Here is Apache Server config example from Apache Server 2.2 documentation
# Ensure that Apache listens on port 80
Listen 80
# Listen for virtual host requests on all IP addresses
NameVirtualHost *:80
<VirtualHost *:80>
DocumentRoot /www/example1
ServerName www.example.com
# Other directives here
</VirtualHost>
<VirtualHost *:80>
DocumentRoot /www/example2
ServerName www.example.org
# Other directives here
</VirtualHost>

Apache VirtualHost is not working

I have a server running Apache 2.4 on Windows, and I have set up a VirtualHost in the httpd-vhosts.conf file, and an 'A' record in my DNS server that points subdomain.mydomain.com to my IP address. Unfortunately, connecting to subdomain.mydomain.com just shows the same page as mydomain.com. Here is the code I used in the httpd-vhosts.conf file:
<VirtualHost *:80>
ServerAdmin admin#mydomain.com
DocumentRoot "c:/Apache24/subdomain/htdocs"
ServerName subdomain.mydomain.com
ErrorLog "c:/Apache24/subdomain/logs/errors.log"
CustomLog "c:/Apache24/subdomain/logs/access.log"
</VirtualHost>
What am I doing wrong?
Make sure your domain provider configuration doesn't redirect # to www, you need them to be configured separately to redirect to your machines IP address

NamedVirtualHost in apache configuration

I have a unix system whose actual name is "ech-10.45.25.12"
i have installed apache server in it.
Now i need to configure it in such a way that the two applications running in the same machine in tomcat in two different ports should be accessed by the same domain.
ie., i have two applications running in the same machine under different port
http://ech-10.45.25.12:8080/issuetracker/
http://ech-10.45.25.12:8180/dashboard/
I would like to name this server(ech-10.45.25.12) as devjunior.mycompany.com
The following is the configuration i have made in httpd.conf
Listen 80
Listen 8080
Listen 8180
NameVirtualHost ech-10.45.25.12:80
NameVirtualHost ech-10.45.25.12:8080
NameVirtualHost ech-10.45.25.12:8180
<VirtualHost ech-10.45.25.12:80>
ServerName devjunior.mycompany.com
DocumentRoot /www/domain-80
</VirtualHost>
<VirtualHost ech-10.45.25.12:8080>
ServerName devjunior.mycompany.com
DocumentRoot /www/domain-8080
</VirtualHost>
<VirtualHost ech-10.45.25.12:8180>
ServerName devjunior.mycompany.com
DocumentRoot /www/domain-8080
</VirtualHost>
i know i am doing a major mistake
But i should be able to access the applications by using the following urls
http://devjunior.mycompany.com/issuetracker
http://devjunior.mycompany.com/dashboard
Should i create ANY directories under any folders any where in the system
Please tell that also.
You configured only the names. So you've configured Apache to listen for:
http://devjunior.mycompany.com:8080
http://devjunior.mycompany.com:8180
You can:
Configure 2 domains with namevirtualhost without using ports. this is the most elegant way of doing what you want
Configure a single domain that points to a single directory on the filesystem with 2 links for the diferrent applications. This works with php mostly or pure html pages. With more complex applications you could incur in a lot of headache..
Domain and port. Like you've done. But you can access only by http://devjunior.mycompany.com:8080/issuetracker and http://devjunior.mycompany.com:8180/dashboard
Solution 1
You can use different domains or subdomains (which are cookie friendly in an eventuality of single sign on).
Listen 80
NameVirtualHost ech-10.45.25.12:80
<VirtualHost ech-10.45.25.12:80>
ServerName devjunior.mycompany.com
DocumentRoot /www/domain-80
</VirtualHost>
<VirtualHost ech-10.45.25.12:80>
ServerName dashboard.devjunior.mycompany.com
DocumentRoot /www/domain-8080
</VirtualHost>
<VirtualHost ech-10.45.25.12:80>
ServerName issuetracker.devjunior.mycompany.com
DocumentRoot /www/domain-8180
</VirtualHost>
Solution 2 is left as an excercise for the reader... :P
Here is what i did to make it work.
Though the change of name in etc/hosts file did nothing in my intranet, so i used the actual name of the machine which is ech-10.45.25.12
NameVirtualHost ech-10.45.25.12:80
<VirtualHost ech-10.45.25.12:80>
ServerName ech-10.45.25.12
ProxyPreserveHost on
ProxyPass /issuetracker http://ech-10.45.25.12:8080/issuetracker
ProxyPass /dashboard http://ech-10.45.25.12:8180/dashboard
</VirtualHost>
Also dont forget to add the "proxyName" & "proxyPort" attribute to the tag in tomcat's server.xml

Apache Multiple VirtualDocumentRoot

Using Apache2 on a Linux system is there a way to have multiple VirtualDocumentRoot using mod_vhost_alias?
This is naming convention I am currently using and would like to continue to use:
host directory
127.0.0.1 domain domain.com
127.0.0.1 sub.domain domain.com_sub
Then in my vhosts section of the httpd.conf I have:
NameVirtualHost 127.0.0.1
<VirtualHost 127.0.0.1>
VirtualDocumentRoot /var/www/%0.0.com
</VirtualHost>
<VirtualHost 127.0.0.1>
VirtualDocumentRoot /var/www/%2.0.com_%1
</VirtualHost>
The problem with this is when I visit sub.domain the Apache error log shows that it is looking for /var/www/sub.domain.com rather than /var/www/domain.com_test which leads me to believe it only reads the first rule and then fails, but what I would like it to do is use any document root that satisfies either of the two VirtualDocumentRoot rules.
Apache typically will pick the first virtual host whose ServerName or ServerAlias matches the host name provided in the Host HTTP header. In your case, since you have no ServerName directives, Apache supposedly uses a reverse DNS lookup on the IP address to fake a server name, and presuming that the reverse DNS leads to domain.com, which doesn't match, Apache then defaults to the first virtual host. Sounds complicated, I know... the bottom line is, you should use ServerName and ServerAlias to make the configuration explicit. Try something more like this:
NameVirtualHost 127.0.0.1
<VirtualHost 127.0.0.1>
ServerName domain.com
ServerAlias www.domain.com
VirtualDocumentRoot /var/www/%0
</VirtualHost>
<VirtualHost 127.0.0.1>
ServerName sub.domain.com
ServerAlias *.domain.com
VirtualDocumentRoot /var/www/%2.%3_%1
</VirtualHost>
That should use /var/www/domain.com for http://domain.com and /var/www/www.domain.com for http://www.domain.com, both of which are served by the first vhost, and /var/www/sub.domain.com for http://sub.domain.com, /var/www/blah.domain.com for http://blah.domain.com, and so on.
You have to qualify the backreferences when you want to put a '.' in the file path. So you need to have it like this:
VirtualDocumentRoot /var/www/%2.0.%3_%1
Regarding the OP and the issue with "/var/www/html" being set:
The problem I had to this was using %1 instead of %2. Here's my working example:
ServerAlias www.*.org.au
UseCanonicalName Off
VirtualDocumentRoot /path/to/sites/%2/pub
Hope that helps someone!
I read the docs on "Directory Name Interpolation" in mod_vhost_alias docs.
I finally found a configuration that allows flexible subdomain creation.
See apache docs on mod_vhost_alias
If your root dev domain has 3 parts like dev.example.com you can use %-4+ as a placeholder for everything before the root domain. If it has 4 parts, use %-5+.
<VirtualHost *:80>
VirtualDocumentRoot "/var/www/%-4+/webroot"
ServerName www.dev.example.com
ServerAlias *.dev.example.com
php_admin_value auto_prepend_file /var/www/setdocroot.php
</VirtualHost>
This way you can create a directory named /var/www/sub.domain/webroot and access it with the url sub.domain.dev.example.com.
The line php_admin_value auto_prepend_file /var/www/setdocroot.php fixes the docroot on some systems like OSX 10.9+
Here is the content of setdocroot.php :
<?php
$_SERVER['DOCUMENT_ROOT'] = str_replace($_SERVER['SCRIPT_NAME'], '', $_SERVER['SCRIPT_FILENAME']);
?>
What I am noticing with this configuration is that $_SERVER['DOCUMENT_ROOT'] is pointing to /var/www/html and not to the vhost's doc root.
weird.
Update (2010-07-24):
I just wrote a blog post how to setup your http proof server
http://www.devcha.com/2010/07/how-to-setup-your-http-proof-server.html