Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions .vuepress/docs-sidebar.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,14 @@ module.exports = [
'installation/armbian',
'installation/docker',
'installation/synology',
['installation/security', 'Security'],
['installation/reverse-proxy', 'Reverse Proxy']
{ title: 'Security',
path: '/docs/installation/security',
children: [
'installation/reverse-proxy-nginx',
'installation/reverse-proxy-apache',
'installation/reverse-proxy-synology'
]
}
]
},
{
Expand Down
150 changes: 150 additions & 0 deletions installation/reverse-proxy-apache.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
---
layout: documentation
title: Apache Reverse Proxy
---

# Apache Reverse Proxy

These are the steps required to use [**Apache 2.4**](https://www.apache.org/), a HTTP server, although you can use [**NGINX**](reverse-proxy-nginx) server or any other HTTP server which supports reverse proxying. If you are familiar with Apache/basic reverse proxy config and are only interested in OpenHAB specific caveats/directives skip to [OpenHAB settings](#openhab-specific-settings)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
These are the steps required to use [**Apache 2.4**](https://www.apache.org/), a HTTP server, although you can use [**NGINX**](reverse-proxy-nginx) server or any other HTTP server which supports reverse proxying. If you are familiar with Apache/basic reverse proxy config and are only interested in OpenHAB specific caveats/directives skip to [OpenHAB settings](#openhab-specific-settings)
These are the steps required to use [**Apache 2.4**](https://www.apache.org/), a HTTP server, although you can use [**NGINX**](reverse-proxy-nginx) server or any other HTTP server which supports reverse proxying. If you are familiar with Apache/basic reverse proxy config and are only interested in openHAB specific caveats/directives skip to [openHAB settings](#openhab-specific-settings)


[[toc]]

## Installation

Apache runs as a service in most Linux distributions, installation should be as simple as:

```shell
sudo apt-get update && sudo apt-get install apache2
sudo service apache2 start
```

for Ubuntu/Debian based Linux, or

```shell
sudo yum install httpd
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dnf replaces yum, so maybe replace it here as well? https://www.linode.com/docs/guides/dnf-package-manager/

sudo systemctl enable httpd
sudo systemctl start httpd
```

for Fedora/CentOS/Red Hat Enterprise

Once installed, you can test to see if the service is running correctly by going to `http://mydomain_or_myip`, you should see the apache2 default page.

## Basic Configuration

::: warning Security Warning
It is not recommended to expose solely the configuration below to the internet.
:::

Apache runs based on configuration files.
The location of the default setup is typically `/etc/apache2/sites-available/000-default.conf`. To allow Apache to proxy openHAB, you need to edit or replace this file. Common practice is to use the domain name of the website it serves, appended with ".conf" - for example, "mydomain.conf"

The configuration below assumes that you run the reverse proxy on the same machine as your openHAB runtime.
If this doesn't fit for you, you just have to replace `localhost` with your openHAB instance hostname or ip. Remember to replace `mydomain` with your actual domain.

```xml
<VirtualHost *:80>
ServerName mydomain

<Location />
ProxyPass "http://localhost/"
ProxyPassReverse "http://localhost/"
</Location>

ErrorLog ${APACHE_LOG_DIR}/mydomain-error.log
CustomLog ${APACHE_LOG_DIR}/mydomain-access.log combined
</VirtualHost>
```

The command `apachectl configtest` can be used to verify any config you write. To enable your new website `a2ensite <config file name>`. Once enabled, you will not have to enable your website again. Configuration can be reloaded with `systemctl reload apache2`. Please make sure to reload and clear browser cookies after every change.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if it's a guideline in this repo, but I wouldn't use more than one sentence on one line. That makes reading diffs easier.


## Authentication

Authentication is recommended for additional security. There are many ways to authenticate to a proxy, but basic auth is sufficient **as long as it is over https**. Note the below documentation about [OpenHAB specific auth headers](#authentication-headers).
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Authentication is recommended for additional security. There are many ways to authenticate to a proxy, but basic auth is sufficient **as long as it is over https**. Note the below documentation about [OpenHAB specific auth headers](#authentication-headers).
Authentication is recommended for additional security. There are many ways to authenticate to a proxy, but basic auth is sufficient **as long as it is over https**. Note the below documentation about [openHAB specific auth headers](#authentication-headers).


### Adding or Removing users

To add users to your reverse proxy, you must use following command. Use the `-c` modifier only for your first user. It will create a new password file. **Do not** use the `-c` modifier again as this will remove all previously created users. Don't forget to change _username_ to something meaningful!

```shell
sudo htpasswd -c /etc/apache2/.htpasswd username
```

```shell
sudo htpasswd /etc/apache2/.htpasswd username
```

and to delete an existing user:

```shell
sudo htpasswd -D /etc/apache2/.htpasswd username
```

### Basic Auth

The below directives set the auth type to basic, set the auth name, point to our credentials file, and require a user to be authenticated before allowing them to access OpenHAB
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
The below directives set the auth type to basic, set the auth name, point to our credentials file, and require a user to be authenticated before allowing them to access OpenHAB
The below directives set the auth type to basic, set the auth name, point to our credentials file, and require a user to be authenticated before allowing them to access openHAB


```xml
<Location />
AuthType Basic
AuthName "Proxy Auth"
AuthUserFile /etc/apache2/.htpasswd
Require valid-user
</Location>
```

## Encryption/HTTPS

In the interest of not duplicating documentation, please see the [nginx encryption documentation](reverse-proxy-nginx#enabling-https) for certificate generation options. The certificate directives for Apache are:

```xml
SSLEngine on
SSLCertificateFile /path/to/cert
SSLCertificateKeyFile /path/to/key
```

For more details, read the [apache mod_ssl docs](https://httpd.apache.org/docs/2.4/mod/mod_ssl.html#sslcertificatefile)

## OpenHAB Specific Settings
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
## OpenHAB Specific Settings
## openHAB Specific Settings

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I stop commenting other wrong spellings as you get the idea.


These settings are required due to the quirks and features of proxies and/or OpenHAB.

### Authentication Headers

If you use authentication with your reverse proxy, the following two directives are necessary in OpenHAB 3.0 and later.

`Header set Set-Cookie "X-OPENHAB-AUTH-HEADER=1"`

This ensures that OpenHAB uses an alternative auth header, so it does not conflict with your proxy authentication credentials.

`RequestHeader unset Authorization`

This ensures that your proxy does not send your proxy authentication credentials to OpenHAB, interfering with login (and potentially sending them in plain text across a network).

## Putting it All Together

Below is a minimal configuration for a reverse proxy that listens for https requests only, uses basic auth, and proxies to 8080 (default OpenHAB http port).

```xml
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
```xml
```xml
<VirtualHost *:80>
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/.well-known/acme-challenge/
RewriteRule ^.*$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,QSA,L]
</VirtualHost>

That redirects http to https which is more user-friendly as some browser still use http first.

<VirtualHost *:443>
ServerName mydomain
SSLEngine on
SSLCertificateFile /path/to/cert
SSLCertificateKeyFile /path/to/key

Header set Set-Cookie "X-OPENHAB-AUTH-HEADER=1"
RequestHeader unset Authorization

<Location />
ProxyPass "http://localhost:8080/"
ProxyPassReverse "http://localhost:8080/"
AuthType Basic
AuthName "Proxy Auth"
AuthUserFile /etc/apache2/.htpasswd
Require valid-user
</Location>

ErrorLog ${APACHE_LOG_DIR}/mydomain-error.log
CustomLog ${APACHE_LOG_DIR}/mydomain-access.log combined
</VirtualHost>
```
Loading