In modern web architectures, the need to efficiently manage and secure access to backend services is paramount. Apache, a versatile and widely-used web server, not only serves web pages but also excels as a reverse proxy server. By leveraging Apache’s reverse proxy capabilities on an Ubuntu server, you can seamlessly route incoming client requests to backend services, enhancing security, performance, and flexibility.
This guide walks you through the steps to configure Apache as a reverse proxy on Ubuntu. Whether you’re looking to enhance the scalability of your applications, improve load balancing, or simplify the management of multiple services, setting up Apache as a reverse proxy is a powerful solution. Follow along to learn how to deploy and configure Apache to effectively proxy requests to backend servers, optimizing your web infrastructure for reliability and performance.
Prerequisites
Infrastructure Setup:
1 Ubuntu VM as Proxy Server:
- Public IP: Used for client communication.
- Private IP: Communicates with backend servers.
2 Ubuntu VMs as Backend Servers:
- Private IPs: Serve content to clients via the proxy server.
Software Requirements:
- Apache Web Server: Installed and configured on the proxy server.
- Basic Backend Services: Simple web pages (HTML files) on backend servers to simulate responses.
Once the servers is deployed, SSH into the command line interface. Before installing any packages, it’s essential to update your system’s package list to ensure you have access to the latest versions. Open a terminal and run the following command:
sudo apt-get update -y
Configure Proxy Server (Apache)
First, install Apache on your Ubuntu server, run the following commands:
sudo apt install apache2
Enable Proxy Modules: Apache needs specific modules enabled to function as a reverse proxy:
sudo a2enmod proxy
sudo a2enmod proxy_http
sudo a2enmod proxy_balancer
sudo a2enmod lbmethod_byrequests
Configure Virtual Host for Reverse Proxy
Edit Apache’s default virtual host configuration file:
sudo nano /etc/apache2/sites-available/000-default.conf
Replace the contents with the following configuration:
ServerName your-server-ip
ProxyPreserveHost On
# ProxyPass for backend server 1
ProxyPass / http://private-ip-backend1/
ProxyPassReverse / http://private-ip-backend1/
# ProxyPass for backend server 2
ProxyPass /backend2/ http://private-ip-backend2/
ProxyPassReverse /backend2/ http://private-ip-backend2/
Replace your-server-ip, private-ip-backend1, and private-ip-backend2 with your actual IP addresses.
Explanation:
- ProxyPreserveHost: Ensures Apache passes the original host header to backend servers.
- ProxyPass: Directs requests matching the specified path (/ or /backend2/) to the corresponding backend server.
- ProxyPassReverse: Modifies the response headers from the backend server to match the proxy address.
Save and close the file (Ctrl + X, then Y to confirm).
Apply the configuration changes by restarting Apache:
sudo systemctl restart apache2
Configure Backend Servers
Create Mock Backend Service for Server 1 by creating directory:
Ensure the directory for Backend Server 1 exists.
sudo mkdir -p /var/www/html/backend1
Create HTML File: Use a text editor to create the HTML file for Backend Server 1.
sudo nano /var/www/html/backend1/index.html
Insert the following content into the file:
<!DOCTYPE html>
<html>
<head>
<title>VPSie Backend Server 1</title>
</head>
<body>
<h1>This is Backend Server 1</h1>
</body>
</html>
Save and close the file.
Set Permissions: Ensure Apache has permissions to access the directory and files.
sudo chown -R www-data:www-data /var/www/html/backend1
sudo chmod -R 755 /var/www/html/backend1
Backend Server 2 Configuration:
Create Directory: Ensure the directory for Backend Server 2 exists.
sudo mkdir -p /var/www/html/backend2
Create HTML File: Use a text editor to create the HTML file for Backend Server 2.
sudo nano /var/www/html/backend2/index.html
Insert the following content into the file:
<!DOCTYPE html>
<html>
<head>
<title>VPSie Backend Server 2</title>
</head>
<body>
<h1>This is Backend Server 2</h1>
</body>
</html>
Save and close the file.
Set Permissions: Ensure Apache has permissions to access the directory and files.
sudo chown -R www-data:www-data /var/www/html/backend2
sudo chmod -R 755 /var/www/html/backend2
Verify Reverse Proxy
Open a web browser or use curl from the command line to access the backend servers through the proxy:
Access Backend Server 1: Navigate to your server’s public IP or domain name.
http://your-server-ip/
This should display “This is Backend Server 1”.
Access Backend Server 2: Append /backend2/ to your server’s IP or domain name.
http://your-server-ip/backend2/
This should display “This is Backend Server 2”.
By following these verification steps, you can ensure that your Apache reverse proxy configuration on Ubuntu is working as intended. Proper testing and monitoring ensure that client requests are correctly routed to the backend servers, enhancing the scalability and security of your web applications.