Apache VirtualHost : multiple sites on same IP - apache

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.)

Related

Include external file in apache conf

I am running apache 2.4 on my web servers and I am always trying to find ways to streamline. Is it possible to include a conf file for every Joomla website and another one for every Wordpress site that I host? I put the Joomla .htaccess configuration (https://docs.joomla.org/Special:MyLanguage/Preconfigured_htaccess) inside of the domainname.com.conf file and specify Allowaccess none for performance reasons.
It would be great to have a single file for different versions of Joomla, Wordpress or other apps that require Apache configurations instead of needing to edit dozens of conf files when the app requirements change.
I found the include directive from Apache, but not sure if it would work on an individual vhost. http://httpd.apache.org/docs/2.4/mod/core.html#include
The Include does work for individual vhosts.
The documentation you linked (https://httpd.apache.org/docs/2.4/mod/core.html#include) states this in the header block of the section:
Context: server config, virtual host, directory
That means that the "Include" directive can be used, amongst other places, in the virtual host section of the configuration.
See here for a definition of the contexts: https://httpd.apache.org/docs/2.4/mod/directive-dict.html#Context
So you could do this:
<VirtualHost *:80>
ServerName joomla1.example.com
Include "conf/joomla.conf"
</VirtualHost>
<VirtualHost *:80>
ServerName joomla2.example.com
Include "conf/joomla.conf"
</VirtualHost>
<VirtualHost *:80>
ServerName wordpress1.example.com
Include "conf/wp.conf"
</VirtualHost>
<VirtualHost *:80>
ServerName wordpress2.example.com
Include "conf/wp.conf"
</VirtualHost>
joomla.conf and wp.conf would contain the directives that are common to either Joomla or Wordpress.

Multiple web applications with Apache 2.4

I want to have two webapps (webapp1 and webapp2 resident under /var/www/html/webapps/), both using PHP and JSP, running on the same machine:
Apache 2.4
Tomcat 7.0.50 (+APJ connector)
and want to make them accessible through the following URLs (with identical IP and ports):
localhost/webapp1
localhost/webapp2
I am aware of Virtual Hosts facility. The problem is that Apache seems to "see" only the first site available: whenever I look for localhost/webapp2, I get a 'Not Found' error. Note that if I look for "localhost:8080/webapp2" (i.e., bypassing apache2) everything works fine.
Each webapp has its own conf file under sites-available directory. For example, in webapp2.conf I have
JkMountCopy On
JkMount /webapp2/* tomcat_worker
How can I solve?
From the documentation
Note
Creating virtual host configurations on your Apache server does not magically cause DNS entries to be created for those host names. You must have the names in DNS, resolving to your IP address, or nobody else will be able to see your web site. You can put entries in your hosts file for local testing, but that will work only from the machine with those hosts entries.
Listen 80
Listen 8080
<VirtualHost 172.20.30.40:80>
ServerName www.example.com
DocumentRoot "/www/domain-80"
</VirtualHost>
<VirtualHost 172.20.30.40:8080>
ServerName www.example.com
DocumentRoot "/www/domain-8080"
</VirtualHost>
<VirtualHost 172.20.30.40:80>
ServerName www.example.org
DocumentRoot "/www/otherdomain-80"
</VirtualHost>
<VirtualHost 172.20.30.40:8080>
ServerName www.example.org
DocumentRoot "/www/otherdomain-8080"
</VirtualHost>
If you want additional help, show us your configuration files related.

NameVirtualHost directive equivalent in Apache 2.4.6?

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.

How do you set the default website to serve when your IP address is entered as URL?

I have a server with multiple websites hosted and distinguishable using name-based virtual hosting of apache.
How do I set it so that a specific website is hosted when the ip of my server is entered in the address bar?
What you want to use is the _default_ VirtualHost.
<VirtualHost _default_:80>
DocumentRoot /www/default80
# ...
</VirtualHost>
It's described here. Basically if nothing else match the request the _default_ host will be used.
EDIT
This could also be written as:
<VirtualHost *>
DocumentRoot /www/default
# ...
</VirtualHost>
Is is important that this is the first VirtualHost in the configuration since Apache will start matching them from top to bottom, selecting the one that fit the best based on ServerName and ServerAlias.
This post might also be of interest:
Apache default VirtualHost
just find the Include sites-enabled/ line in your apache2.conf file and add the path to the conf file you want to be site default above it. from:
Include sites-enabled/
to
Include sites-enabled/mydefault.conf
Include sites-enabled/
When you first install apache2, there is a site configuration file named 000-default.conf. This is going to be the default because it is very likely to appear first in the list of files under /etc/apache2/sites-enabled.
To have your own file as the default, you can either replace the file under /etc/apache2/sites-available/000-default.conf with your own, or replace the link like so:
sudo rm /etc/apache2/sites-enabled/000-default.conf
sudo ln -s ../sites-available/my-site-setup.conf /etc/apache2/sites-enabled/000-default.conf
Then restart apache2 (or just reload).
The _default_ as mentioned by the other answer is for defining a virtual host which can be found with the default IP address. It's not the default virtual host.
<VirtualHost _default_:80>
...
is equivalent to
<VirtualHost *:80>
...
The * is a globing pattern which matches any IP addresses.
Note:
Replacing the 000-default.conf file is okay, but in most cases the installation package is going to view that as a modified file and manage it in some weird way which is why I think it's cleaner to only change the soft link.
Keep it clean, don't delete or edit anything in /etc/apache2/sites-available/.
Create all new site configurations in /etc/apache2/sites-available/. Copy whatever site configuration you want enabled to /etc/apache2/sites-enabled/. Only make sure /etc/apache2/sites-enabled/ has only one configuration file.
Sample format for new apache site configurations in Ubuntu 20.04 LTS is
<VirtualHost *:80>
ServerName http://localhost
ServerAdmin admin#mysite.com
DocumentRoot /var/www/html/mysiteroot/public
<Directory /var/www/html/mysiteroot>
AllowOverride All
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Notice that 000-default.conf is by default in both directories mentioned above and should only be replaced by your new configuration in /etc/apache2/sites-enabled/ so that it can be restored anytime you need it.
Restart Apache2 service after you make any configuration changes.

Running many websites on one server

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