Default site when using apache virtual hosts - apache

I am hosting two domains using the virtual host directive
<VirtualHost *:80>
ServerAdmin xy
ServerName xyz
ServerAlias www.xyz
DocumentRoot /var/www/sites/xyz
<Directory /var/www/sites/xyz>
Options -Indexes +FollowSymLinks +MultiViews
AllowOverride All
Require all granted
</Directory>
LogLevel warn
</VirtualHost>
<VirtualHost *:80>
ServerAdmin xy
ServerName xyz2
ServerAlias www.xyz2
DocumentRoot /var/www/sites/xyz2
<Directory /var/www/sites/xyz2>
Options -Indexes +FollowSymLinks +MultiViews
AllowOverride All
Require all granted
</Directory>
LogLevel warn
</VirtualHost>
When I access the site using the bare IP address I am directed to the first virtual host xyz. I would like to be directed to the default webiste /var/www/html
how can I achieve that? If I create a virtual host with the server IP address ... what would the server name be?

Just use the IP as the server name.
<VirtualHost *:80>
ServerName 12.345.67.89
DocumentRoot /var/www/html
<Directory /var/www/html>
...
</Directory>
</VirtualHost>
Or you could leave it blank to act as a default as per Stasik's answer.

Look here and search for "Main host goes away". Just add a new first virtual host without name and alias, it will match the requests that are not mached by any following vitrtual hosts.

Related

Apache Virtual Hosts for Two Sites Not Working Properly

I am trying to have two separate sites with Apache virtual hosts on a test server. I am going to access the sites with the ip address of the instance (for example, http://167.275.122.215). When I enable the following configuration, I would be able to load the first site with just the ip address correctly (with http://167.275.122.215), but not http://167.275.122.215/exp. I get a 404 error when I point to that address. What am I doing wrong here?
<VirtualHost *:80>
ServerAdmin webmaster#localhost
DocumentRoot /var/www/html/main
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
<VirtualHost *:80>
Alias /exp /usr/share/wordpress
DocumentRoot /usr/share/wordpress
<Directory /usr/share/wordpress>
Options FollowSymLinks
AllowOverride all
DirectoryIndex index.php
require all granted
</Directory>
</VirtualHost>
In fact, my need is to be able to access the exp (experimental) site through the same ip while the main site is still available (from the original ip address). I tried giving a different port to the experimental site too (like below), but that still gave me a site can't be reached error.
<VirtualHost *:90>
<Directory /usr/share/wordpress>
Options FollowSymLinks
AllowOverride all
DirectoryIndex index.php
require all granted
</Directory>
</VirtualHost>
For port based multiple sites, listing the default port as the second site resolved the issue:
LISTEN 90
<VirtualHost *:90>
DocumentRoot /usr/share/wordpress
<Directory /usr/share/wordpress>
Options FollowSymLinks
AllowOverride all
DirectoryIndex index.php
require all granted
</Directory>
</VirtualHost>
<VirtualHost *:80>
ServerAdmin webmaster#localhost
DocumentRoot /var/www/html/main
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Apache VirtualHost redirection from different subdomains

I have a DO droplet (Ubuntu 18.04) on which I want to host two sites. Let's say the droplet has an IP of 101.1.1.1. Now I want the sites to be pointed from another server (with different IP, let's say 104.1.1.1.) subdomain. Let's say siteone.example.org and sitetwo.example.org. So I follow the guides and set my Apache VirtualHost like this:
<VirtualHost *:80>
ServerAdmin webmaster#example.org
ServerName siteone.example.org
ServerAlias www.siteone.example.org
DocumentRoot /var/www/siteone/public_html
<Directory /var/www/siteone/public_html/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
However, when I press siteone.example.org in my browser I get no response. I've set A name in both to point ends to point to 101.1.1.1. Is there something I'm doing wrong?
You want 2 web sites on the same machine, each with an IP address? So:
configure both IP on your system
Set both IP:80 in Listen
Configure one VirtualHost per IP / domain
Like so:
Listen *.80
<VirtualHost 101.1.1.1:80>
ServerName siteone.example.org
ServerAlias www.siteone.example.org
ServerAdmin webmaster.example.org
DocumentRoot "/var/www/siteone/public/html"
<Directory /var/www/siteone/public_html/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/siteone_error.log
CustomLog ${APACHE_LOG_DIR}/siteone_access.log combined
</VirtualHost>
<VirtualHost 104.1.1.1:80>
ServerName sitetwo.example.org
ServerAlias www.sitetwo.example.org
ServerAdmin webmaster.example.org
DocumentRoot "/var/www/sitetwo/public/html"
<Directory /var/www/sitetwo/public_html/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/siteotwo_error.log
CustomLog ${APACHE_LOG_DIR}/sitetwo_access.log combined
</VirtualHost>
In your DNS, configure:
101.1.1.1 siteone.example.org www.siteone.example.org
104.1.1.1 sitetwo.example.org www.sitetwo.example.org

Name and IP Virtual Host on Apache

I am trying to direct web traffic for our domain to a Vhost site and our internal web site to separate directory that is only accessible within our private network,i.e 192.168.x.x.
I have modified Vhost configuration on Apache to include a name-based Vhost for the external website and a IP Vhost for the Intranet. No far I have had no luck, Apache does not like it.
Here is my modified Vhost config file.
NameVirtualHost *:80
<Directory "/home/webs">
Options +FollowSymLinks +Includes
AllowOverride All
Order allow,deny
Allow from all
</Directory>
<VirtualHost *:80>
DocumentRoot /home/webs/foo
ServerName www.foo.com
ServerAlias foo.com
LogLevel warn
ErrorLog /home/webs/foo/error.log
CustomLog /home/webs/foo/logs/access.log combined
</VirtualHost>
NameVirtualHost 192.168.0.*:80
<Directory "/home/webs/OffCat">
Options +FollowSymLinks +Includes
AllowOverride All
Order allow,deny
Allow from all
</Directory>
<VirtualHost 192.168.0.*:80>
DocumentRoot /home/webs/OffCat
ServerName 192.168.0.15/OffCat
LogLevel warn
ErrorLog /home/webs/OffCat/logs/error.log
CustomLog /home/webs/OffCat/logs/access.log combined
</VirtualHost>
I would appreciate any help.
Thanks,
Tony Cripps

Using a directory in VirtualHost ServerName

I'm currently using name-based virtual host configuration, to server about 5 different websites from the same IP address, just like in the apache documentation:
<VirtualHost *:80>
ServerName www.domain.tld
DocumentRoot /www/domain
</VirtualHost>
<VirtualHost *:80>
ServerName www.otherdomain.tld
DocumentRoot /www/otherdomain
</VirtualHost>
Is it possbile to have something like:
<VirtualHost *:80>
ServerName www.domain.tld/folderpath
DocumentRoot /www/software
</VirtualHost>
The webpages in this folder are using a different software stack, and I'd like to keep it nicely separate. I tried the method above but it didn't work.
It's not possible the way you show - a VirtualHost is always just a host. But you could use an Alias.
<VirtualHost *:80>
ServerName www.domain.tld
DocumentRoot /www/domain
Alias /folderpath /www/software
</VirtualHost>
Is it possible to have a different vhost for each application like that:
<VirtualHost *:80>
ServerName www.domain.tld
DocumentRoot /www/domain
</VirtualHost>
<VirtualHost *:80>
ServerName www.domain.tld
Alias otherApp /www/otherApp
</VirtualHost>
I add to the alias.conf file (on a windows machine).
Remember that if it outside the 'document root' path, you'll need permissions
<IfModule alias_module>
#### FolderPath ####
Alias /folderpath "E:/any/path/you/like"
<Directory "E:/any/path/you/like">
Options Indexes MultiViews
AllowOverride None
Order allow,deny
Allow from all
</Directory>
#### Another ####
Alias /another "E:/another/different/path"
<Directory "E:/another/different/path">
Options Indexes MultiViews
AllowOverride None
Order allow,deny
Allow from all
</Directory>
</IfModule>

How to set up virtual hosts on Apache 2.2

Can anyone direct me to a good tutorial on how to set up virtual hosts using Apache 2.2? Here's my situation:
I have Apache running on my laptop and I want two websites-- one on port 80 and one on port 8089. I want to access each site from the other computer on my network by entering the computer's IP address, such as http://192.168.1.102 and http://192.168.1.102:8089. Yet when I enter the second url, it directs me to the website running on port 80.
Thanks in advance for any help.
First you need to instruct Apache to listen on the ports you need:
Listen 80
Listen 8089
Second you need to tell it what to do with 80 and 8089 traffic:
<VirtualHost *:80>
DocumentRoot /website/site80
ServerName internet.dev
</VirtualHost>
<VirtualHost *:8089>
DocumentRoot /website/site8089
</VirtualHost>
Third you need to "allow" Apache to use those directories:
<Directory "C:/website/site80">
Options Indexes FollowSymLinks Includes ExecCGI
AllowOverride All
Order allow,deny
Allow from all
</Directory>
<Directory "C:/website/site8089">
Options Indexes FollowSymLinks Includes ExecCGI
AllowOverride All
Order allow,deny
Allow from all
</Directory>
Just have 2 virtual hosts defined like this, but with differeing DocumentRoots:
<VirtualHost *:80>
ServerAdmin webmaster#dummy-host.somecompany.com
DocumentRoot "/docs/dummy-host.somecompany.com"
ServerName dummy-host.somecompany.com
ServerAlias www.dummy-host.somecompany.com
ErrorLog "logs/dummy-host.somecompany.com-error.log"
CustomLog "logs/dummy-host.somecompany.com-access.log" common
</VirtualHost>
<VirtualHost *:8089>
ServerAdmin webmaster#dummy-host.somecompany.com
DocumentRoot "/docs/dummy-host.somecompany.com"
ServerName dummy-host.somecompany.com
ServerAlias www.dummy-host.somecompany.com
ErrorLog "logs/dummy-host.somecompany.com-error.log"
CustomLog "logs/dummy-host.somecompany.com-access.log" common
</VirtualHost>