0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

レンタルサーバーに OTP 二段階認証アプリ「2FAuth」を入れる

0
Posted at

はじめに

OTP 二段階認証アプリをスマホに入れてしまうとこんなことが起こりがち。

  • 機種変更するときに移行を忘れてログインできなくなる
  • 1つのアカウントを複数人で使いまわしているからログインするときが大変
    • アカウントは人数分発行しましょう、なんだけどそうもいかないことがある

「2FAuth」は認証アプリを Web 上に構築するリスキーなアプリケーションです。
アクセス制限を厳密に行わないと非常に危険なので、分かる人だけ参考にしてください。

環境

今回はエックスサーバーに入れました。
しかし sodium 拡張モジュールがないため先に入れておく必要があります。

準備

# php コマンドのバージョンを 8.3.21 に固定
vi ~/.bashrc

# PHP version override 
export PATH="/opt/php-8.3.21/bin:$PATH"

# 反映
source ~/.bashrc

インストール

今回はサブドメインに入れるので、エックスサーバー上でサブドメインの設定を済ませておきます。
ドキュメントルートは
~/my.domain.com/public_html/sub.domain.com になります。

# sub.domain.com ディレクトリは 2FAuth のものと入れ替えるので一旦削除
cd ~/my.domain.com/public_html/
rm sub.domain.com

# 2FAuth を取得
curl -OL https://github.com/Bubka/2FAuth/archive/refs/tags/v5.6.1.tar.gz
tar xf v5.6.1.tar.gz

# 2FAuth のディレクトリを sub.domain.com のディレクトリ名にする
mv 2FAuth-5.6.1 sub.domain.com
cd sub.domain.com
composer install --prefer-dist --no-scripts --no-dev

エックスサーバー上で MySQL とメールアカウントを発行してから、環境設定ファイルを作ります。

# テンプレートからコピー
cp .env.example .env
vi .env
~/my.domain.com/public_html/sub.domain.com/.env
APP_TIMEZONE=Asia/Tokyo

SITE_OWNER=2fa@my.domain.com

APP_URL=https://sub.domain.com

DB_CONNECTION=mysql
DB_DATABASE=xsXXXXXX_2fa
DB_HOST=localhost
DB_PORT=3306
DB_USERNAME=xsXXXXXX_2fa
DB_PASSWORD=P@ssW0rd

MAIL_MAILER=smtp
MAIL_HOST=localhost
MAIL_PORT=465
MAIL_USERNAME="2fa@my.domain.com"
MAIL_PASSWORD=P@ssW0rd
MAIL_ENCRYPTION=ssl
MAIL_FROM_NAME="2FAuth"
MAIL_FROM_ADDRESS="2fa@my.domain.com"

APP_KEY は php artisan key:generate コマンドで生成しろと書いてあるので、従います。

# .env 中の APP_KEY を生成する
php artisan key:generate

# インストール
php artisan 2fauth:install

*******************************
*     2FAuth installation     *
*******************************

Start processing
  Clearing caches ............................................................................................. 4.37ms DONE
  Preparing .env file ......................................................................................... 0.01ms DONE
  Retrieving app key .......................................................................................... 0.00ms DONE

 Existing .env file found. Do you wish to review its vars? (yes/no) [yes]:
 > no
 
 [OK] Installation complete successfully

最後の手続き

Laravel 製のため、プログラムは public 以下に入っています。
DocRoot/public の状態なので、 sub.domain.com でアクセスできるよう修正します。

# .htaccess を編集
vi .htaccess
~/my.domain.com/public_html/sub.domain.com.env/.htaccess
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/public/
RewriteRule ^(.*)$ public/$1 [L]

とてもリスキーなアプリケーションなので気を付けて運用してください。
お疲れ様でした。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?