Setting up Machine1:
In machine1 Nginx was installed first
Sudo apt install nginx
Followed by Apache2
sudo apt install apache2
Now, apache2 will fail to start since Nginx is already listening on port 80. To change the listening port edit /etc/apache2/ports.conf and change the line Listen 80 to Listen 85
You must also edit /etc/apache2/sites-enabled/000-default.conf and make the following changes
- <VirtualHost *: 80> should be changed as <VirtualHost *: 85>
- change the DocumentRoot from /var/www/html to /var/www/html_apache
Open folder /var/www/html and CUT and paste index.html to /var/www/html_apache
If you are unable to save changes to files and get a permission error, run your text editor as sudo. If you get errors while copy-pasting index files run the file manager as root. In Lubuntu you can open a terminal and type sudo pcmanfm
Also, create an index.html inside /var/www/html_apache/test1 and include some text so that you know the response is from apache.
Restart apache with the command
sudo service apache2 restartNow opening Localhost should point to Nginx welcome page and localhost:85 should show the apache welcome page
Setting up Machine2:
In machine2 you can simply install apache2, no special changes are required. Get the local IP address of machine2 using the command ifconfig (mine was 192.168.1.5). Also, create an index.html inside /var/www/html/test1 and include some text. Although in real scenarios we will be providing the same functionality, for this demo include a different text compared to the one in machine1 so that the request change can be noticed.Setting up the load balancer:
To set up the load balancer, in machine1 you should edit the file /etc/nginx/sites-available/default (another approach will be to create a new conf file and linking it to sites-enabled, but I will be editing the default file). Find the block that says server before it starts add the block
upstream sampleloadbalancer {
server 127.0.0.1:85 weight=1;
server 192.168.1.5 weight=2;
}location /test1/ {
proxy_pass http://sampleloadbalancer;
}By default, it follows the round-robin algorithm. However, you can specify a different algorithm as given here.

