#author("2018-10-10T19:38:38+09:00","","")
#author("2018-10-10T19:40:15+09:00","","")
#nofollow
#norelated
総数:&counter(total); 今日:&counter(today); 昨日:&counter(yesterday);

* もくじ [#ieb1d42f]

#contents

&size(22){&color(blue){apache2.4.18にダイジェスト認証を設定する};};

*バージョンを確認 [#bd80d673]
 ~$ apache2 -version
 Server version: Apache/2.4.29 (Ubuntu)
 Server built:   2018-10-03T14:41:08
-2.4.29 だった。

*認証が必要となるディレクトリを作成する。 [#ld9a7201]
-例)auth_required_dir ディレクトリを作成する。
 ~$ sudo mkdir -p /var/www/html/auth_required_dir/

*有効化された設定ファイルはどれかみる。 [#fe9c0d62]
 ~$ ls -la /etc/apache2/sites-enabled/
 合計 8
 drwxr-xr-x 2 root root 4096 10月  8 21:57 .
 drwxr-xr-x 9 root root 4096 10月  8 22:02 ..
 lrwxrwxrwx 1 root root   35 10月  8 21:57 000-default.conf -> ../sites-available/000-default.conf

-シンボリックリンクになっている。

*設定ファイル実体はどれかみる。 [#r337d3ca]
 ~$ ls -la /etc/apache2/sites-available/
 合計 20
 drwxr-xr-x 2 root root 4096  1月 28 15:43 .
 drwxr-xr-x 9 root root 4096  1月 28 15:25 ..
 -rw-r--r-- 1 root root 1599  1月 28 15:43 000-default.conf
 -rw-r--r-- 1 root root 6338  4月  6  2016 default-ssl.conf
 drwxr-xr-x 2 root root 4096 10月  8 21:57 .
 drwxr-xr-x 9 root root 4096 10月  8 22:02 ..
 -rw-r--r-- 1 root root 1332  6月 28 02:05 000-default.conf
 -rw-r--r-- 1 root root 6338  6月 28 02:05 default-ssl.conf

-/etc/apache2/sites-available/000-default.conf が設定ファイルの実体だ。

*設定ファイルを編集する。 [#m08650d2]
 ~$ sudo gvim -f /etc/apache2/sites-available/000-default.conf
-例)username という名前のユーザの例

 ~$ cat /etc/apache2/sites-available/000-default.conf 
 <VirtualHost *:80>
 	# The ServerName directive sets the request scheme, hostname and port that
 	# the server uses to identify itself. This is used when creating
 	# redirection URLs. In the context of virtual hosts, the ServerName
 	# specifies what hostname must appear in the request's Host: header to
 	# match this virtual host. For the default virtual host (this file) this
 	# value is not decisive as it is used as a last resort host regardless.
 	# However, you must set it for any further virtual host explicitly.
 	#ServerName www.example.com
 
 	ServerAdmin webmaster@localhost
 	DocumentRoot /var/www/html
 	#ここから追記開始
 	<Directory "/var/www/html/auth_required_dir">
 	    AuthType Digest
 	    AuthName "realm_auth_required_dir"
 	    AuthDigestDomain /auth_required_dir/
 	    AuthUserFile /etc/apache2/.htdigest
 	    Require user username
 	</Directory>
 	#ここまで追記おわり
 	# Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
 	# error, crit, alert, emerg.
 	# It is also possible to configure the loglevel for particular
 	# modules, e.g.
 	#LogLevel info ssl:warn
 
 	ErrorLog ${APACHE_LOG_DIR}/error.log
 	CustomLog ${APACHE_LOG_DIR}/access.log combined
 
 	# For most configuration files from conf-available/, which are
 	# enabled or disabled at a global level, it is possible to
 	# include a line for only one particular virtual host. For example the
 	# following line enables the CGI configuration for this host only
 	# after it has been globally disabled with "a2disconf".
 	#Include conf-available/serve-cgi-bin.conf
 </VirtualHost>
 
 # vim: syntax=apache ts=4 sw=4 sts=4 sr noet

*パスワードファイル .htdigest を新規作成するコマンドを実行する。 [#pff08a9b]
-新規ではない場合は -c オプション無し。
 ~$ sudo htdigest -c /etc/apache2/.htdigest realm_auth_required_dir username

*mod_auth_digestモジュールを有効にする。 [#gf6e4a02]
 ~$ sudo a2enmod auth_digest

*apache2を再起動する。 [#a33eed06]
 ~$ sudo service apache2 restart

*wgetで確認 [#o146be67]
**そのままやると失敗 [#t4a368a8]
 ~$ wget http://hostname/auth_required_dir/file_to_be_downloaded
 --2018-01-28 18:30:50--  http://hostname/auth_required_dir/file_to_be_downloaded
 hostname:80 に接続しています... 接続しました。
 HTTP による接続要求を送信しました、応答を待っています... 401 Unauthorized
 
 Username/Password による認証に失敗しました。
 

**成功 ユーザー名とパスワードを指定してwgetする [#g70d969a]
 ~$ wget --http-user=username --http-password=password http://hostname/auth_required_dir/file_to_be_downloaded.tar.gz
 --2018-01-28 18:30:13--  http://hostname/auth_required_dir/file_to_be_downloaded.tar.gz
 hostname:80 に接続しています... 接続しました。
 HTTP による接続要求を送信しました、応答を待っています... 401 Unauthorized
 選択された認証形式: Digest realm="realm_auth_required_dir", nonce="省略", algorithm=MD5, domain="/auth_required_dir/", qop="auth"
 hostname:80 への接続を再利用します。
 HTTP による接続要求を送信しました、応答を待っています... 200 OK
 長さ: 34845682 (33M) [application/x-gzip]
 `file_to_be_downloaded.tar.gz' に保存中
 
 file_to_be_downloaded.tar.gz 100%[==========================================>]  33.23M  --.-KB/s    in 0.07s   
 
 2018-01-28 18:30:13 (494 MB/s) - `file_to_be_downloaded.tar.gz' へ保存完了 [34845682/34845682]

トップ   差分 バックアップ リロード   一覧 単語検索 最終更新   ヘルプ   最終更新のRSS