#author("2018-01-28T18:40:22+09:00","","") #author("2018-01-28T18:42:29+09:00","","") #nofollow #norelated 総数:&counter(total); 今日:&counter(today); 昨日:&counter(yesterday); * もくじ [#q59cf97f] #contents &size(22){&color(blue){apache2.4.18にダイジェスト認証を設定する};}; *バージョンを確認 [#wcbb1188] ~$ apache2 -version Server version: Apache/2.4.18 (Ubuntu) Server built: 2016-07-14T12:32:26 -2.4.18 だった。 *認証が必要となるディレクトリを作成する。 [#w5be34aa] -例)auth_required_dir ディレクトリを作成する。 ~$ sudo mkdir -p /var/www/html/auth_required_dir/ *有効化された設定ファイルはどれかみる。 [#yddbe1cb] ~$ ls -la /etc/apache2/sites-enabled/ 合計 8 drwxr-xr-x 2 root root 4096 10月 10 2016 . drwxr-xr-x 9 root root 4096 1月 28 15:25 .. lrwxrwxrwx 1 root root 35 10月 10 2016 000-default.conf -> ../sites-available/000-default.conf -シンボリックリンクになっている。 *設定ファイル実体はどれかみる。 [#ya6db937] ~$ 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 -/etc/apache2/sites-available/000-default.conf が設定ファイルの実体だ。 *設定ファイルを編集する。 [#mbfbda63] ~$ 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 を新規作成するコマンドを実行する。 [#s931e3bf] -新規ではない場合は -c オプション無し。 ~$ sudo htdigest -c /etc/apache2/.htdigest realm_auth_required_dir username *mod_auth_digestモジュールを有効にする。 [#vd73e276] ~$ sudo a2enmod auth_digest *apache2を再起動する。 [#me1fe1aa] ~$ sudo service apache2 restart *wgetで確認 [#p110562d] **そのままやると失敗 [#de481f6f] ~$ 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する [#g917f3ae] ~$ wget --http-user=username --http-password=password http://hostname/auth_required_dir/file_to_be_downloaded.tar.gz ~$ 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 に接続しています... 接続しました。 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]