Apache2 : Basic認証を利用する2018/06/08 |
|
Basic認証を利用して、ある特定のページに対してユーザー認証が必要なようにアクセス制限を設定します。
|
|
| [1] | 例として [/var/www/html/auth-basic] ディレクトリ配下を認証対象として設定します。 |
|
root@www:~#
apt -y install apache2-utils
root@www:~#
vi /etc/apache2/sites-available/auth-basic.conf # 新規作成
<Directory /var/www/html/auth-basic>
AuthType Basic
AuthName "Basic Authentication"
AuthUserFile /etc/apache2/.htpasswd
require valid-user
</Directory>
# ユーザーを登録 (-c でファイル新規作成 : -c は初回のみ付与) root@www:~# htpasswd -c /etc/apache2/.htpasswd ubuntu New password: # パスワード設定 Re-type new password: Adding password for user ubuntu mkdir /var/www/html/auth-basic root@www:~# a2ensite auth-basic Enabling site auth-basic. To activate the new configuration, you need to run: service apache2 reload
root@www:~#
systemctl restart apache2
# テストページ作成
root@www:~#
vi /var/www/html/auth-basic/index.html <html> <body> <div style="width: 100%; font-size: 40px; font-weight: bold; text-align: center;"> Test Page for Basic Auth </div> </body> </html> |
| [2] | クライアントPC で Web ブラウザを起動し、作成したテストページにアクセスします。 すると設定通り認証を求められるので、[1] で登録したユーザーで認証します。 |
|
| [3] | アクセスできました。 |
|
| Sponsored Link |
|
|