LoginSignup
7
9

More than 5 years have passed since last update.

いまさらながらcentOS7.2でベーシック認証を設定する

Last updated at Posted at 2016-11-20

環境

プラットフォーム
さくらのクラウド
CPU:2core Memory:2GB
ストレージ:SSD20GB

OS

/etc/redhat-release
CentOS Linux release 7.2.1511 (Core) 
version 7.0.6

Apache

# httpd -v
Server version: Apache/2.4.6 (CentOS)

準備

Apacheをインストール

# yum install -y httpd

Apacheを起動

# systemctl start httpd.service
# systemctl enable httpd.service

ベーシック認証を設定するディレクティブ Authtype が使用できるコンテキストはディレクトリ(Directory)外部設定ファイルでしたので、その2パターンで設定してみたいと思います。

ドキュメントルート

/etc/httpd/conf.d/httpd.conf
DocumentRoot "/var/www/html"

外部設定ファイルでのディレクティブ許可設定

デフォルトだと/var/www/html/ディレクトリには3箇所、AllowOverride noneの設定でしてありました。ディレクティブタイプをピンポイントで指定できますが、面倒なのでall(すべてのディレクティブを許可)に設定してます。

103行目

/etc/httpd/conf.d/httpd.conf
<Directory />
    AllowOverride all                                                                                                                 
    Require all denied
</Directory>

125行目

/etc/httpd/conf.d/httpd.conf
<Directory "/var/www">
   AllowOverride all
   # Allow open access:
   Require all granted
</Directory>

151行目

/etc/httpd/conf.d/httpd.conf
 AllowOverride all

外部設定ファイル(.htaccess)での設定

認証に使うユーザ情報のファイルを作成

/var/www/html/
# htpasswd -c /var/www/html/.htpasswd basictestuser
New password: 
Re-type new password: 
Adding password for user basictestuser

外部設定ファイルを作成。
外部設定ファイルが置いてあるカレントディレクトリ以下がベーシック認証の対象となります。外部設定ファイル名はAccessFileNameディレクティブで設定できます。何も設定していない場合は、デフォルトで.htaccessになります。

/var/www/html/
# vi .htaccess
/var/www/html/.htaccess
AuthType basic
AuthName "Auth"
AuthUserFile /var/www/html/.htpasswd
Require valid-user

各ディレクティブの役割

ディレクティブ 役割
AuthType 認証の種類。ベーシック認証はBasic、ダイジェスト認証はDigest。
AuthName 認証の領域の名前(なんでもOK)
AuthUserFile 認証に使うユーザ情報のファイル
Require ベーシック認証された中で、どのユーザがアクセスできるか指定

HTTPリクエストでテストします。

# curl http://59.106.222.210/
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>401 Unauthorized</title>
</head><body>
<h1>Unauthorized</h1>
<p>This server could not verify that you
are authorized to access the document
requested.  Either you supplied the wrong
credentials (e.g., bad password), or your
browser doesn't understand how to supply
the credentials required.</p>
</body></html>

IDとパスワードを指定していないので、401Unauthorizedが返ってきてます。
ブラウザでアクセスして確認してみてください。

ディレクトリ(Directory)での設定

認証に使うユーザ情報のファイルを作成

/etc/httpd/conf.d/
# htpasswd -c /etc/httpd/conf/htpasswd basictestuser

設定ファイルを作成

/etc/httpd/conf.d/
# vi basic_auth.conf

Directoryでベーシック認証をかけたいディレクトリを指定します。
指定したディレクトリ以下がベーシック認証の対象となります。

/etc/httpd/conf.d/basic_auth.conf
<Directory /var/www/html/>
    AuthType basic
    AuthName "Auth"
    AuthUserFile /etc/httpd/conf/.htpasswd
    Require valid-user
</Directory>

設定ファイルをhttpdに読み込むため再起動

/etc/httpd/conf.d/
# systemctl restart httpd.service

HTTPリクエストでテスト。

# curl http://59.106.222.210/
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>401 Unauthorized</title>
</head><body>
<h1>Unauthorized</h1>
<p>This server could not verify that you
are authorized to access the document
requested.  Either you supplied the wrong
credentials (e.g., bad password), or your
browser doesn't understand how to supply
the credentials required.</p>
</body></html>

.htaccessの場合と同じく401Unauthorizedが返ってきてます。

IPアドレスでのアクセス制限

特定のIPアドレスからのアクセスを許可、拒否することができます。
先ほどの設定ファイルに追記します。(.htaccessに記述しても同様の効果がでます)

/etc/httpd/conf.d/basic_auth.conf
<Directory /var/www/html/>
    AuthType basic
    AuthName "Auth"
    AuthUserFile /etc/httpd/conf/.htpasswd
    Require valid-user
    Allow from 192.168.0.1
    Deny from all
    Order Deny,Allow
    Satisfy all
</Directory>

各ディレクティブの役割

ディレクティブ 役割
Allow アクセスを許可するホスト(IP)
Deny アクセスを拒否するホスト(IP)
Order AllowとDenyの評価される順番
Satisfy AuthTypeとAllow,Denyが指定されている時のアクセスポリシー

AllowDenyで許可、拒否の設定をします。

OrderディレクティブではAllowDenyの評価される順番を指定できます。

例)
Order Deny,Allow
DenyされないIPとAllowされたIPを許可。それ以外は拒否。

Order Allow,Deny
AllowされないIPとDenyされたIPを拒否。それ以外は許可。

Satisfyディレクティブではベーシック認証IPアドレスでのアクセス制限が設定されている場合のアクセス設定を指定します。

例)
Satisfy Any
AllowRequireのどちらかを満たせば許可。

Satisfy all
AllowRequireの両方を満たせば許可。

設定ファイルをhttpdに読み込むため再起動

/etc/httpd/conf.d/
# systemctl restart httpd.service
7
9
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
7
9