Secure your Web application
Why ?
Web services may contain applications, APIs, databases, or other functionality that is accessible over the network. These services must be secured because they can potentially process user accounts, authentication information, research or educational data, and other information that should not be exposed to unauthorised users.
All web applications that are accessible through a network must therefore use HTTPS with TLS encryption. This ensures that communication between the user's browser and the service is encrypted and helps protect against eavesdropping, manipulation of network traffic, and credential interception.
For this reason, an reverse proxy should be placed in front of the web services. The reverse proxy acts as the central entry point for incoming HTTPS connections and performs TLS/SSL termination (SSL offloading).
The architecture can be represented as:
HTTPS
┌────────────────┐
│ Webbrowser │
└───────┬────────┘
│
│ HTTPS
▼
┌────────────────┐
│ Reverse Proxy │
| |
│ TLS termination│
└───────┬────────┘
│
│ HTTPS or HTTP
▼
┌────────────────┐
│ Web Application│
└────────────────┘
The use of a reverse proxy does not mean that the backend application itself no longer needs to be secure. Applications must still be developed and configured according to secure coding practices, authentication and authorisation requirements, and applicable data-protection policies. The reverse proxy provides an additional security boundary rather than replacing application-level security.
In this guide, the web application is running locally on the same server as the Apache reverse proxy and is listening on port 8080. Because the connection between Apache and the application remains on the local host, HTTPS is not required for this backend connection. The connection can therefore use HTTP:
Client ── HTTPS ──> Apache ──
## HTTP ──> localhost:8080 ──> Web application
The important point is that the connection between the user's browser and Apache is protected by HTTPS. The HTTP connection between Apache and the application does not leave the server and therefore is not exposed to the external network.
If the backend application is moved to a different server, HTTP should only be used if the connection is otherwise appropriately protected. In that situation, HTTPS between the reverse proxy and the backend may be required to protect the traffic while it traverses the network.
Requesting a FQDN and TLS Certificate
Before securing your web application, you must request a Fully Qualified Domain Name (FQDN) for your application within the fse-cslab.nl domain.
For example:
webappx.fse-cslab.nl
You can request an FQDN by creating a ticket in Topdesk. (https://fse-cslab.maastrichtuniversity.nl/docs/category/support)
As part of this request, the infrastructure team will also create and configure the required TLS certificate for the Apache reverse proxy.
The FQDN and certificate will be used to provide secure HTTPS access to your web application.
Apache reverse Proxy
Installing Apache
Install packages:
sudo apt install apache2
Disable the default settings
sudo a2dissite 000-default
Configuring reverse Proxy
In this example, we will use webappx.fse-cslab.nl as the FQDN. Please replace webappx.fse-cslab.nl with the FQDN assigned to your own web application.
Also in this example your web application is running on localhost:8080
Create the config file /etc/apache2/sites-available/webappx.fse-cslab.nl.conf with the following config (use sudo):
<VirtualHost *:443>
ServerName webappx.fse-cslab.nl
SSLEngine on
SSLCertificateFile /etc/letsencrypt/live/webappx.fse-cslab.nl/cert.pem
SSLCertificateKeyFile /etc/letsencrypt/live/webappx.fse-cslab.nl/privkey.pem
ErrorLog /var/log/apache2/webappx_error.log
CustomLog /var/log/apache2/webappx_access.log combined
RequestHeader set "X-Forwarded-Proto" expr=%{REQUEST_SCHEME}
ProxyPass / http://127.0.0.1:8080/
ProxyPassReverse / http://127.0.0.1:8080/
</VirtualHost>
Enable the configuration and some modules
sudo a2ensite webappx.fse-cslab.nl
sudo a2enmod rewrite proxy proxy_http ssl headers
Restart the apache services
sudo systemctl restart apache2
Check the status of the apache service
sudo systemctl status apache2
Troubleshooting
- Check whether Linux is listening for incoming connections on the ports used by Apache and your web application. In this example, Apache is listening on port 443, while the web application is running on port 8080.
sudo ss -ntulp
Netid State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
tcp LISTEN 0 4096 127.0.0.1:8080 0.0.0.0:* users:(("rootlessport",pid=3717460,fd=10))
tcp LISTEN 0 511 *:443 *:* users:(("apache2",pid=941474,fd=4),("apache2",pid=71972,fd=4),("apache2",pid=71971,fd=4))
...
- Check the status of your Apache services
sudo systemctl status apache2
- Check the logging of Apache
sudo less /var/log/apache2/webappx_access.log
sudo less /var/log/apache2/webappx_error.log
or using journalctl
sudo journalctl -u apache2
Creating self-signed certificates
Introduction
A self-signed certificate is a certificate that is signed using its own private key instead of being signed by a trusted Certificate Authority.
Self-signed certificates are useful for:
- Development and testing environments.
- Student projects and experiments.
- Internal services that are not publicly accessible.
Important: A self-signed certificate does not automatically have the trust of your operating system or web browser. When visiting a website using a self-signed certificate, your browser will normally display a security warning.
For publicly accessible websites, we recommend using a certificate issued by a trusted Certificate Authority.
Creating the Certificate
First, install the OpenSSL package if it isn't already installed.
sudo apt install openssl
Generate the private key and certificate We will use the following hostname as an example: someservice.fse-cslab.nl
openssl req -x509 -newkey rsa:3072 -sha256 -days 365 \
-noenc \
-keyout someservice.key \
-out someservice.crt \
Let's explain the command:
| Option | Description |
|---|---|
req | Creates a certificate or certificate request. |
-x509 | Generates a self-signed certificate. |
-newkey rsa:3072 | Generates a new RSA private key with 3072 bits. |
-sha256 | Uses SHA-256 for the certificate signature. |
-days 365 | Makes the certificate valid for 365 days. |
-noenc | Does not encrypt the private key with a passphrase. |
-keyout | Specifies the output filename for the private key. |
-out | Specifies the output filename for the certificate. |
The following output will be generated:
-----
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [AU]:NL
State or Province Name (full name) [Some-State]:
Locality Name (eg, city) []:
Organization Name (eg, company) [Internet Widgits Pty Ltd]:FSE
Organizational Unit Name (eg, section) []:DACS
Common Name (e.g. server FQDN or YOUR name) []:someservice.fse-cslab.nl
Email Address []:
It's very important that the Common Name matches the hostname: someservice.fse-cslab.nl
The command generates two files:
- someservice.key: The private key.
- someservice.crt: The self-signed certificate
Important: The private key must remain secret. Never share it, publish it, or commit it to a Git repository. Only the certificate (.crt) should be distributed when necessary. Also check the permissions and ownership of the file. The private key must only be readable by root (and by the web server if you deliberately configure a different access mechanism).
-rw-r--r-- 1 root root 1679 Dec 2 11:30 someservice.crt
-rw------- 1 root root 2484 Dec 2 11:30 someservice.key
You can inspect the contents of your certificate using:
openssl x509 -in someservice.crt -text -noout
Next step is to move the files to the recommended directories:
sudo mv someservice.crt /etc/ssl/certs
sudo mv someservice.key /etc/ssl/private