2018-07-17_ApacheでBasic認証を使ってみる

このページで分かること

  • apacheでbasic認証のかけ方

所用でwebサーバーにbasic認証を掛ける必要があったのでやり方をまとめました。

環境

  • Centos7
  • Apache2.4

ApacheのDocumentRootはデフォルトの/var/www/html/に設定しています。

対応の流れ

  1. apacheのインストール
  2. .htpasswdファイルの作成
  3. apacheの設定

1. apacheのインストール

Apacheがインストールされていない場合はインストールします。 Centosではパッケージ名はhttpdですので注意してください。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
##c rpmパッケージを検索(httpdがなければapacheは未インストール)
$c rpm -qa | grep httpd

##c インストール
$c sudo yum install -y httpd

##c インストールされたか確認
$c rpm -qa | grep httpd
httpd-tools-2.4.6-80.el7.centos.1.x86_64
httpd-2.4.6-80.el7.centos.1.x86_64

2. .htpasswdファイルの作成

.htpasswdファイルは認証時に入力するユーザー名とパスワード情報を記載するファイルです。 なおCentOSのユーザーとは関係ないので注意してください。

1
2
3
4
5
6
7
##c 「Adding password for user hoge」が表示されればOK
##c 凡例: htpasswd -b -m -c ${file_name} ${user_name} ${password}
$c sudo htpasswd -b -m -c /etc/httpd/conf/.htpasswd hoge hogepass

##c 内容の確認 
$c cat /etc/httpd/conf/.htpasswd
hoge:$apr1$mLUNDdu1$HrlMx.YlmHXNmsqnSSj4o0

3. apacheの設定

ポイントとしてはRequire all grantedは全てのアクセスを許可する設定なので書き換えます。 diff結果の157,161c156の内容(Basic認証関係)は必ず<Directory "/var/www/html">から</Directory>の範囲内に加えます。

分かりづらい場合は備考:編集後のhttpd.confの全内容を確認。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
##c バックアップをとる
$c sudo cp -p /etc/httpd/conf/httpd.conf  /etc/httpd/conf/httpd.conf.bak

##c 編集する
$c sudo vim /etc/httpd/conf/httpd.conf 

##c 編集前後で比較
$c diff /etc/httpd/conf/httpd.conf  /etc/httpd/conf/httpd.conf.bak
44d43
< ServerName testserver:80
157,161c156
<     AuthUserFile /etc/httpd/conf/.htpasswd
<     AuthGroupFile /dev/null
<     AuthName "input your id and passwd"
<     AuthType Basic
<     require valid-user
---
>     Require all granted

##c httpdの起動
$c sudo systemctl start httpd

ブラウザでページにアクセスした際にユーザー、パスワードの入力が求められれば設定完了です。

備考

編集後のhttpd.confの全内容

コメント行は省いてます。

編集行

  • サーバー名追加: 5行目
  • Basic認証追加: 33-37行目
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
ServerRoot "/etc/httpd"

Listen 80

ServerName testserver:80
Include conf.modules.d/*.conf

User apache
Group apache


ServerAdmin root@localhost


<Directory />
    AllowOverride none
    Require all denied
</Directory>


DocumentRoot "/var/www/html"

<Directory "/var/www">
    AllowOverride None
    Require all granted
</Directory>

<Directory "/var/www/html">
    Options Indexes FollowSymLinks

    AllowOverride None

    AuthUserFile /etc/httpd/conf/.htpasswd
    AuthGroupFile /dev/null
    AuthName "input your id and passwd"
    AuthType Basic
    require valid-user
</Directory>

<IfModule dir_module>
    DirectoryIndex index.html
</IfModule>

<Files ".ht*">
    Require all denied
</Files>

ErrorLog "logs/error_log"

LogLevel warn

<IfModule log_config_module>
    LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
    LogFormat "%h %l %u %t \"%r\" %>s %b" common

    <IfModule logio_module>
      LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %I %O" combinedio
    </IfModule>


    CustomLog "logs/access_log" combined
</IfModule>

<IfModule alias_module>


    ScriptAlias /cgi-bin/ "/var/www/cgi-bin/"

</IfModule>

<Directory "/var/www/cgi-bin">
    AllowOverride None
    Options None
    Require all granted
</Directory>

<IfModule mime_module>
    TypesConfig /etc/mime.types

    AddType application/x-compress .Z
    AddType application/x-gzip .gz .tgz



    AddType text/html .shtml
    AddOutputFilter INCLUDES .shtml
</IfModule>

AddDefaultCharset UTF-8

<IfModule mime_magic_module>
    MIMEMagicFile conf/magic
</IfModule>


EnableSendfile on

IncludeOptional conf.d/*.conf