Transcript Document
第3回 Webサーバー設定 知的システム構築工房 Linux設定セミナーシリーズ 第3回 Webサーバー設定 2011年3月14日(月) 株式会社 アセンディア -1 - 第3回 Webサーバー設定 目的 当セミナーを通して、 Webサーバの設定方法を理解し、Webサービス の構築を体得する -2 - 第3回 Webサーバー設定 目標 当セミナーを受講することにより 1.Webサーバ構築で必要な知識を習得し、 Webサービスを設定・構築することができるように なること 2.セキュリティに考慮したWebサービスを構築 できるようになること -3 - 第3回 Webサーバー設定 セミナー内容 1.Webサービスのインストール設定 Apache HTTP Serverインストール ドキュメントルートについて Apacheの設定(応用編) 2.アクセスログの設定方法 Apacheのアクセスログ管理 アクセスログを解析する Webalizerのインストール 3.セキュアなWebサービスの構築方法 セキュアなWebサービスの構築 OpenSSL設定までの流れ -4 - 第3回 Webサーバー設定 1.Webサービスのインストール設定 -5 - 第3回 Webサーバー設定 Webサーバー HTMLファイルや画像などの情報を蓄積し要求に応じて提供するサーバ HTTP(HyperText Transfer Protocol)を使用する 主なWebサーバーソフト Apache HTTP Server 世界中で約半数のサーバーで利用されているソフトウェア 大規模な商用サイトから、個人で運用するサイトまで、幅広く利用される。 Windows ,MacOS,Linux,Unix等、さまざまなOSで利用可能 IIS(Internet Information Server) Microsoft社が提供するソフトウェア Webサービス以外に、FTP,SMTP等の機能を有する。 Windows OSのみインストール、運用可能 Oracle HTTP Server Oracle社が提供するソフトウェア Apache HTTP Serverの派生商品。 Oracleアプリケーションと連携する -6 - 第3回 Webサーバー設定 Apache HTTP Serverインストール インストールの流れ 1.ソフトウェアの取得 2.ダウンロードしたソフトウェアの解凍 3.プログラムソースコードのコンパイル 4.ソースコードのインストール 5.httpd.conf設定 6.動作確認 -7 - 第3回 Webサーバー設定 1.ソフトウェアの取得 Webサイトより取得します。 http://httpd.apache.org/ -8 - 第3回 Webサーバー設定 タイトル カーネル : : -9 - 第3回 Webサーバー設定 個人フォルダ/ダウンロードフォルダ配下に コピーされます -10 - 第3回 Webサーバー設定 2.ダウンロードしたソフトウェアの解凍 ① ② ③ ④ ⑤ ⑥ ⑦ ①pwd 自分がいるディレクトリを確認 ②ls ファイル確認 ③ダウンロードディレクトリへ移動 ④Webサイトよりダウンロードしたプログラムファイル(httpd-2.2.17.tar.gz)が存在することを確認 ⑤gzip gz圧縮したファイルを解凍する ⑥解凍されていることを確認 ⑦tar アーカイブされたファイルをディレクトリ構成に戻す -11 - 第3回 Webサーバー設定 ① ② ③ ①tarコマンド発行により、復元状況が表示される ②復元されたディレクトリが表示される(httpd-2.2.17) ③httpd-2.2.17ディレクトリへ移動し、ファイルが解凍されていることを確認する -12 - 第3回 Webサーバー設定 3.プログラムソースコードのコンパイル コンパイルの流れ 1.rootユーザに変わります 2.プログラムソースを/usr/local/srcへ移動します 3.configure コマンド発行 コンパイラ、ライブラリのコンパイル環境チェック ⇒チェックOKの場合、MakeFileが作成されます 4.make コマンド発行 Makefile(コンパイル手順設定ファイル)に従って、コンパイルされます -13 - 第3回 Webサーバー設定 1.su コマンドの発行 su - [rootユーザのパスワード] 2.プログラムソースの移動 mv /home/[ユーザ名]/ダウンロード/httpd-2.2.17 /usr/local/src 3.configure コマンドの発行 .configure --prefix=/usr/local/apache2 --enable-ssl オプションの追加 prefix インストールされる場所 enable-ssl SSL機能を追加する ※/usr/local/src/httpd-2.2.17へ移動してコマンド発行 4.make コマンドの発行 make -14 - 第3回 Webサーバー設定 4.ソースコードのインストール 1.make install コマンドの発行 make install インストールが完了すると、/usr/local/配下にapache2フォルダが作成されます。 apache2フォルダの中を確認します -15 - 第3回 Webサーバー設定 5.httpd.conf 設定 設定ファイルは、/usr/local/apache2/conf/配下にあります。 この設定ファイルを編集することで、Apache Webサービスの動作を変更します。 まずは、httpd.confファイルをのぞいてみましょう! 1.管理者の設定 ServerAdmin [email protected] 2.サーバー名の設定 ServerName XXXX:80 3.トップページとして表示されるファイルの設定 DirectoryIndex topPage.html ※既存のファイルをリネームします。 rootユーザにて作業します -16 - 第3回 Webサーバー設定 ドキュメントルートについて ドキュメントルート = URLによるアクセスで起点となるディレクトリ WWWサーバ /data/web/oita-u/eng/ http://www2.cc.oita-u.ac.jp/eng/index.html -17 - DocumentRoot ドキュメントルート index.html 第3回 Webサーバー設定 6.動作確認 1.Apacheソフトウェアの起動 Apacheの実行ファイルは /usr/local/apache2/binフォルダ配下にあります。 apachectl 実行ファイルにて操作を行う オプション: start 起動 stop 停止 restart 再起動 -18 - 第3回 Webサーバー設定 2.ブラウザで起動確認 ブラウザにて、下記のアドレスを入力します。 http://localhost/ It works!と表示されていればOKです。 このように表示した場合はhttpd.confを のDirectoryIndexを確認! -19 - 第3回 Webサーバー設定 Apacheの設定(応用編) 1.ネットワークアクセス制限の設定 httpd.confファイルで設定します 1. /usr/local/apache2/conf/へ移動 2. httpd.confファイルを修正 <Directory "/usr/local/apache2/htdocs/accessLimit"> Allow from 192.168.0.0/255.255.255.0 Deny from all Order Deny,Allow </Directory> -20 - 第3回 Webサーバー設定 2.ユーザ名とパスワードで認証を行う ユーザ認証が必要なWebサイトに設定を行います。 1. rootユーザに変わり、/usr/local/apache2/confディレクトリに移動します 2. パスワードファイルを作成します ※ユーザ名は、Linuxのユーザとは別に管理されます htpasswd コマンド htpasswd -c ファイル名 ユーザ名 -c:新規のみ 3.http.confファイルを修正します <Directory "/usr/local/apache2/htdocs/accessLimit"> Allow from 192.168.0.0/255.255.255.0 Deny from all Order Deny,Allow AuthType Basic AuthName "Access ERROR!!" AuthUserFile "conf/.htpasswd" ※Order : Orderから遠い記述から優先される Require valid-user Order Allow,Deny と記述した場合、すべてのネットワークから アクセスできなくなります。 </Directory> 4.Apache 再起動 -21 - 第3回 Webサーバー設定 HTTPステータスコードについて HTTP通信時、Webサーバからの応答を3桁の数字でクライアントへ返します。 この3桁の数字のことを、HTTPステータスコードになります。 100番台:Infomation情報 リクエストを受け取った 200番台:正常処理 300番台:Redirection 400番台:クライアントエラー 500番台:サーバエラー 代表的なステータスコード一覧 正常 内容 番号 100 処理の継続 200 リクエスト正常終了 304 アクセスしたページ未更新 番号 400 401 403 404 500 503 504 -22 - エラー 内容 不正なリクエスト 認証エラー 禁止されているページへアクセスした ページが見つからなかった サーバ内部エラー サービス利用不可 ゲートウェイタイムアウト 第3回 Webサーバー設定 ステータスコードの応じたページのカスタマイズ httpd.confファイル設定 347 # 348 # Customizable error responses come in three flavors: 349 # 1) plain text 2) local redirects 3) external redirects 350 # 351 # Some examples: 352 #ErrorDocument 500 "The server made a boo boo." ① 353 ErrorDocument 404 /missing.html 354 #ErrorDocument 404 "/cgi-bin/missing_handler.pl" 355 #ErrorDocument 402 http://www.example.com/subscription_info.html 356 # ①HTTPステータスコード 404(ページが見つからない)の場合、missing.htmlを 表示させる設定 -23 - 第3回 Webサーバー設定 演習問題1 下記の構成でApcheの設定を行いましょう。 認証OK 認証OK Member Check 認証リンク http://localhost/login.html ユーザID パスワード http://localhost/accessLimit/index.html 認証NG 認証に失敗しました! http://localhost/401.html -24 - 第3回 Webサーバー設定 2.アクセスログの設定方法 -25 - 第3回 Webサーバー設定 Apacheのアクセスログ管理 Apacheは、外部からアクセスした様々な情報ログを書き出すロギング機能が あります。 ログを管理することで、サーバの状態を把握し、プログラムのエラーを検知し、 セキュリティの確保を行うことができます。 ログはサーバにとって貴重な情報源になります。 ログの種類 Apacheのログは、2つ種類があります。 アクセスログ ⇒外部からWebサービスに接続した記録を取るためのログ クライアント名(IPアドレスやホスト名) アクセス日時、クライアントからのリクエスト内容等 エラーログ ⇒サーバ側で発生したさまざまなエラー(HTTPステータス 400番,500番) を記録します CGIエラーログ ⇒CGIスクリプトエラーが発生した場合に記録します -26 - 第3回 Webサーバー設定 ログの設定 ログの設定は httpd.conf ファイルで行います。 AccessLog 207 208 LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined LogFormat "%h %l %u %t \"%r\" %>s %b" common 216 217 218 219 220 221 222 # The location and format of the access logfile (Common Logfile Format). # If you do not define any access logfiles within a <VirtualHost> # container, they will be logged here. Contrariwise, if you *do* # define per-<VirtualHost> access logfiles, transactions will be # logged therein and *not* in this file. # CustomLog "logs/access_log" common %h %l %u %t %r %>s %b %{Referer}i %{User-Agent}i リモートホスト名 クライアント識別子 ユーザID アクセス日時 リクエスト内容 httpステータスコード 送信されたバイト数 参照元 ユーザエージェント(Webブラウザ) -27 - ※設定する項目によって、logファイルの 肥大化が発生しますので、注意が必要です! 第3回 Webサーバー設定 logs/access.log その他のパラメータ %a %b %D %f %q %T クライアントIPアドレス 送信されたバイト数 サーバーがリクエストに返答するまでの時間 アクセスファイル名 問い合わせ文字列 リクエスト返信時間 -28 - 第3回 Webサーバー設定 ErrorLog [Sat Feb 26 11:23:50 2011] [error] [client 127.0.0.1] user sdf not found: /accessLimit, referer: http://localhost/login.html [Sat Feb 26 11:26:58 2011] [notice] caught SIGTERM, shutting down [Sat Feb 26 11:27:00 2011] [notice] SSL FIPS mode disabled [Sat Feb 26 11:27:00 2011] [warn] Init: Session Cache is not configured [hint: SSLSessionCache] [Sat Feb 26 11:27:00 2011] [notice] SSL FIPS mode disabled [Sat Feb 26 11:27:00 2011] [notice] Apache/2.2.17 (Unix) mod_ssl/2.2.17 OpenSSL/1.0.0a-fips configured -resuming normal operations [Sat Feb 26 11:27:12 2011] [error] [client 127.0.0.1] user ds not found: /accessLimit/, referer: http://localhost/login.html [Sat Feb 26 15:32:10 2011] [notice] caught SIGTERM, shutting down [Sat Feb 26 15:32:17 2011] [notice] SSL FIPS mode disabled [Sat Feb 26 15:32:17 2011] [warn] Init: Session Cache is not configured [hint: SSLSessionCache] [Sat Feb 26 15:32:17 2011] [notice] SSL FIPS mode disabled [Sat Feb 26 15:32:17 2011] [notice] Apache/2.2.17 (Unix) mod_ssl/2.2.17 OpenSSL/1.0.0a-fips configured -resuming normal operations [Sat Feb 26 15:32:33 2011] [error] [client 127.0.0.1] File does not exist: /usr/local/apache2/htdocs/favicon.ico [Sat Feb 26 15:32:33 2011] [error] [client 127.0.0.1] File does not exist: /usr/local/apache2/htdocs/favicon.ico [Sat Feb 26 15:32:33 2011] [error] [client 127.0.0.1] File does not exist: /usr/local/apache2/htdocs/favicon.ico [Sat Feb 26 15:32:45 2011] [error] [client 127.0.0.1] user sugiura: authentication failure for "/accessLimit/": Password Mismatch, referer: http://localhost/login.html [Sat Feb 26 15:34:42 2011] [error] [client 127.0.0.1] File does not exist: /usr/local/apache2/htdocs/favicon.ico [Sat Feb 26 15:34:48 2011] [error] [client 127.0.0.1] File does not exist: /usr/local/apache2/htdocs/userAccess -29 - 第3回 Webサーバー設定 ログの内容 favicon.ico ブラウザのお気に入りに登録された場合に表示されるアイコンファイルです。 このアイコンファイルが無い場合、Errorログに出力されます。 トップページのディレクトリに配置します。 robots.txt 検索エンジン(google,yahoo等)のロボットに対して、サイトの巡回方法を 指定するテキストファイルです。 サイトのトップディレクトリに配置します。(htdocs直下) SEO対策として用いられる。 検索エンジンにヒットされないようにする設定 User-agent: * Disallow: / 検索エンジン(google)にadminOnly,cgi-binディレクトリ配下を巡回から省く設定 User-agent: Googlebot Disallow: /adminOnly/ Disallow: /cgi-bin/ -30 - 第3回 Webサーバー設定 ログの内容からセキュリティチェックを行う 1.ステータスコードの確認 404 (Not Found:ページが見つかりません) このエラーが頻発する場合、サーバーへのDoS攻撃(サーバーアタック) やApacheのセキュリティフォールを狙った攻撃をされている可能性が あります。 401 (Unauthorized:認証エラー) このエラーが頻発する場合、認証の失敗が原因であることから、 不正アクセスを行おうとしている場合や、ブルートフォースアタックツール (暗号を解読してパスワードを盗むソフト)を使用した攻撃をされている 可能性があります。 2.不信なファイル名へのアクセス このエラーが頻発する場合、Webアプリケーションファイルを不正に取得 しようとしている場合や、アプリケーションのセキュリティフォールを攻撃 しようとしている可能性があります。 -31 - 第3回 Webサーバー設定 ログの内容からセキュリティチェックを行う 1.ステータスコードの確認 404 (Not Found:ページが見つかりません) このエラーが頻発する場合、サーバーへのDoS攻撃(サーバーアタック) やApacheのセキュリティフォールを狙った攻撃をされている可能性が あります。 401 (Unauthorized:認証エラー) このエラーが頻発する場合、認証の失敗が原因であることから、 不正アクセスを行おうとしている場合や、ブルートフォースアタックツール (暗号を解読してパスワードを盗むソフト)を使用した攻撃をされている 可能性があります。 2.不信なファイル名へのアクセス このエラーが頻発する場合、Webアプリケーションファイルを不正に取得 しようとしている場合や、アプリケーションのセキュリティフォールを攻撃 しようとしている可能性があります。 -32 - 第3回 Webサーバー設定 アクセスログを解析する Webalizerによるアクセスログ解析 ⇒解析ツールを利用することによって、ログの視覚化を行います。 ・Webalizerの機能 1.トラフィック統計(月、日、時間毎) 2.ヒット数ランキング(Webページ) 3.リファラー(どのページより遷移してきたか) 4.クライアントのブラウザ、OS情報 等々・・・ -33 - 第3回 Webサーバー設定 Webalizerのインストール インストールの流れ 1.yum コマンドにてインストール 2.httpd.conf設定 3.webalizer.conf設定 4.Apache再起動 5.動作確認 1.yumコマンドにてインストール rootユーザで作業を行います。 yum -y install webalizer -34 - 第3回 Webサーバー設定 2.httpd.conf ファイル設定 Webalizerページが表示できるように設定します。 # Webalizer設定 Alias /webalizer /var/www/usage <Location /webalizer> Order deny,allow Deny from all Allow from 127.0.0.1 Allow from ::1 </Location> 3.webalizer.confファイル設定 /etc/webalizer.confを設定します。 #LogFile LogFile /var/log/httpd/access_log /usr/local/apache2/logs/access_log 4.Apache再起動 -35 - 第3回 Webサーバー設定 5.動作確認 http://localhost/webalizer/index.html -36 - 第3回 Webサーバー設定 3.セキュアなWebサービスの構築方法 -37 - 第3回 Webサーバー設定 セキュアなWebサービスの構築 OpenSSLを利用したHTTPSページの構築方法 SSL通信:WWWやFTPなどのデータを暗号化し、安全な通信を行う OpenSSL:SSL通信に必要な各種証明書を作成するパッケージ CA(Certificate Authority):電子証明書を発行し、管理する機関 CSR(Certificate Signing Request)ファイル:公開鍵、サーバ情報を記載 HTTPS クライアント 秘密鍵ファイル Webサーバ サーバー証明書ファイル SSL通信が開始されると、ブラウザには鍵のマークが表示されます -38 - 第3回 Webサーバー設定 OpenSSL設定までの流れ Webサーバ SSL認証局 ①OpenSSLをインストールします ⑤サーバー証明書発行 ②秘密鍵を作成します ④CSRファイルを提出 ⑥サーバー証明書を適用 秘密鍵ファイル ③公開鍵を基にCSRファイルを作成します ※④’自己署名証明書 SSL認証局を通さずにサーバー証明書を作成する 限られたネットワークやテスト用として運用します。 CSRファイル -39 - 第3回 Webサーバー設定 ①OpenSSLインストール http://www.openssl.org/ openssl-1.0.0d.tar.gzをダウンロードします。 ダウンロードしたファイルを /usr/local/src へコピーします。 -40 - 第3回 Webサーバー設定 ・インストール ./config make make install ・opensslのライブラリにパスを通します echo "/usr/local/ssl/lib" >> /etc/ld.so.conf ・設定を反映します ldconfig [root@user01 etc]# echo "/usr/local/ssl/lib" >> /etc/ld.so.conf [root@user01 etc]# ldconfig [root@user01 etc]# -41 - 第3回 Webサーバー設定 ②秘密鍵の作成 1.confディレクトリに移動します cd /usr/local/apache2/conf/ 2.秘密鍵作成コマンドの発行 openssl genrsa -des3 1024 > secret.key genrsa:RSA形式の秘密鍵の作成 -des3:des3方式で暗号 1024 : 1024バイトの鍵を生成 secret.key : 秘密鍵名 [root@user01 conf]# openssl genrsa -des3 1024 > secret.key Generating RSA private key, 1024 bit long modulus ......++++++ ......++++++ e is 65537 (0x10001) Enter pass phrase: Verifying - Enter pass phrase: [root@user01 conf]# 秘密鍵を覗いてみましょう! -42 - 第3回 Webサーバー設定 ③CSRファイルの作成 [root@user01 conf]# openssl req -new -text -out webcsr.pem -keyout secret.key Generating a 2048 bit RSA private key ........................+++ ................................................................................................................................................+++ writing new private key to 'secret.key' Enter PEM pass phrase: Verifying - Enter PEM pass phrase: ----You are about to be asked to enter information that will be incorporated into your certificate request. What you are about to enter is what is called a Distinguished Name or a DN. There are quite a few fields but you can leave some blank For some fields there will be a default value, If you enter '.', the field will be left blank. ----Country Name (2 letter code) [XX]: State or Province Name (full name) []: Locality Name (eg, city) [Default City]:oita Organization Name (eg, company) [Default Company Ltd]:oita Organizational Unit Name (eg, section) []:ac Common Name (eg, your name or your server's hostname) []:user01.oita-u.testsv.ac.jp Email Address []: CSRファイル生成のコマンドオプション オプション req CSRファイルを作成 -new 新規にCSRファイルを作成 -key 秘密鍵のファイルを指定 Please enter the following 'extra' attributes to be sent with your certificate request A challenge password []:パスワードを入力 An optional company name []: [root@user01 conf]# -43 - 第3回 Webサーバー設定 ④自己署名証明書 運用では、SSL認証局へCSRファイルを提出し、サーバー証明書を発行する のですが、テスト運用・限られたネットワークのみでの運用として自己署名 証明書を作成します。 openssl x509 -in webcsr.pem -out server.crt -req -signkey secret.key -days 90 自己証明書作成のコマンドオプション オプション x509 X509形式のデジタル証明書を発行 -in CSRファイルを指定 -days 日数 証明書の有効期限 -req 入力ファイルがCSRであること -signkey 秘密鍵を指定 [root@user01 conf]# openssl x509 -in webcsr.pem -out server.crt -req -signkey secret.key -days 90 Signature ok subject=/C=XX/L=oita/O=oita/OU=ac/CN=user01.oita-u.testsv.ac.jp Getting Private key Enter pass phrase for secret.key:パスワードを入力 [root@user01 conf]# ls extra httpd.conf.org mime.types secret.key userExtra httpd.conf magic original server.cert webcsr.pem [root@user01 conf]# -44 - 第3回 Webサーバー設定 ⑤サーバー証明書を適用 Apache設定ファイルの修正 1.httpd.confの設定 422 # Secure (SSL/TLS) connections 423 Include conf/extra/httpd-ssl.conf 424 # ←コメントをはずします 2./conf/extra/httpd-ssl.confファイルの設定 92 # Server Certificate: 93 # Point SSLCertificateFile at a PEM encoded certificate. If 94 # the certificate is encrypted, then you will be prompted for a 95 # pass phrase. Note that a kill -HUP will prompt again. Keep 96 # in mind that if you have both an RSA and a DSA certificate you 97 # can configure both in parallel (to also allow the use of DSA 98 # ciphers, etc.) 99 SSLCertificateFile "/usr/local/apache2/conf/server.crt" 100 #SSLCertificateFile "/usr/local/apache2/conf/server-dsa.crt" 101 102 # Server Private Key: 103 # If the key is not combined with the certificate, use this 104 # directive to point at the key file. Keep in mind that if 105 # you've both a RSA and a DSA private key you can configure 106 # both in parallel (to also allow the use of DSA ciphers, etc.) 107 SSLCertificateKeyFile "/usr/local/apache2/conf/server.key" 108 #SSLCertificateKeyFile "/usr/local/apache2/conf/server-dsa.key" -45 - 第3回 Webサーバー設定 3.Apache再起動 [root@user01 conf]# /usr/local/apache2/bin/apachectl stop [root@user01 conf]# /usr/local/apache2/bin/apachectl start Apache/2.2.17 mod_ssl/2.2.17 (Pass Phrase Dialog) Some of your private key files are encrypted for security reasons. In order to read them you have to provide the pass phrases. Server www.example.com:443 (RSA) Enter pass phrase:パスワードを入力します OK: Pass Phrase Dialog successful. [root@user01 conf]# -46 - 第3回 Webサーバー設定 4.動作確認 ①https://localhost/topPage.html へアクセスします。 Mozilla Firefoxの場合、下記のページが表示されます。 -47 - 第3回 Webサーバー設定 ① ①【証明書を取得】を押下します。 ②次回以降にもこの例外を有効にする にチェックが入っていることを確認し、 【セキュリティ例外を承認】を押下します ② -48 - 第3回 Webサーバー設定 ①クリック SSL通信にてページが表示されます 作成した証明書を確認します -49 -