Welcome to my website

Setup Apache webserver

Guide: Apache2 Webserver Setup with HTTPS and URL Rewriting



This guide will help you set up an Apache2 web server on your Linux server, including automatic HTTPS redirection and URL rewriting for "clean" addresses.

Requirements:
  • A Linux server (e.g., Ubuntu, Debian)
  • Root access or sudo privileges
  • A registered domain name (e.g., ewaldjurgens.nl) pointing to your server's IP address
  • Ports 80 (HTTP) and 443 (HTTPS) must be open in your firewall


Step 1: Install Apache2
Start by installing Apache2 on your server.


sudo apt update
sudo apt install apache2


After installation, Apache2 will start automatically. You can check its status:

sudo systemctl status apache2

You should see "active (running)".

Step 2: Configure Firewall (If Active)
Ensure your firewall (e.g., UFW) allows HTTP and HTTPS traffic.


sudo ufw allow 'Apache Full'
sudo ufw enable

(Or if you don't have 'Apache Full':
sudo ufw allow 'Apache'
and
sudo ufw allow 'Apache Secure'
)

Step 3: Test Basic Apache2 Functionality
Open your web browser and type in your server's IP address. You should see the default Apache2 Ubuntu landing page.

Step 4: Enable Modules (SSL and Rewrite)
We need two important modules:
  • mod_ssl: For HTTPS encryption.
  • mod_rewrite: For URL rewriting and redirects.


Enable them:

sudo a2enmod ssl
sudo a2enmod rewrite
sudo systemctl restart apache2


Step 5: Create a Webroot Directory
Create a directory for your website files. We'll use /var/www/your_domain as an example.


sudo mkdir -p /var/www/your_domain/html
sudo chown -R $USER:$USER /var/www/your_domain/html
sudo chmod -R 755 /var/www/your_domain


Create a simple index.html file for testing:

nano /var/www/your_domain/html/index.html

Add the following content:

<html>
<head>
<title>Welcome to My Website!</title>
</head>
<body>
<h1>Hello, this is my website!</h1>
<p>This is a test page for my Apache2 server.</p>
</body>
</html>

Save and exit (Ctrl+O, Enter, Ctrl+X).

Step 6: Virtual Host Configuration (HTTP)
Create a virtual host configuration file for your domain for HTTP traffic. This file will also handle the redirection to HTTPS.


sudo nano /etc/apache2/sites-available/your_domain.conf

Add the following content. Replace your_domain with your actual domain name (e.g., ewaldjurgens.nl).


<VirtualHost *:80>
ServerAdmin webmaster@your_domain
ServerName your_domain
ServerAlias www.your_domain
DocumentRoot /var/www/your_domain/html

# Redirect HTTP to HTTPS
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

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

  • ServerName and ServerAlias: Your domain name with and without 'www.'.
  • DocumentRoot: The directory where your website files are located.
  • RewriteEngine On, RewriteCond, RewriteRule: These are the rules that permanently (301) redirect all HTTP traffic (port 80) to the HTTPS version of your website.

Save and exit.

Step 7: Install Certbot for Let's Encrypt SSL
The easiest way to obtain and manage free SSL certificates is with Certbot.


sudo snap install core
sudo snap refresh core
sudo snap install --classic certbot
sudo ln -s /snap/bin/certbot /usr/bin/certbot


Step 8: Obtain SSL Certificate with Certbot
Certbot can automatically obtain an SSL certificate and adjust your Apache configuration.


sudo certbot --apache -d your_domain -d www.your_domain

  • Follow the prompts. Enter your email address and accept the terms of service.
  • Certbot will ask if you want to redirect HTTP traffic to HTTPS. Although we manually did this in Step 6, Certbot can also configure this for you. Choose the option that best fits (usually option 2 to redirect all traffic).


Upon successful execution, Certbot will create a new virtual host file (usually your_domain-le-ssl.conf) in /etc/apache2/sites-available/ which contains the HTTPS configuration.

Step 9: URL Rewriting (Clean URLs)
You asked to simplify URLs like your_domain.nl/books/indext.html to, for example, your_domain.nl/books/ or your_domain.nl/books.
Apache's DirectoryIndex automatically handles showing index.html when you access a directory (e.g., your_domain.nl/books/).
In case index.html is not specified and you don't have a trailing slash, we can use mod_rewrite.

Re-open your your_domain-le-ssl.conf file (or the configuration file for your HTTPS virtual host). This is typically found in /etc/apache2/sites-available/.


sudo nano /etc/apache2/sites-available/your_domain-le-ssl.conf


Locate the DocumentRoot and add the following rules inside the <VirtualHost *:443> section, preferably below the DocumentRoot or under the SSL-related settings. You might need to add or adjust an existing <Directory> block to enable AllowOverride All if you intend to use .htaccess files for rewrites later. For simplicity, we add the rules directly in the VirtualHost.


<VirtualHost *:443>
# ... existing HTTPS configuration ...
ServerName your_domain
# ... other settings like DocumentRoot, SSL certificates ...

# Ensure RewriteEngine is on
RewriteEngine On

# Rule 1: Automatically add trailing slash for directories (optional, but good for SEO)
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.+[^/])$ $1/ [R=301,L]

# Rule 2: If a file does not exist, try to serve index.html in that directory
# Example: your_domain.nl/books shows your_domain.nl/books/index.html
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/?$ $1/index.html [L]

# ... remaining ErrorLog, CustomLog etc. ...

<Directory /var/www/your_domain/html>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Require all granted
</Directory>
</VirtualHost>

  • RewriteEngine On: Ensures that rewriting is active for this Virtual Host.
  • Rule 1: If the URL points to an existing directory but lacks a trailing slash (e.g., your_domain.nl/books), this adds a slash and redirects the browser (301 redirect). This is good for SEO.
  • Rule 2: If the requested URL does not point to an existing file or directory (e.g., your_domain.nl/books without a slash and without books being a file), it tries to serve index.html within that directory. This is useful if you want to use your_domain.nl/books to show your_domain.nl/books/index.html.
  • <Directory> block: Is crucial. AllowOverride All is needed if you want to add more complex rewrites via .htaccess files in your webroot later. Require all granted ensures access.

Save and exit.

Step 10: Enable Virtual Host and Restart Apache2
Enable the newly configured virtual host (the HTTP version) and the HTTPS version created by Certbot.


sudo a2ensite your_domain.conf
sudo a2ensite your_domain-le-ssl.conf
sudo apache2ctl configtest

If the configuration test shows "Syntax OK", restart Apache2:

sudo systemctl restart apache2


Step 11: Test Your Website
1. Open your browser and go to http://your_domain (or http://www.your_domain). You should automatically be redirected to https://your_domain with a secure connection (green padlock).
2. Now test your simplified URLs. If you've created a directory /var/www/your_domain/html/books/ with an index.html file inside, try:
  • https://your_domain/books/ (should display index.html)
  • https://your_domain/books (should also display index.html, and possibly add a redirect with trailing slash if the first rewrite rule is active)


Conclusion
Your Apache2 server is now configured to serve HTTPS traffic, redirect HTTP to HTTPS, and rewrite URLs for a cleaner presentation. Good luck!

Back to Knowledge Base