Apache Virtual Host IP address points to another DocumentRoot - apache

I installed a VPS on Vultr running Ubuntu 16.04 and using virtual hosts.
There are two domains (abc.com & xyz.com) points to two different dictionary, and now it works fine with Let's Encrypt (SSL).
I can type domain name to access these two websites.
The virtual host file configurations in /etc/apache2/sites-available are:
abc.conf
ServerName abc.com
DocmentRoot /var/www/abc
and
XYZ.conf
ServerName xyz.com
DocumentRoot /var/www/xyz
However, this is the question I have now. Occasionally I found if I type IP address:
456.456.456.456/xyz
I thought it will point to the homepage of xyz.com, but it is not.
The webpage is showing and pointing to an error 404 pages (non-SSL) in abc.com
I want to make it to xyz.com, how do I make it right?
Why does it point to another document root?
I checked all abc.conf abc-le-ssl.conf xyz.conf xyz-le-ssl.conf configurations in /etc/apache2/sites-available.

Related

How to access Virtual Hosts setup on the VM from the Host Machine?

I want to access my virtual hosts (websites) that I've set up in my VirtualBox -> Ubuntu-Server 18. I have apache2 installed and everything is setup.
The host machine (macOS) can presently gain access to the default website of apache2 in the guest (Ubuntu-server) through a Port Forwarding rule I have assigned in the VirtualBox Network Settings:
127.0.0.1:8000 -> 10.0.2.15:80
I've set up a virtual host in my guest machine with the following configuration:
<VirtualHost *:80>
ServerName dropn.taxi
ServerAdmin admin#dropn.taxi
DocumentRoot /var/www/dropn.taxi/public_html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
The DocumentRoot directory mentioned above exists and has the files necessary to serve a basic "Hello World" home page. I've tested it with my apache installed in my local machine as well. The website's not the problem.
But everytime I access localhost:8000 from the local machine, I'm greeted with an Ubnutu-Apache greetings page. Whereas when I try localhost:8000/dropn.taxi, I get a 404 page. I've researched quite a bit and the closest I can get to a solution is what is suggested in this page:
Ubuntu Apache2 local virtual hosts url not found
Simply put: How can I access virtual hosts of the guest machine (VM) from the host machine?
Still not specific to my case. Would appreciate any help in this regards.
localhost in the apache2 server is set to point to /var/www/html by default. For the virtual hosts to work properly, the domain name accessing them must be what is defined in the ServerName field.
localhost:8000/dropn.taxi would translate to 127.0.0.1/dropn.taxi in the VM guest. Which means, it is trying to access the /var/www/html/dropn.taxi, which, ultimately does not exist, hence the 404 error.
The solution to this is to add dropn.taxi to the /etc/hosts file in both the Host Machine and the VM. The line would look like:
127.0.0.1 localhost dropn.taxi
While accessing from the Host Machine, we use dropn.taxi:8000 in the browser whereas the port :8000 is not used in the guest machine. This way, whenever accessing the virtual hosts, the domain name accessing them matches the ServerName field and it simply works!

Apache mass virtual host configuration issue

I'm brand new to VPS hosting, so bear with me.
I recently purchased a VPS through inmotion hosting. It has Apache, cPanel and WHM installed out of box. I'm not sure if this is important, but at the top right of the WHM root login it shows: CENTOS 7.3 x86_64
I'm working on developing a system where users can sign up with me and be provided with a sub domain like so: theirUsername.mydomain.com. This will point to a directory on my server where their files will be located. People should also be allowed to have a domain name with my nameservers. I need the system to automatically detect their domain name and point it to where their directory is. It's just like if I were to manually log into cPanel and create an addon domain, but I need this process to be automated. I also need the sub domain to remain and I need to be able to install SSL certs for these domains.
My system would be VERY similar to how Squarespace works.
I've read into mass virtual hosting but cannot for the life of me figure out how to get it working or if it would even be what I'm looking for.
I've tried configuring the virtual hosting but have had no luck. Just for testing purposes I was trying a single domain. Here is my code:
NameVirtualHost *:80
Listen 80
<IfModule mod_ssl.c>
# SSL name based virtual hosts are not yet supported, therefore no
# NameVirtualHost statement here
Listen 443
</IfModule>
<VirtualHost *:80>
DocumentRoot "/home/username/www/domain.com"
ServerName www.domain.com
ServerAlias domain.com
ErrorLog "/home/username/www/domain.com/error.log"
CustomLog "/home/username/www/domain.com/requests.log" common
</VirtualHost>
The log files are making it to that directory but visiting the domain gives me a "This site can’t be reached" error in Chrome.
Any tips would be greatly appreciated.
Thanks!

Apache2 and Vagrant: localhost mapping

I'm using Vagrant 1.6.3 for running Ubuntu 12.04 virtual machine. On this virtual machine I have Apache 2.4.9. For a long time Apache served only one site- there was 000-default.conf file in the /sites-available directory.
Now I want to add another site. I've created 2 files in /sites-available directory:
site1.local.conf
site2.local.conf
and I renamed 000-default.conf to default. Also I added site1.local and site2.local hosts to the ./etc/hosts file on the main machine.
Server names in Apache configs are set to site1.local and site2.local correspondently. And when I input http://site1.local:8080/ and http://site2.local:8080/ in browser everything works.
But I don't understand why http://localhost:8080/ still points to the first site? In what config should I define mapping for "localhost"? After setting up server names in apache configs I thought that http://localhost:8080/ should return "Can not connect to server" error.
Assuming you're using a name-based virtual host, site1 and site2 work because the client passes the target host in the header:
If the client provided a Host: header field the list is searched for a matching vhost and the first hit on a ServerName or ServerAlias is taken and the request is served from that vhost.
With localhost, it doesn't know how to match against the defined virtual host entries:
If no matching vhost could be found the request is served from the first vhost with a matching port number that is on the list for the IP to which the client connected (as already mentioned before).
As far as the last part of your question, you could play around with adding another virtual host entry with localhost as the name definition.

how to Redirect two URL's on same port to two diff IP's internally

These two machines A and B are NAT behind 1 IP address.
Url.xyz.com goes to machine A on port 80 served by apache 2.2
and url2.xyz.com is supposed to go to machine B on port 80 served by apache 2.2 on that machine
i have machine A taking all inbound port 80 packets from my gateway...fyi
at one point i had apache on machine A setup to do this, but now i am struggling
to regain the good config. I am using a diff OS on a diff Machine B for good reasons....
i recall setting up virtual server url2.xyz.com in apache 2.2 on machine A using
port 80, then setting the host file on machine A and B to have a line titled:
192.168.0.x url2.xyz.com url2
and setting the document root for url2.xyz.com in apache on machine A equal to the document root on machine B. (it won't take a blank field....)
I am either dreaming, or missing a slight setup step. Any help appreciated. As I recall, from about six months ago, it seemed that apache on machine A read the host file and did this right..
the net good result is (hopefully) that the virtual server on machine A gets content from machine B (or C, or D, etc...)
alternatively i will try to get another Ip address from my provider.
One possibility is to have machine A act as a proxy for machine B. Meaning all requests for both domains go to machine A on port 80, but then you set up name based virtual hosts in apache. The virtual host for url2.xyz.com then forwards on the request to machine B.
Something like this:
Virtual hosts on machine A:
<VirtualHost *:80>
ServerName url1.xyz.com
DocumentRoot /var/www/url1
</VirtualHost>
<VirtualHost *:80>
ServerName url2.xyz.com
ProxyPass / http://url2.machineB/
ProxyPassReverse / http://url2.machineB/
</VirtualHost>
And then on machine A you define url2.machineB to point to the IP address of machine B. You do this by adding a line to the file /etc/hosts. Then on machine B you create an apache virtual host to listen for that domain, like so:
<ViirtualHost *:80>
ServerName url2.machineB
DocumentRoot /var/www/url2
</VirtualHost>
This might not be exactly the solution, but should give you one option to take. See http://httpd.apache.org/docs/2.2/mod/mod_proxy.html for more on proxying.

Multiple Apache2 vhosts are pointing to the same website

I'm running Apache2 on Ubuntu 10, and I have my site configuration files laid out numerically and in order. My default server is psychedeli.ca, but I also run another site off the same box at mahoganytales.com. Currently, both of these domains point to the same site (the one for psychedeli.ca). The declaration NameVirtualHost *:80 is in my ports.conf file, so I'm pretty sure my global server config checks out. How can I fix this?
Here are my vhost files:
001-psycho
<VirtualHost *:80>
DocumentRoot /var/apps/psycho/public
ServerName psychedeli.ca
</VirtualHost>
002-mahogany
<VirtualHost *:80>
DocumentRoot /var/apps/mahogany/public
ServerName mahoganytales.com
</VirtualHost>
try create new conf file at /etc/apache2/conf.d, e.g., vhosts.conf
with this content in it:
NameVirtualHost *
It looks like the default configuration is in effect rather than your host entries. Following is the procedure that works in Ubuntu Apache2.
First,
create a VirtualHost in /etc/apache2/sites-available/somesite,
then a2ensite somesite to make it live.
Finally, /etc/init.d/apache2 restart to restart apache.
If you think, you have followed the above steps, then can you please confirm, that you have your hosts files in /etc/apache2/sites-enabled/?
Each domain name needs to have it's own single unique ip address, that's how different sites are found.
By using the *:80 in the virtual host directive, you're instructing Apache to listen on all IP addresses, port 80 and send it to this directory. With your second vhost, you're doing the same thing (All IP's port 80, and send it there). Well, since you're giving it two conflicting statements, it takes the first match, and uses it.
If you want to serve multiple websites, each must answer to it's own unique IP address, ie:
site aaa.com - 145.25.82.110
site bbb.com - 145.25.82.111
From there, each vhost entry will listen on it's own ip address and port for each site. In the OP's case the vhost needs to change to (using the example IPs):
&ltVirtualHost 145.25.82.110:80>
DocumentRoot /var/apps/psycho/public
ServerName psychedeli.ca
&lt/VirtualHost>
&ltVirtualHost 145.25.82.111:80>
DocumentRoot /var/apps/mahogany/public
ServerName mahoganytales.com
&lt/VirtualHost>
This instructs the server to listen on static IP 1 port 80 (as defined in the named.conf and associtated bind config files, and send it to the first site base directory, and any calls on the second static IP port 80 and send it to the second site base directory.
As for configuring bind/named, that's beyond the scope of this question...