trafficpixels Blog
Questions? Suggestions? message me
previous -- actual -- next

How to use apache2 for port redirects

What is Apache?

Apache is a webserver software.

install Apache

sudo apt update
sudo apt install apache2

activate proxy modules

sudo /usr/sbin/a2enmod proxy
sudo /usr/sbin/a2enmod proxy_http
sudo systemctl restart apache2

create configuration file for a virtual host

In this example our domain name is redirect.example.com. Replace this with your own domain.
sudo nano /etc/apache2/sites-available/redirect.example.com.conf
If you fill in the following text, then requests to port 80 will be proxied to port 8000 on the local machine and traffic to port 17777 will be proxied to port 17776 on the local machine.
<VirtualHost *:80>
  ProxyPreserveHost On
  ProxyRequests Off
  ServerName redirect.example.com
  ProxyPass /brouter http://localhost:17777/
  ProxyPassReverse /brouter http://localhost:17777/
  ProxyPass / http://localhost:8000/
  ProxyPassReverse / http://localhost:8000/
</VirtualHost>
Source of solution

listen on different ports

If Apache should listen on other ports than 80 or 443 if the ssl module is enabled, then these ports must be added to /etc/apache2/ports.conf. Rahul described how to do this and here is an example: sudo nano /etc/apache2/ports.conf
# If you just change the port or add more ports here, you will likely also
# have to change the VirtualHost statement in
# /etc/apache2/sites-enabled/000-default.conf

Listen 80
Listen 8080

<IfModule ssl_module>
	Listen 443
</IfModule>

<IfModule mod_gnutls.c>
	Listen 443
</IfModule>
Press Ctrl+X, then Y and then Enter to save.

activate configuration

The site has to be activated the first time. After every change to the configuration the webserver needs to be reloaded.
sudo /usr/sbin/a2ensite redirect.example.com
systemctl reload apache2

The webserver now serves a site at http://redirect.example.com:80/. If a URL at redirect.example.com:80/brouter is requested, it requests the content from localhost:17777 and if another URL is requested it delivers whatever it finds at http://localhost:8000/ followed by that URL.

deliver brouter service though apache

In order to serve requests to the brouter routing engine through Apache, for example if Apache should TLS instead of the browser connection directly with brouter without encryption, you have to tell the browser to connect through Apache. nano brouter-web/config.js comment out the line BR.conf.host =... and add a line under this line that says BR.conf.host = '/brouter/';. Press Ctrl+X, then Y and then Enter to save.

How to use Let's Encrypt with Apache

sudo apt install certbot python3-certbot-apache
/usr/sbin/apache2ctl configtest
systemctl reload apache2
sudo certbot --apache
Source