Authenticate users with apache2 configuration
Basic setup
To keep things clean and avoid surprises:
In your
<Directory /var/www/mfmkdocs-blog>
Options Indexes FollowSymLinks
AllowOverride None
AuthType Basic
AuthName "Restricted Documentation"
AuthUserFile /etc/apache2/YOUR_ACTUAL_HTPASSWD_NAME
Require valid-user
</Directory>Double-check with:
ls -l /etc/apache2/YOUR_ACTUAL_HTPASSWD_NAMEThen, activate the auth_basic authn_file:
sudo a2enmod auth_basic authn_file
sudo systemctl restart apache2Then, create the .htpasswd file:
sudo htpasswd -c /etc/apache2/.htpasswd-mfmkdocs testuser
sudo systemctl restart apache2!!! warning “Make sure the .htpasswd-xxxx file name is matched in the vhost conf file.”
If you ever change the password file name or move it, update that one line in the vhost and reload Apache.
With that aligned, Apache-side auth should be stable, and your client-side JS login/logout can just be a nicer UX layer on top. Well. Nice. Forgetting our name error, this was a 10 min fix compared to hours of fighting with NPM.
That matches the experience of a lot of people: Apache auth is boring but solid; NPM’s access lists are more powerful but also much easier to fight with.
You’re now in a good spot:
Apache is doing the real access control (works even with JS off, and before any MkDocs file is served).
Your MkDocs + JS login/logout UI is just a nice layer on top for users, without having to rely on it for security.
No need to touch the MkDocs venv or add extra moving parts.
If you later want to:
Restrict only certain subpaths (e.g. /admin or /internal), you can switch from <Directory> to <Location> blocks.
Or share the same .htpasswd across multiple vhosts.
I can sketch those configs if/when you need them. So, we need to create a .htpasswd for every user that are to access the site?(no problem). We can use these .htpasswd files for other vhost on the server? You mentioned something about a bit nicer approach that the basic apache approach.
You don’t need one .htpasswd per user; you can put many users in a single file, and you can also reuse that same file across multiple vhosts. Basic Auth is simple and robust, but there are indeed “nicer” options if you want a better UX later.
Users and .htpasswd files
One .htpasswd file can contain multiple users, one per line:
You add users with repeated htpasswd commands (without -c after the first time).You can have:
One shared .htpasswd for all your protected sites, or Separate files for different groups (e.g. /etc/apache2/docs.htpasswd, /etc/apache2/admins.htpasswd).
In each
AuthType Basic
AuthName "Docs"
AuthUserFile /etc/apache2/docs.htpasswd
Require valid-userFor another vhost you can reuse the same file:
AuthUserFile /etc/apache2/docs.htpasswdor use a different one if you want different users/groups.
2. “Nicer” than plain Basic Auth (but still Apache-side)
Basic Auth works but has a few downsides:
Browser-native dialog (not very pretty).
Password is sent with every request (over HTTPS this is fine, but still basic).
No concept of “logout” beyond closing the browser or clearing credentials.
Nicer Apache-level alternatives (without writing your own app) include:
Form-based auth with Apache (mod_auth_form)
You create a normal HTML login page (your own design).
Apache handles the auth logic, sets a session cookie, and protects the site behind it.
This gives you a real login form + redirect flow, more similar to an app, but still fully at the Apache layer.
It’s more involved to configure than Basic Auth, but you avoid writing a whole proxy app.
External identity provider (SSO / reverse proxy auth)
If your intranet has something like Keycloak, Authelia, OAuth2 proxy, or corporate SSO:
Apache (or NPM) can delegate authentication to that.
Users log in once with their company account.
Apache sees headers or environment variables indicating who they are and uses Require rules to allow/deny.
This is the smoothest for users (single sign-on, no new passwords), but needs existing infrastructure.
Given your setup and desire to keep it simple:
What you have now (Apache Basic Auth + your MkDocs JS logout button) is a very reasonable balance.
You can:
Put all allowed users into /etc/apache2/docs.htpasswd.
Reuse that file for any other docs vhosts you spin up.
Later, if you want a nicer login UX, you could explore mod_auth_form without changing MkDocs at all.
Rouge outline how to setup apache2 ‘mod_auto_form’ for a nicer login experience.
With mod_auth_form you can keep a custom HTML login page (your existing password.html or a slight variant), and let Apache handle the actual authentication and session instead of JavaScript.
Because I can’t look up your exact Apache version/config right now, I’ll outline the concept and what you’d change at a high level, so you can see if it’s worth pursuing.
- How mod_auth_form changes the flow
Today:
Browser → Apache → static MkDocs files.
Your JS on password.html:
Checks password in the browser, sets localStorage, redirects.
With mod_auth_form:
Browser → Apache.
If not authenticated:
Apache redirects to a login form URL (your password.html or similar).
User posts username/password to a special URL.
Apache validates the credentials (e.g. against an htpasswd file), sets a session cookie, and redirects back to the docs.
All checks happen on the server; no JS is required for protection.
Your password.html becomes just the UI that submits the form; the logic moves from JavaScript into Apache. 2. Reusing your password.html
You’d adapt password.html from a JS-driven page into a standard HTML form, roughly like:
xml
Key points:
action="/login_check" is a URL Apache will handle (not a MkDocs page).
name="httpd_username" and name="httpd_password" are the default field names mod_auth_form expects (configurable).
No JS is required to validate; the form POST goes directly to Apache.
Your existing styling from password.html (CSS, layout, etc.) can be kept almost as-is. 3. What changes in Apache (conceptually)
Instead of AuthType Basic + AuthUserFile in a
Enable mod_auth_form and a session module (mod_session, often with session_cookie).
Configure something like:
A protected area (your docs).
A login form URL (/password.html).
A login check URL (/login_check) where Apache reads the submitted credentials, checks them against your htpasswd, and sets a session cookie.
The result:
Unauthenticated user requests / → Apache redirects to /password.html?redirect=/.
User submits form → POST /login_check → Apache checks credentials, sets session cookie, and redirects back to /.
Subsequent requests include that session cookie, so Apache lets them through automatically.
From the user’s point of view, it feels a lot like a “normal” web app login but without you writing a separate app. 4. How this fits with your current JS-based UX
You have a few options:
Replace the JS login with Apache form auth completely
password.html becomes a pure server-side login form.
Your protect.js redirect and localStorage logic are no longer needed for security.
You might still keep the logout button, but instead of clearing localStorage, it would just href="/logout" (Apache can be configured to clear the session cookie).
Keep some UX bits from the JS setup
For example, you can still have a logout link/button in the header.
Instead of JS logic, it simply goes to an Apache-handled logout URL.
- Is it worth it vs. Basic Auth?
Pros compared to Basic Auth:
Custom, nice-looking login page (your password.html).
Easier to provide help text, branding, instructions.
You can implement a proper “logout” that clears the session cookie.
Cons:
Configuration is more involved than Basic Auth.
Slightly more moving parts (sessions, login handler URL).
Given you already have Apache Basic Auth working and a small user base, a realistic approach is:
Keep Basic Auth if you’re happy with the browser dialog and simplicity.
If you specifically want to reuse password.html and have a nicer login experience, then yes, mod_auth_form is the mechanism that lets you do that without building a separate Python/Node app.
If you like, you can paste:
The current vhost config you’re using now (with Basic Auth), and
The current password.html (minus any secrets),
and I can show you how that page would change structurally to be a clean mod_auth_form login form, plus the key Apache directives you’d need conceptually.