In our older guides we have shown to configure Nginx as IPv6 reverse proxy. Apache Webserver also can work as loadbalancer. Here is Ubuntu 18.04 Apache Reverse Proxy, Loadbalancer Configuration Step by Step Guide. For HSTS site, the configuration and steps are quite complex and probably Nginx is lighter and easy to consider. It is PHP-FPM which throws odd errors with Nginx, but Nginx is highly stable as reverse proxy and loadbalancer. Using Apache as reverse proxy and/or loadbalancer demands working knowledge on Apache webserver as well as knowledge on basic theoretical knowledge.
Apache Reverse Proxy, Loadbalancer Configuration Step by Step
We will take it granted that the backend servers you’ll loadbalance are HTTPS. Unlike Nginx, just to reverse proxy one backend server; you have to configure like complete loadbalancer else Apache on minimum error will serve local directories! Nginx basically become loadbalancer when we simply listen to IP or host outside local network. Apache is complete package, upon facing error it thinks that we are doing the wrong and serve local files (which actually for good intention). Apache however is quite powerful as loadbalancer. Most importantly, Apache is completely Libre Software and unlike Nginx has no paid part of more options! It is time taking to learn as first timer but worth on long run.
You need to add the IP (IPv4 or IPv6) of the server to DNS record of domain before the final step. In our earlier guide, we have described specific configuration of Apache IPv6 as webserver – it is suggested to finally configure server in that way.
---
SSH as root user. Update and upgrade :
1 2 | apt update -y apt upgrade -y |
We will install Apache2 from Ondřej Surý’s PPA :
1 2 3 4 5 6 7 8 | sudo apt install software-properties-common sudo add-apt-repository ppa:ondrej/apache2 # hit enter/return key to accept apt update -y apt upgrade -y sudo apt install apache2 ## apache2-bin has libapache2-mod-proxy-html, which is a dependency sudo apt install apache2-bin libxml2-dev |
You’ll get list of modules as output which will be automatically activated, like :
1 2 3 4 5 6 7 | Enabling module mpm_event. Enabling module authz_core. Enabling module authz_host. Enabling module authn_core. Enabling module auth_basic. Enabling module access_compat. ... |
We need to activate more modules and restart Apache :
1 2 3 4 5 6 7 8 9 10 11 12 | sudo a2enmod proxy sudo a2enmod proxy_http sudo a2enmod proxy_balancer sudo a2enmod lbmethod_byrequests sudo a2enmod proxy_ajp sudo a2enmod rewrite sudo a2enmod deflate sudo a2enmod headers sudo a2enmod proxy_connect sudo a2enmod proxy_html sudo a2enmod ssl sudo a2enmod http2 |
Then restart Apache2 :
1 | service apache2 restart |
Now copy the SSL certificates from main server, in our guides like to renewal GeoTrust or RapidSSL/Comodo cert, they are on the below path of original server :
1 2 3 | /usr/local/ssl/crt/public.crt /usr/local/ssl/private/private.key /usr/local/ssl/crt/intermediate.crt |
You can SSH from other terminal window, cat each file, copy their content and paste on this server. Alternatively you can copy via FTP. Now go to /etc/apache2/sites-available
and perform ls -al
:
1 2 | cd /etc/apache2/sites-available ls |
There will be two files – 000-default.conf
and default-ssl.conf
. Disable those configurations and restart Apache :
1 2 3 | sudo a2dissite 000-default.conf sudo a2dissite default-ssl.conf service apache2 restart |
Create two new configurations with memorable names, activate :
1 2 3 4 | cd /etc/apache2/sites-available/ rm 000-default.conf default-ssl.conf touch loadbalancer.conf loadbalancer-ssl.conf ls |
Basic configuration for proxy for port 80 is like this :
1 2 3 4 5 6 7 8 9 10 11 12 | <VirtualHost *:80> ServerAdmin webmaster@localhost DocumentRoot /var/www/ ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined ProxyPreserveHost On # Servers to proxy the connection, or # List of application servers Usage ProxyPass / http://server-ip-address:8080/ ProxyPassReverse / http://server-ip-address:8080/ ServerName localhost </VirtualHost> |
Basic configuration for proxy for port 443 is like this :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | <VirtualHost *:443> ServerAdmin webmaster@localhost DocumentRoot /var/www/ ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined SSLEngine On # Set the path to SSL certificate SSLCertificateFile /etc/ssl/private/public.crt SSLCertificateKeyFile /etc/ssl/private/private.key SSLCertificateChainFile /etc/ssl/private/intermediate.crt ProxyPreserveHost On ProxyPass /var/www/ http://server-ip-address:8080/ ProxyPassReverse /var/www/ http://server-ip-address:8080/ ServerName localhost </VirtualHost> |
Save them for now, run configuration test :
1 | sudo apache2ctl configtest |
Enable new virtual host file:
1 | sudo a2ensite loadbalancer.conf loadbalancer-ssl.conf |
Then restart Apache :
1 | service apache2 restart |
For loadbalancing, you need Proxy Balancer
on the top of the above basic configuration, and Proxy Pass
that cluster :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | <Proxy balancer://cluster1> # Define back-end servers: ## Server 1 BalancerMember http://0.0.0.0:8080/ ## Server 2 # BalancerMember http://0.0.0.0:8081/ </Proxy> <VirtualHost *:443> SSLEngine On # Set the path to SSL certificate SSLCertificateFile /etc/ssl/private/public.crt SSLCertificateKeyFile /etc/ssl/private/private.key SSLCertificateChainFile /etc/ssl/private/intermediate.crt ## ProxyPass / http://0.0.0.0:8080/ ## ProxyPassReverse / http://0.0.0.0:8080/ # Or balance the load: ProxyPass / balancer://cluster1 </VirtualHost> |
You can find official documents on Apache’s site :
1 | https://httpd.apache.org/docs/2.4/howto/reverse_proxy.html |
We have demonstrated the steps of basic setup. Apache is highly configurable. There are advanced guides on web to test other settings, such as :
1 | https://www.netnea.com/cms/apache-tutorial-9_setting-up-a-reverse-proxy/ |
Tagged With install mod_proxy ubuntu 18 04 , ubuntu 18 04 install mod_proxy , ubuntu 18 04 apache2 proxy module , ubuntu 18 04 apache reverse proxy installation , reverse proxy ubuntu 18 apache , reverse proxy ubuntu 18 04 , Reverse Proxy for Apache on One Ubuntu 18 04 , load balancer creating steps in ubuntu , install apache reverse proxy ubuntu 18 04 , ubuntu 18 04apache mod_proxy