This may be a duplicate question, but i have been thinking about it for long. I know, apache supports hosting many websites on a single server. But i want to know the implementation.
The server will have the single IP address. TCP is always port 80. Then how is it possible to run 10 different websites on a single machine. Also DNS, has one-to-one mapping.
I am thinking, probably some tweaking is done in HTTP protocol, but cant think of exact and best possible solution .
Thanks
You can add many VirtualHost entries in your Apache config as follows:
<VirtualHost *:80>
ServerName www.domain.tld
ServerAlias domain.tld *.domain.tld
DocumentRoot /www/domain
</VirtualHost>
<VirtualHost *:80>
ServerName www.otherdomain.tld
DocumentRoot /www/otherdomain
</VirtualHost>
This basically prompts Apache to respond differently, serving different documents based on which domain was requested.
More information can be found in the Apache docs: http://httpd.apache.org/docs/2.2/vhosts/name-based.html
Related
Overview of new features in Apache HTTP Server 2.4 states:
NameVirtualHost directive:
No longer needed and is now deprecated.
Can someone please explain the virtual host equivalent syntax to produce this behaviour in the new version of Apache?
<VirtualHost 127.0.0.3:80>
DocumentRoot /var/www/html3
ServerName site3.com
</VirtualHost>
<VirtualHost 127.0.0.3:80>
DocumentRoot /var/www/html4
ServerName site4.com
</VirtualHost>
My apologies everyone but I have since destroyed this server and so cannot supply the config file :(
Just FYI I have since seen an example online which placed the server name in the VirtualHost header as seen below which may have been the problem, though I have no way of knowing this until I get an opportunity to test it at some point in the future/
<VirtualHost site3.com:80>
DocumentRoot /var/www/html3
</VirtualHost>
Your configuration is correct and automatically behaves as if "NameVirtualHost 127.0.0.3:80" was present. If http://site4.com appears to be the default virtualhost:
make sure you're actually testing with "http://site4.com"
confirm it's not browser cache
try a command-line client
make sure the content really differs on disk
I agree with #covener. Apache enables SNI based on how many valid vhost blocks it detects for an IP address/port combo. Your question is best answered on apache's official Documentation:
https://httpd.apache.org/docs/2.4/vhosts/details.html
However, for full NameVirtualHost behavior, either pick an internal IP that is routing traffic to your server (use the same IP in all vhost config blocks), or use the wildcard (*):
<VirtualHost *:80>
<VirtualHost *:443>
I, personally, use the wildcard syntax and have roughly 10 different vhost configurations on my own server.
EDIT:
I just realized this post is 4 years old.
I've been trying to create 3 different domains linking to 3 different sites on the same machine, 2 which works but the third on the different port links to the first page.
My apache config looks like this:
Listen 81
NameVirtualHost *:81
<VirtualHost *:80>
ServerName www.example.com
DocumentRoot /var/www/www
</VirtualHost>
<VirtualHost *:80>
ServerName www2.example.com
DocumentRoot /var/www/www2
</VirtualHost>
<VirtualHost *:81>
ServerName controlpanel.example.com
DocumentRoot /var/www/controlpanel
</VirtualHost>
I've used Bind9 to set up the domains.
www IN A 123.123.123.123
www2 IN A 123.123.123.123
controlpanel IN A 123.123.123.123
www and www2 works fine and shows the correct site, however controlpanel.example.com also links to the first www site. When I enter the port manualy on the ip, xxx.xxx.xxx.xxx:81, i get linked correctly. The thing is that I don't really know where I'm doing it wrong, this is the first time I'm trying anything like this. You got any ideas?
Im also running this on an old ubuntu 12.04 server.
Regarding where you're going in the comments for the previous answer:
You could add a port 80 virtualhost for controlpanel.example.com and put a single statement inside,
Redirect / http://controlpanel.example.com:81
The purpose of the ServerName is not to inform the browser what port your webserver is using. It's used for name-based virtualhosts and as a last resort for self-referential links (out of the box, self-referential links are generated with whatever the client already thought it was accessing via the Host: header)
But there is definitely something quite bizarre about your requirement. Usually the motivation is to not use custom ports, and if they are, to address the server with a low port and have the por remapped by some intermediary (load balancer, proxy).
If you want your third virtualhost to be simulataneously the defautl on port 81 and a name-based option on port 80:
Change
<VirtualHost *:81>
to
<VirtualHost *:80 *:81>
Apache finds the set of virtual hosts with the best IP:PORT based match first, then if NameVirtualHost also matches, starts looking at the ServerNames from that set.
Let assume that blah.com, blah2.com all point to the same server with IP=5.31.111.7.
I would like that:
accessing blah.com serves /var/www/site1
accessing blah2.com serves /var/www/site1
accessing 5.31.111.7 serves /var/www/site2
I tried
<VirtualHost *:80>
DocumentRoot /var/www/site1
</VirtualHost>
<VirtualHost 5.31.111.7:80>
DocumentRoot /var/www/site2
</VirtualHost>
but now everything goes to /var/www/site2, which is not what I wanted.
How to configure the VirtualHost, such that the served website depends on the URL ?
PS: why should I do this in /etc/apache2/sites-enabled/000-default instead of /etc/apache2/apache2.conf ? I don't understand this sites-enabled / sites-available/default naming... Why are there so many different config files by default on Debian, for such a simple thing?
What you want to do is called Name-Based Virtual Hosting, you'll need
NameVirtualHost *:80
to enable it on port 80, and for each VirtualHost, you need to give the name(s):
<VirtualHost *:80>
ServerName blah2.com
ServerAlias www.blah2.com
DocumentRoot /var/www/site1
</VirtualHost>
Note that there are limitations on SSL/TLS when doing name-based virtual hosting, but it's a bit of a moot point since post-POODLE, people start to require TLS anyway, so ancient browsers are out of luck anyway.
As to the config files, it's very very useful to have two classes of config files: the ones with defaults that a package update will overwrite, and your local ones that it will not touch, or better even, a directory full of the former and a directory full of the latter. (Because additional packages might want to make configuration settings, they'll all install in the former place, and you should only ever change/override config in the second place.)
I've got a pretty straightforward issue with a linux based Apache 2.2 server I am setting up. I want to setup two totally different domains on the same server.
But it only serves content from the first tag! I've searched StackOverflow and read items at Apache.org but no luck.
I followed the directions on Apache.org and put these two sections at the bottom of my http.conf file.
<VirtualHost *:80>
DocumentRoot /var/www/mydomain1
ServerName sub1.mydomain1.com
</VirtualHost>
<VirtualHost *:80>
DocumentRoot /var/www/mydomain2
ServerName sub2.mydomain2.com
</VirtualHost>
Now when I use a browser to go to: http://sub1.mydomain1.com it comes up fine. But if I go to http://sub2.mydomain2.com I still only get the content that located in /var/www/webfiles/mydomain1.
I did many of the obvious things such as:
- service httpd restart
- I changed the order of the two entries in my httpd.conf and once again, it only serves the first one in the list.
- One support doc I had Googled said to make sure to have the following entry point to a valid domain on your system. So I entered this (but it didn't change anything):
ServerName sub2.mydomain2.com:80
It must be something silly but I can't figure it out!
Ok, I figured it out. It was pretty silly. I just needed to uncomment this line so I would actually use all the virtual hosts:
NameVirtualHost *:80
You need to set up the two domains in two separate virtual hosts. Generally when I do this I like to split off an include directory full of virtual host files, with each file containing one virtual host.
<VirtualHost *:80>
ServerName site1.com
DocumentRoot "/var/www/site1"
</VirtualHost>
<VirtualHost *:80>
ServerName site2.com
DocumentRoot "/var/www/site2"
</VirtualHost>
I have been working on a drupal test site for a while, which has a bunch of virtual hosts set up like this:
<VirtualHost *:80>
ServerAdmin email#example.com
DocumentRoot "/path/to/root"
ServerName testsite1.example.com
</VirtualHost>
I have been using a modified host file to view each of these test sites, along the lines of:
12.0.0.1 localhost
20.02.2.22 testsite1.example.com
20.02.2.22 testsite2.example.com
20.02.2.22 testsite3.example.com
This has worked fine, however now I need to send the sites over to some people remotely who are not technical enough to modify their own host files and see it the way I do.
Is there a way I could set up Apache so that the url "http://20.02.2.22/testsite1" would forward to testsite1.example.com internally? I am using Drupal, and the site setup needs to see the "testsite1.example.com" so that it can correctly choose the instance to select. I have been looking through apache rewrite, but I am a bit of a newb at this so any help is much appreciated.
testsite1.example.com will only be resolved on your machine, so you cannot redirect. You can set up proxy with mod_proxy. Hope this works for you:
<VirtualHost *:80>
ServerAdmin email#example.com
DocumentRoot "/path/to/root"
ServerName testsite1.example.com
ServerAlias 20.02.2.22
<Location /testsite1/>
ProxyPass http://testsite1.example.com/
</Location>
</VirtualHost>
The way I show my local test sites is a combination of Dynamic DNS and port-forwarding.
Internally, my Drupal site is at [my machine ip] or localhost.
I setup a free dynamic dns name to my IP and then on my router, accept incoming requests on port to route to [my machine ip]
That way, they can see yoursite.dyndns.com, but its looking at your local copy.
Why you just don't buy a new domain name and point it to your server IP address...
Or there are free domain solutions like:
http://www.dot.tk/en/index.html?lang=en
or
http://dyn.com/dns/
If you wanna buy new domain, i recommending you this one, cheap and quality service: http://server2.elite7hackers.us/recommend.php?site=godaddy