Skip to content

2FAuth

Prefer one command? Try the 2FAuth Playbook

2FAuth manages two-factor authentication accounts and generates one-time passwords. This guide uses PHP and SQLite, enables database encryption, and creates the administrator before publishing the site. The 2FAuth Playbook automates installation.

Website
2FAuth
License
MIT
Author
Uberspace
Last changed
2026-12-05

Tested with 2FAuth 9.1.0 on Uberspace 8.0.105. We can't guarantee it to work with newer versions.

Report a guide issue

Note

You should be familiar with PHP, the document root, and web backends. Use a fresh Asteroid with an empty document root. Example output below is abbreviated.

Prerequisites

2FAuth 9 requires PHP 8.4 or newer. Select PHP 8.4; this applies to the whole Asteroid:

[isabell@moondust ~]$ uberspace tool php version set 8.4
OK: The PHP version has been changed.
[isabell@moondust ~]$ php --version
PHP 8.4.[...] (cli) [...]

Installation

Resolve the latest stable release and clone it outside the document root:

[isabell@moondust ~]$ latest=$(curl -fsS --retry 3 https://api.github.com/repos/Bubka/2FAuth/releases/latest | jq -er '.tag_name')
[isabell@moondust ~]$ echo "$latest"
v9.1.0
[isabell@moondust ~]$ git clone --depth 1 --branch "${latest:?Resolve the latest release first}" https://github.com/Bubka/2FAuth.git /var/www/virtual/$USER/2fauth
Cloning into '/var/www/virtual/isabell/2fauth'...
[...]
[isabell@moondust ~]$ cd /var/www/virtual/$USER/2fauth
[isabell@moondust 2fauth]$ composer install --prefer-dist --no-dev --no-scripts --no-interaction --optimize-autoloader
[...]
Generating optimized autoload files

curl -fsS hides progress, reports errors, and fails on HTTP errors; --retry 3 retries temporary failures. jq -er extracts the tag as text and fails if it is missing. Stop on any error. The release includes the built frontend. Composer installs PHP dependencies from its lockfile without development packages.

Configuration

Database and environment

Create a private SQLite database:

[isabell@moondust 2fauth]$ umask 077
[isabell@moondust 2fauth]$ touch database/database.sqlite
[isabell@moondust 2fauth]$ cp .env.example .env
[isabell@moondust 2fauth]$ chmod 600 .env database/database.sqlite
[isabell@moondust 2fauth]$ stat -c '%a' .env database/database.sqlite
600
600

Edit .env, adjusting these existing entries and adding missing ones. Replace the domain, paths, and addresses with yours. Use an email address you read for SITE_OWNER:

APP_ENV=production
APP_DEBUG=false
APP_URL=https://isabell.uber.space
SITE_OWNER=you@example.com
DB_CONNECTION=sqlite
DB_DATABASE=/var/www/virtual/isabell/2fauth/database/database.sqlite
CACHE_DRIVER=file
SESSION_DRIVER=file
SESSION_SECURE_COOKIE=true
QUEUE_DRIVER=sync
MAIL_MAILER=sendmail
MAIL_SENDMAIL_PATH="/usr/bin/sendmail -t -i"
MAIL_FROM_ADDRESS=isabell@uber.space
MAIL_FROM_NAME=2FAuth
AUTHENTICATION_GUARD=web-guard
TRUSTED_PROXIES=*
WEBAUTHN_NAME=2FAuth
WEBAUTHN_ID=isabell.uber.space

The web backend terminates HTTPS. Trusting its forwarding headers lets Laravel recognize HTTPS requests. Keep the normal web-guard authentication; do not enable reverse-proxy authentication for this setup. Mail uses local sendmail.

Generate the encryption key and initialize the application. These commands are for a fresh installation:

[isabell@moondust 2fauth]$ php artisan key:generate --force
INFO  Application key set successfully.
[isabell@moondust 2fauth]$ php artisan package:discover
[...]
[isabell@moondust 2fauth]$ php artisan migrate --force
[...]
[isabell@moondust 2fauth]$ php artisan passport:keys --no-interaction
[...]
[isabell@moondust 2fauth]$ php artisan passport:client --personal --name 2FAuth --provider users --no-interaction
[...]
[isabell@moondust 2fauth]$ php artisan 2fauth:fix-passport-key-permissions
[...]
[isabell@moondust 2fauth]$ php artisan storage:link
[...]

Keep APP_KEY safe. Losing it after enabling encryption can make your stored OTP secrets unreadable. Never regenerate it during an update.

Create the administrator

Before publishing the site, open Laravel's console:

[isabell@moondust 2fauth]$ php artisan tinker
Psy Shell [...]
>

Paste the following PHP into that console, replacing the email address. It creates a random initial password and saves it in a private file outside the public directory:

$password = bin2hex(random_bytes(24));
$user = new App\Models\User();
$user->name = 'Administrator';
$user->email = 'you@example.com';
$user->password = $password;
$user->is_admin = true;
$user->email_verified_at = now();
$user->save();
App\Facades\Settings::set('disableRegistration', true);
App\Facades\Settings::set('useEncryption', true);
App\Facades\Settings::set('checkForUpdate', false);
file_put_contents(base_path('admin-password'), $password . PHP_EOL);
chmod(base_path('admin-password'), 0600);
exit

The console prints the results of expressions, including the generated password. Keep this session private. Read /var/www/virtual/$USER/2fauth/admin-password over SSH, save the password securely, and remove that file after your first login. Registration is disabled and OTP secrets are encrypted in SQLite.

Publish the site

Cache the configuration, then replace the empty standard document root with a link to the application's public directory. Do not expose the application root, .env, or database:

[isabell@moondust 2fauth]$ php artisan config:cache
INFO  Configuration cached successfully.
[isabell@moondust 2fauth]$ rm -f /var/www/virtual/$USER/html/nocontent.html
[isabell@moondust 2fauth]$ rmdir /var/www/virtual/$USER/html
[isabell@moondust 2fauth]$ ln -s "/var/www/virtual/$USER/2fauth/public" /var/www/virtual/$USER/html
[isabell@moondust 2fauth]$ uberspace web backend add "${USER}.uber.space/" apache --force --wait
[...]
OK: Your webserver configuration has been adapted

If rmdir fails, stop and inspect the directory instead of deleting its contents. Open https://isabell.uber.space/ and log in using your administrator email and generated password. Change the password in your account settings. Additional registrations can be managed in the administrator settings.

Add the following line using crontab -e for application maintenance:

* * * * * /usr/bin/php /var/www/virtual/isabell/2fauth/artisan schedule:run >> /home/isabell/logs/2fauth-scheduler.log 2>&1

Replace isabell in both paths. This runs Laravel's scheduler; it does not install application updates. Apache and PHP-FPM handle web requests, so no additional systemd application service is required.

Debugging

Check /var/www/virtual/$USER/2fauth/storage/logs/ and the PHP logs. Verify the database and cached configuration:

[isabell@moondust ~]$ cd /var/www/virtual/$USER/2fauth
[isabell@moondust 2fauth]$ php artisan 2fauth:check-db-connection
[...]
[isabell@moondust 2fauth]$ php artisan config:cache
INFO  Configuration cached successfully.

If assets or WebAuthn fail, check APP_URL, WEBAUTHN_ID, and HTTPS forwarding. After changing .env, rebuild the configuration cache. Use Forgot password? for password recovery; verify sendmail and the account's email address if delivery fails. A login password reset does not replace APP_KEY.

Updates

Note

Check the release notes regularly. Review PHP requirements and migration instructions before upgrading. Updates are manual.

Temporarily disable the scheduler entry, then enable maintenance mode and back up the complete installation, including .env, SQLite, OAuth keys, and uploaded files:

[isabell@moondust ~]$ cd /var/www/virtual/$USER/2fauth
[isabell@moondust 2fauth]$ php artisan down
INFO  Application is now in maintenance mode.
[isabell@moondust 2fauth]$ umask 077
[isabell@moondust 2fauth]$ tar -czf "$HOME/2fauth-backup-$(date +%Y%m%d-%H%M%S).tar.gz" -C "/var/www/virtual/$USER" 2fauth

Keep a private backup outside the Asteroid. Stop if the backup fails.

Resolve the latest tag again and update the code, preserving .env and data:

[isabell@moondust 2fauth]$ latest=$(curl -fsS --retry 3 https://api.github.com/repos/Bubka/2FAuth/releases/latest | jq -er '.tag_name')
[isabell@moondust 2fauth]$ git fetch --depth 1 origin tag "${latest:?Resolve the latest release first}"
[...]
[isabell@moondust 2fauth]$ git checkout --detach "$latest"
HEAD is now at [...]
[isabell@moondust 2fauth]$ composer install --prefer-dist --no-dev --no-scripts --no-interaction --optimize-autoloader
[...]
[isabell@moondust 2fauth]$ php artisan package:discover
[...]
[isabell@moondust 2fauth]$ php artisan migrate --force
[...]
[isabell@moondust 2fauth]$ php artisan config:cache
INFO  Configuration cached successfully.
[isabell@moondust 2fauth]$ php artisan view:clear
INFO  Compiled views cleared successfully.
[isabell@moondust 2fauth]$ php artisan up
INFO  Application is now live.

Reenable the scheduler and verify login and OTP generation. Do not rerun key:generate, reset the database, or recreate Passport clients during updates. A rollback must restore the matching code, database, keys, and configuration together while maintenance mode is active.

Further Reading