Index ソフト・ハード | Apache タスク |
機能 構成・方式 動作 タスク 起動 FW許可 SSL導入 LVS使用時 ルータ使用時 Let'sEncrypt 更新エラー rewrite ログ httpd.conf セキュリティ WSGI バーチャルH インストール 負荷分散 クラスタ化 |
Apacheの起動(CentOS7) ・httpd.serviceを起動 # systemctl start httpd.service・システム起動時に起動する。 # systemctl enable httpd.serviceファイアウォール許可(CentOS7) ・httpsでのアクセスを許可、システム起動時に許可する。 # firewall-cmd --permanent --add-service=https --zone=public # firewall-cmd --reload・ファイアウォール確認 ApacheにSSL導入 ・通常手順 自分のWebサーバーでCSRとKEYを作成 CSRをCAへ送信して証明書の発行依頼 CAから電子署名されたCRTと中間CA証明書を受け取る。 受け取った証明書などをWebサーバーへ設置・mod_sslモジュールインストール パッケージ確認 $ yum info mod_ssl・インストール済みパッケージ
名前 : mod_ssl
アーキテクチャー : x86_64
エポック : 1
バージョン : 2.4.6
リリース : 90.el7.centos
容量 : 224 k
・mod_sslインストール # yum install mod_ssl・インストール確認 # httpd -M | grep ssl ssl_module (shared)・仮の証明書と秘密鍵が生成され、とりあえずSSLは実行可能 ・SSLで使用する秘密鍵(例、server.key)の生成例 # openssl genrsa -out server.key -aes128 2048・秘密鍵からCSR(例、server.csr)の生成例 # openssl req -out server.csr -new -key server.key・CSRから自己証明勝書(例、server.crt)の生成例 # openssl x509
-in server.csr -out server.crt -days 365 -signkey server.key -req 作成した「server.crt」を「/etc/httpd/conf/ssl.crt」へ移動・Certbotは無料かつ自動(CSRとKEY作成も含む)でSSL証明書を発行できるツール 「Let's Encrypt 証明書」の作成LVSによる負荷分散時 ・lvs ・Ldirectord ・ipvsadm LVS入力にブロードバンドルータ使用時 ・ポート80設定
# ipvsadm -Lc
IPVS connection entries
pro expire state source virtual destination
TCP 00:05 SYN_RECV buffalo.setup:49743 kvm35:http localhost:http
# ipvsadm -L
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP kvm35:http rr
-> localhost:http Route 1 0 4
-> 192.168.5.33:http Route 0 0 0
-> 192.168.5.43:http Route 0 0 0
TCP kvm35:https rr
-> localhost:https Route 1 0 0
-> 192.168.5.33:https Masq 0 0 0
-> 192.168.5.43:https Masq 0 0 0
Let's Encrypt 証明書を使用 ・Certbotクライアントを用いて取得 有効期間90日 cronで延長・Certbotクライアント インストール(確認) ・SSL/TLS証明書の作成 # cd /usr/local/certbot/ (certbotは /usr/local にインストール済み。) # ./certbot-auto certonly --webroot -w /var/www/html/ -d www.xxxx.jp メールアドレスを入力 (A)gree/(C)ancel: A (Y)es/(N)o: N (Electronic Frontier Foundation とメールアドレスを共有) 正常終了の場合、サーバー証明書などが作成・作成されたサーバー証明書 # ls -l /etc/letsencrypt/live/www.xxxx.xxx/・Apache 2.4 の設定(/etc/httpd/conf.d/ssl.conf)
・・・
SSLCertificateFile /etc/letsencrypt/live/[サーバーのドメイン]/cert.pem
SSLCertificateKeyFile /etc/letsencrypt/live/[サーバーのドメイン]/privkey.pem
SSLCertificateChainFile /etc/letsencrypt/live/[サーバーのドメイン]/chain.pem
・証明書の更新 # certbot renewLet's Encrypt 証明書の更新のエラー ・http-01チャレンジの失敗 負荷分散で複数サーバの場合は1台だけで更新する。
{
"identifier": {
"type": "dns",
"value": "www.xxxx.xxx"
},
"status": "invalid",
#"status": "valid",
"expires": "2020-12-10T05:47:27Z",
"challenges": [
{
"type": "http-01",
"status": "invalid",
"error": {
"type": "urn:ietf:params:acme:error:unauthorized",
"detail": "無効な応答"
"status": 403
#"status": "valid",
},
:
:
・参考rewrite ・module /etc/httpd/conf/httpd.conf Include conf.modules.d/*.conf /etc/httpd/conf.modules.d/00-base.conf LoadModule rewrite_module modules/mod_rewrite.soログの管理 ・ログフォーマットの定義パターン(httpd.conf) LogFormat "%h %l %u %t "%r" %>s %b "%{Referer}i" "%{User-Agent}i"" combined LogFormat "%h %l %u %t "%r" %>s %b" common LogFormat "%{Referer}i -> %U" referer LogFormat "%{User-agent}i" agent・httpd.conf デフォルト
<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>
# You need to enable mod_logio.c to use %I and %O
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %I %O" combinedio
</IfModule>
#CustomLog "logs/access_log" common
CustomLog "logs/access_log" combined
</IfModule>
・ログのフォーマットは自分で定義可能・tab区切り、ラベル付けしたLTSVフォーマットでログ出力も可能 CustomLog logs/access_log.ltsv ltsv LogFormat "ts:%t\\t ip:%a\\t status:%>s\\t method:%m\\t path:%U\\t query:%q\\t referer:%{Referer}i\\t ua:%{User-Agent}i\\t usec:%D\\t received:%I\\t sent:%O" ltsv・haCheckなどアクセスは対象外 特定のIPからのアクセスを記録しない。(「Remote_Addr」パラメータ) SetEnvIf Remote_Addr "192.168.50.45" nolog 特定のユーザからのアクセスもコントロールできる。 「nolog」と不一致なログは記録する。(「env=!nolog」をCustomLogディレクティブに追記) CustomLog access_log combined env=!nolog・アクセスログ /var/log/httpd/access_log・設定確認 # apachectl -t・カスタムログ書式 参考httpd.conf(2.4)
ServerRoot "/etc/httpd"
Listen 80
Include conf.modules.d/*.conf
User apache
Group apache
ServerAdmin root@localhost
#ServerName www.example.com:80
<Directory />
AllowOverride none
Require all denied
</Directory>
DocumentRoot "/var/www/html"
<Directory "/var/www">
AllowOverride None
# Allow open access:
Require all granted
</Directory>
<Directory "/var/www/html">
#Options Indexes FollowSymLinks
Options FollowSymLinks
AllowOverride None
Require all granted
</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>
# You need to enable mod_logio.c to use %I and %O
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %I %O" combinedio
</IfModule>
#CustomLog "logs/access_log" common
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-gzip .tgz
#AddEncoding x-compress .Z
#AddEncoding x-gzip .gz .tgz
AddType application/x-compress .Z
AddType application/x-gzip .gz .tgz
#AddHandler cgi-script .cgi
#AddHandler type-map var
AddType text/html .shtml
AddOutputFilter INCLUDES .shtml
</IfModule>
AddDefaultCharset UTF-8
<IfModule mime_magic_module>
MIMEMagicFile conf/magic
</IfModule>
#ErrorDocument 500 "The server made a boo boo."
#ErrorDocument 404 /missing.html
#ErrorDocument 404 "/cgi-bin/missing_handler.pl"
#ErrorDocument 402 http://www.example.com/subscription_info.html
#EnableMMAP off
EnableSendfile on
# Load config files in the "/etc/httpd/conf.d" directory, if any.
IncludeOptional conf.d/*.conf
# HTTP TRACE method off
TraceEnable off
# X-Frame-Options
Header always set X-Frame-Options "SAMEORIGIN"
# Version
ServerTokens ProductOnly
ServerSignature off
セキュリティ ・サービス運用上必要最低限のコンテンツのみ公開 ・不要なコンテンツは公開領域に配置しない。 ・デフォルトコンテンツ apache-welcomeページやデフォルトのエラーページで利用 サービス提供上、公開不要なページ /etc/httpd/conf.d/welcome.conf 必要ならば削除などを行う。 確認 # httpd -t Syntax OK # systemctl restart httpd.service (以下、同様に)・デフォルトコンテンツで使う、iconsフォルダ /etc/httpd/conf.d/autoindex.conf 必要ならば削除などを行う。・ディレクトリ内容一覧表示への対応 該当するディレクトリのIndexesオプションを無効にする。・HTTP TRACEメソッドの無効化 「HTTP TRACE method off」を「httpd.conf」に追記 確認 # curl -v -X TRACE http://192.168.1.xx/ 「405 Method Not Allowed」を確認する。・クリックジャッキング(Clickjacking、リンクなどの要素を隠蔽、偽装してクリックを誘う。)対策 X-Frame-Optionsレスポンスヘッダーを設定 「Header always set X-Frame-Options "SAMEORIGIN"」を「httpd.conf」に追記 確認 # curl -v -X TRACE http://192.168.1.xx/ 「X-Frame-Options: SAMEORIGIN」を確認する。・Apacheバージョン表示の無効化 Versionの無効化を「httpd.conf」に追記 ServerTokens ProductOnly ServerSignature off 確認 # curl -v -X TRACE http://192.168.1.xx/ 「Server: Apache/2.4.6 (CentOS)」→「Server: Apache」・Apache + mod_sslの場合 「heartbleed」がないOpenSSLを使用する。
# yum info openssl
インストール済みパッケージ
名前 : openssl
アーキテクチャー : x86_64
エポック : 1
バージョン : 1.0.2k
リリース : 19.el7
リポジトリー : installed
提供元リポジトリー : base
ライセンス : OpenSSL
・脆弱性が存在するSSLv2、SSLv3の無効化を確認 「openssl s_client」コマンドで確認・SSL/TLSプロトコルのデータ圧縮機能の脆弱性対応 「openssl s_client」コマンドで確認 「Compression: NONE」を確認 必要なら、「ssl.conf」に SSLCompression off を追加WSGI (mod_wsgi)でDjangoを使用 ・WSGIアプリケーション用にmod_wsgiを設定する。 ・デーモンモードで使用する。 各サイトをそれぞれ独立したデーモンプロセスで実行する。バーチャルホスト ・WSGIアプリケーション用にmod_wsgiを設定する。 |
All Rights Reserved. Copyright (C) ITCL |