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.
Related
Please i need help, I have gone through a couple of similar issues and how they were resolved and it seems mine is such a pain it just wont work.
I used a Turnkey Linux Appliance [OrangeHRM] and i want to use its Apache server to host another site/software [Sentrifugo] and it is so difficult, each time i host it only the default site shows up.
i have read through several similar issues here on stack overflow and it didn't work, i need someone to please take a look at my case.
Sincere appreciations in anticipation
Below is the output of grep "<VirtualHost" /etc/apache2/sites-enabled/*
/etc/apache2/sites-enabled/adminer.conf:<VirtualHost *:12322>
/etc/apache2/sites-enabled/orangehrm.conf:<VirtualHost *:80>
/etc/apache2/sites-enabled/orangehrm.conf:<VirtualHost *:443>
/etc/apache2/sites-enabled/sentrifugo2.conf:<VirtualHost *>
Good morning, based on what we discussed:
you have 4 VH (VirtualHost): *:12322, *:443, *:80, *
*:12322 is not a default port, it would have to be specified explicitly (http://SOMETHING:12322/...)
*:443: default SSL port (https://SOMETHING/...)
*:80: default http port (http://SOMETHING/...)
*: matches all ports
Now you are using http://IP/sentrifugo2/...
Using the IP address, Apache will check the VH in order and use the first one that matches.
If you ask for http://IP/..., since this is port 80 (default for http), Apache will match the third VH (the *:80 one).
But here you want the 4th one, the one with *
Solutions:
Works, but is not the best: define your 4th VirtualHost with a port. <VirtualHost *:80>. Then modify the order in which your VH appear in Apache and put the sentrifugo VH before the orangehrm.conf one. But that could create a new problem where the orangehrm.conf is not usable anymore... Keep reading...
Best: use name virtual hosting:
<VirtualHost *:80>
ServerName orangehrm.conf
...OTHER CONFIGURATION DIRECTIVES...
</VirtualHost>
<VirtualHost *:80>
ServerName sentrifugo
...OTHER CONFIGURATION DIRECTIVES...
</VirtualHost>
So you see, this way, Apache can diffentiate between the two *:80, using the requested domain (or name). On your client system, in /etc/hosts, define the two names:
10.0.0.18 orangehrm.conf
10.0.0.18 snetrifugo
So when you type http://orangehrm.conf or http://sentrifugo Apache will know which one you want.
Note: on Apache 2.4, this works no problem. On Apache 2.2, you need to specify NameVirtualHost directive (see Apache documentation on this).
Again works but not the best: might not be possible for you. You can split between the VH using a different IP address. Ex. 10.0.0.18 is orangehrm.conf and <VirtualHost 10.0.0.18:80> is used. Then 10.0.0.19 is sentrifugo, so <VirtualHost 10.0.0.19:80> is used (with Listen 10.0.0.19:80). But you must be allowed to configure extra IP addresses on your host for this...
another possibility: split on the port. <VirtualHost *:80> is for orangehrm.com, <VirtualHost *:81> is for sentrifugo. For this, you need to specify http://10.0.0.18:81/sentrifugo. This also requires Listen *:80 and Listen *:81.
In conclusion Apache needs a way to know which one you want. It can decide with: IP, Port, Name. Name is the most flexible.
IMHO using IP address is not the way to go. It works in simple setups, but quickly limits you. Use domain names. Here I showed you how to do it in your local host file, the "best-best" way to do is to set them up in DNS.
I'm trying to get Apache 2.4.10 on Debian 8 "Jessie" up and running with multiple websites hosted on it. This might be an obvious and already answered question but I've never had the need to set-up a dedicated web host (usually just drop a WAMP server for development needs or pick up a web hosting service) and so far I have not had any luck finding an answer to my problem (I've found the complete opposite answers of what I'm trying to achieve). I need to get this working because apart from just hosting a couple of websites, there will be additional software set-up, for which, a regular web hosting service won't do.
Everything seems to be working as intended but the only problem is that I can't seem to find an optimal configuration which wouldn't just block access to default vhost with 403 - Forbidden. What I need is Apache to ignore requests (not just return a 404 document but tell the browser there's nothing there) from anyone accessing the default vhost or by accessing the server directly through it's designated IP. The designated IP should be left for SSH access only (since I don't have any kind of physical access to this server).
Basically, the web server should be accessible from a web browser through "FQDN-1" and "FQDN-2" (each located in their individual directories) and access to any other web address on this server should be ignored (invoking browser "404 not found" instead of returning a server error document, which would indicate that something is there).
my current vhost files:
<VirtualHost *:80>
ServerName FQDN-1
ServerAlias www.FQDN-1
ServerAdmin mail#FQDN-1
DocumentRoot /var/www/FQDN-1/public_html
ErrorLog /var/www/FQDN-1/logs/error.log
CustomLog /var/www/FQDN-1/logs/access.log combined
</VirtualHost>
And
<VirtualHost *:80>
ServerName FQDN-2
ServerAlias www.FQDN-2
ServerAdmin mail#FQDN-2
DocumentRoot /var/www/FQDN-2/public_html
ErrorLog /var/www/FQDN-2/logs/error.log
CustomLog /var/www/FQDN-2/logs/access.log combined
</VirtualHost>
The default vhost has been disabled through "a2dissite 000-default"
Iptables block everything except tcp port 80 and 22 (SSH access is whitelisted in iptables to just few specific iPs).
You can let the built-in name-based vhosting do the work for you. You can simply setup an additional (non default / non-first listed for *:80) virtualhost with
ServerName your-ip
and put a simple rule like
RewriteEngine ON
RewriteRule ^ .* [F]
or
Redirect 403 /
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 have installed CentOS 6.5 on my VPS (IP 149.210.167.9). Eventually I want to serve multiple domains, but for now I only want to handle 1 domain in particular: aforismo.eu. When you surf to the IP directly it shows me an Apache test page, so that works. When I ping aforismo.eu it shows me the correct IP, so I think the DNS is setup correctly. If I'm missing something trivial, please teach me :)
What I did so far to handle this domain:
I) I've created /etc/httpd/conf.d/site-aforismo.conf
<VirtualHost *:80>
ServerAdmin webmaster#aforismo.eu
DocumentRoot /var/www/aforismo.eu/public/
ServerName aforismo.eu
ServerAlias *.aforismo.eu
ErrorLog logs/aforismo.error_log
CustomLog logs/aforismo.access_log common
</VirtualHost>
II) Modified a few bits in /etc/http/conf/httpd.conf:
Include conf.d/*.conf
NameVirtualHost *:80
III) Afterwards I've restarted apache
service httpd restart
IV) Checking httpd -S results in:
wildcard NameVirtualHosts and _default_ servers:
_default_:443 vps.vanoosten.me (/etc/httpd/conf.d/ssl.conf:74)
*:80 is a NameVirtualHost
default server aforismo.eu (/etc/httpd/conf.d/site-aforismo.conf:1)
port 80 namevhost aforismo.eu (/etc/httpd/conf.d/site-aforismo.conf:1)
wild alias *.aforismo.eu Syntax OK
But ... surfing to http://www.aforismo.eu (or http://aforismo.eu) does not work. Since it's not my everyday business, I'd sure miss something quite trivial, but I can't find out what it is. Can you help me a bit further? Much appreciated.
You may need to add Listen *:80.
You may also need to add UseCanonicalName off under <VirtualHost *:80>.
Edit:
Unless it's meant to be internal only, there is something wrong with your DNS. I can hit the IP but aforismo.eu doesn't resolve to anything.
Update: As you guys suggested, it turns out to be a DNS issue. I thought I had created an A record to the given IP. But ... in the interface of my DNS provider I had to use the #-character for the domain itself instead of to really write it down. So what I really created was an A-record for aforismo.eu.aforismo.eu. Fixed (and next time I promise to read the manual :P)
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