How can I set up a load balancer for multiple virtual hosts (apache) - apache

I am trying to set up a load balancer for a couple of virtual hosts on my apache server.
These virtual hosts are added by adding the following lines for the file "C:\Windows\System32\drivers\etc\hosts":
127.0.0.1 localhost
127.0.0.1 vhosta
127.0.0.1 vhostb
127.0.0.1 vhostc
127.0.0.1 load-balancer
::1 localhost
Then I've added the following lines for the file "C:\xampp\apache\conf\extra\httpd-vhosts.conf":
<VirtualHost *:80>
DocumentRoot c:/xampp/htdocs
ServerName localhost
</VirtualHost>
<VirtualHost *:80>
DocumentRoot c:/vhosts/vhosta
ServerName vhosta
</VirtualHost>
<VirtualHost *:80>
DocumentRoot c:/vhosts/vhostb
ServerName vhostb
</VirtualHost>
<VirtualHost *:80>
DocumentRoot c:/vhosts/vhostc
ServerName vhostc
</VirtualHost>
<VirtualHost *:80>
DocumentRoot c:/vhosts/load-balancer
ServerName load-balancer
</VirtualHost>
And of course I've created the folders in C:/vhosts/ and added an index.php file to each one (with an echo statement inside).
Now, I can access the virtual hosts through my browser by visiting "http://vhosta" etc.
But what I need, is to make a load balancer that chooses to execute either "http://vhosta", "http://vhostb" or "http://vhostc".
How can I achieve this? And have I done everything correct so far?
Any help will be greatly appreciated!
Thanks in advance!
(i am using xampp on windows 8.1 btw.)

There many ways to accomplish this, but what you are trying to do it won't work. The /etc/hosts is a basic way of IP to host name mapping. If you want round-robin resolution you will have to use DNS server. Also, it doesn't make much sense to load balance on the same machine, except for learning and configuration testing.
These are some of the options you have.
1) Using mod_proxy_balancer. You need to enable mod_proxy and mod_proxy_balancer modules. Also, you need to pick one of the scheduler algorithms. Options are: mod_lbmethod_byrequests, mod_lbmethod_bytraffic, mod_lbmethod_bybusyness and mod_lbmethod_heartbeat.
http://httpd.apache.org/docs/2.4/mod/mod_proxy_balancer.html
<VirtualHost *:80>
...
ServerName load-balancer
<Proxy balancer://mybalancers>
BalancerMember http://vhosta:80
BalancerMember http://vhostb:80
BalancerMember http://vhostc:80
</Proxy>
ProxyPass / balancer://mybalancers
ProxyPassReverse / balancer://mybalancers
...
</VirtualHost>
2) Using DNS round-robin option. You need to point multiple IPs to the same host name. With this option, when you make a request to your load-balancer host, DNS server will give you next IP (in a round-robin fashion).
DNS configuration
load-balancer IN A 10.0.0.1
load-balancer IN A 10.0.0.2
load-balancer IN A 10.0.0.3
Virtual hosts for apache servers
<VirtualHost 10.0.0.1:80>
DocumentRoot c:/vhosts/vhosta
ServerName load-balancer
</VirtualHost>
<VirtualHost 10.0.0.2:80>
DocumentRoot c:/vhosts/vhostb
ServerName load-balancer
</VirtualHost>
<VirtualHost 10.0.0.3:80>
DocumentRoot c:/vhosts/vhostc
ServerName load-balancer
</VirtualHost>
And one more thing related to hosts file. If you want to map a loopback IP to hostname, feel free use full range, from 127.0.0.0 to 127.255.255.255. I'm pretty sure this is mapped in Windows, but I have no ways to test it. To test, just ping 127.1.2.3, and see what you get back.
http://en.wikipedia.org/wiki/Loopback
This is how you can organize your /etc/hosts file if you need multiple IPs for testing.
127.0.0.1 localhost
127.0.0.2 vhosta
127.0.0.3 vhostb
127.0.0.4 vhostc
127.0.0.5 load-balancer

Related

Apache VHost Intranet Setup

Hi I would like to ask some help on setting up my webserver to access over my network.
Basically I have more projects on the www folder. For example I have 2 website that I want to access on different machine.
Heres my vhost config.
NameVirtualHost *:80
<VirtualHost *:80>
ServerName website1
ServerAlias website1
DocumentRoot "/www/website1"
</VirtualHost>
<VirtualHost my_ip_add:8080>
ServerName website2
ServerAlias website2
DocumentRoot "/www/website2"
</VirtualHost>
And I also configure the /etc/hosts file.
127.0.0.1 localhost
127.0.0.1 website1
my_ip_add website2
What I want to is to access website2 from other machine.
What happen is when I put http://my_ip_add:8080/ on my browser it was "ERR_CONNECTION_REFUSED", but when I use http://my_ip_add/ it render website1.
How can I access the website2 on other machine? Is there is missing on my configuration?
I hope someone can help me on this. Thanks in advance.
Why so complex? Why don't you deliver both sites on the same port? That is what virtual hosts are for. You only have to take care to always request the two sites by their host name as resolved in your local name resolution...
Simplify your virtual hosts definition:
NameVirtualHost *:80
<VirtualHost *:80>
ServerName website1
DocumentRoot "/www/website1"
</VirtualHost>
<VirtualHost *:80>
ServerName website2
DocumentRoot "/www/website2"
</VirtualHost>
Your local name resolution should resolve both host names:
127.0.0.1 localhost
my_ip_add website1
my_ip_add website2
now you can make these requests from all systems with above name resolution:
http://website1
http://website2

mod_proxy apache vhosts.conf

sorry for my bad English, I'm gonna try to explain my problem.... I have to do a configuration of Apache for school. I want create a web server model with three host. I have three virtual machine on virtual box and each one can communicate with an internal network. Indeed three different apache server can be seen in each vm if I call it in browser.
Now I have to configure mod_proxy.
I want this configuration: the first vm is a server, responding a specifical domain, from this server I want to reach the other 2 apache from the other 2 different vm. Server localhost ip address 192.168.1.100 vm01 localhost/vm01 link to ip address 192.168.1.101 vm02 localhost/vm02 link to ip address 192.168.1.102
So, I spend few days in apache mod_proxy but I can't find a perfect guide or example.
I try to use this vhosts.conf in server, but didn't work. Please be patient I'm new in Apache.
<VirtualHost *:8080>
ServerName localhost
DocumentRoot /home/francesco/proxy/htdocs/
</VirtualHost>
<VirtualHost *:8080>
ServerAdmin webmaster#proxy.com
ServerName www.vm01.com
ProxyPass /vm01 http://192.168.1.101
ProxyPassReverse /vm01 http://192.168.1.101
</VirtualHost>
You have defined two Virtual Hosts on port 8080. Combine it to one.
<VirtualHost *:8080>
ServerAdmin webmaster#proxy.com
ServerName www.vm01.com
ProxyPreserveHost On
ProxyPass /vm01 http://192.168.1.101
ProxyPassReverse /vm01 http://192.168.1.101
</VirtualHost>

How to customise my URL which is having IP Address

Is it possible to configure my URL which has my IP address on it- like: "http://192.168.xx.yy/index.php". The situation is when I run Apache server in my PC, and load localhost in it. I know it is possible after hosting with external server, but is there any way we can configure within our localhost?
How to configure the Apache files to make this happen? I tried in my localhost, editing the "httpd.conf" by adding this inside like this - please tell me where I am getting the issue!
ServerName localhost:80
HostnameLookups Off
<VirtualHost *:80>
# This first-listed virtual host is also the default for *:80
ServerName www.example.com
ServerAlias example.com
DocumentRoot /www/domain
</VirtualHost>
<VirtualHost *:80>
ServerName other.example.com
DocumentRoot /www/otherdomain
</VirtualHost>
DocumentRoot "c:/wamp/www/"
Yes, you can play with multiple IP addresses on your machine. Configuration depends on your OS. Article Create Multiple IP Addresses to One Single Network Interface is for linux.
But, better way is to use VirtualHosts based on host names or (simplest) on ports. So you can get http://siteA.mycoputer.localhost, http://siteB.mycomputer.localhost in the first case and http://192.168.x.y:8000, http://192.168.x.y:9000 in the second case
Here is Apache Server config example from Apache Server 2.2 documentation
# Ensure that Apache listens on port 80
Listen 80
# Listen for virtual host requests on all IP addresses
NameVirtualHost *:80
<VirtualHost *:80>
DocumentRoot /www/example1
ServerName www.example.com
# Other directives here
</VirtualHost>
<VirtualHost *:80>
DocumentRoot /www/example2
ServerName www.example.org
# Other directives here
</VirtualHost>

Apache port and url

I want to have several sites on my local machine, associated to specific port in my apache configuration. I don't even know if what I want is possible to do, or if I have the good way of thinking. May be I need other tools, so please, can you explain me ?
I have my dev machine with several site working on it, in my hosts file I put:
127.0.0.1 local.site1.com
127.0.0.1 local.site2.com
In appache configuration, I have these virtual hosts:
Listen 8081
Listen 8082
<VirtualHost *:8081>
ServerName local.site1.com
ServerAlias local.site1.com
DocumentRoot "C:/site1"
</VirtualHost>
<VirtualHost *:8082>
ServerName local.site2.com
ServerAlias local.site2.com
DocumentRoot "C:/site2"
</VirtualHost>
Do you know what should I do to have ?
http://local.site1.com that goes automatically and only to port 8081
http://local.site2.com that goes automatically and only to port 8082
Now, my configuration looks useless, because http://local.site1.com opens port 80, and http://local.site2.com:8081 goes to site1.
Why do you want them running on different ports? You don't need to do that to get what you want.
You can use NameVirtualHosts and run both sites on the same port, and the server will serve the correct site based on the domain name used in your browser.
http://httpd.apache.org/docs/2.2/vhosts/name-based.html
Example
NameVirtualHost *:80
<VirtualHost *:80>
ServerName local.site1.com
ServerAlias local.site1.com
DocumentRoot "C:/site1"
</VirtualHost>
<VirtualHost *:80>
ServerName local.site2.com
ServerAlias local.site2.com
DocumentRoot "C:/site2"
</VirtualHost>

NamedVirtualHost in apache configuration

I have a unix system whose actual name is "ech-10.45.25.12"
i have installed apache server in it.
Now i need to configure it in such a way that the two applications running in the same machine in tomcat in two different ports should be accessed by the same domain.
ie., i have two applications running in the same machine under different port
http://ech-10.45.25.12:8080/issuetracker/
http://ech-10.45.25.12:8180/dashboard/
I would like to name this server(ech-10.45.25.12) as devjunior.mycompany.com
The following is the configuration i have made in httpd.conf
Listen 80
Listen 8080
Listen 8180
NameVirtualHost ech-10.45.25.12:80
NameVirtualHost ech-10.45.25.12:8080
NameVirtualHost ech-10.45.25.12:8180
<VirtualHost ech-10.45.25.12:80>
ServerName devjunior.mycompany.com
DocumentRoot /www/domain-80
</VirtualHost>
<VirtualHost ech-10.45.25.12:8080>
ServerName devjunior.mycompany.com
DocumentRoot /www/domain-8080
</VirtualHost>
<VirtualHost ech-10.45.25.12:8180>
ServerName devjunior.mycompany.com
DocumentRoot /www/domain-8080
</VirtualHost>
i know i am doing a major mistake
But i should be able to access the applications by using the following urls
http://devjunior.mycompany.com/issuetracker
http://devjunior.mycompany.com/dashboard
Should i create ANY directories under any folders any where in the system
Please tell that also.
You configured only the names. So you've configured Apache to listen for:
http://devjunior.mycompany.com:8080
http://devjunior.mycompany.com:8180
You can:
Configure 2 domains with namevirtualhost without using ports. this is the most elegant way of doing what you want
Configure a single domain that points to a single directory on the filesystem with 2 links for the diferrent applications. This works with php mostly or pure html pages. With more complex applications you could incur in a lot of headache..
Domain and port. Like you've done. But you can access only by http://devjunior.mycompany.com:8080/issuetracker and http://devjunior.mycompany.com:8180/dashboard
Solution 1
You can use different domains or subdomains (which are cookie friendly in an eventuality of single sign on).
Listen 80
NameVirtualHost ech-10.45.25.12:80
<VirtualHost ech-10.45.25.12:80>
ServerName devjunior.mycompany.com
DocumentRoot /www/domain-80
</VirtualHost>
<VirtualHost ech-10.45.25.12:80>
ServerName dashboard.devjunior.mycompany.com
DocumentRoot /www/domain-8080
</VirtualHost>
<VirtualHost ech-10.45.25.12:80>
ServerName issuetracker.devjunior.mycompany.com
DocumentRoot /www/domain-8180
</VirtualHost>
Solution 2 is left as an excercise for the reader... :P
Here is what i did to make it work.
Though the change of name in etc/hosts file did nothing in my intranet, so i used the actual name of the machine which is ech-10.45.25.12
NameVirtualHost ech-10.45.25.12:80
<VirtualHost ech-10.45.25.12:80>
ServerName ech-10.45.25.12
ProxyPreserveHost on
ProxyPass /issuetracker http://ech-10.45.25.12:8080/issuetracker
ProxyPass /dashboard http://ech-10.45.25.12:8180/dashboard
</VirtualHost>
Also dont forget to add the "proxyName" & "proxyPort" attribute to the tag in tomcat's server.xml