Apacheの設定

Apache、というかhttpサーバーの関連する設定はここにメモる。

WordPressを入れる
https://shirakawa.weblike.jp/blog-page/2022/03/13…
でApacheを入れたため、設定する。

今はローカルでWordPressのテスト用にしか使わないため、動けばいい簡単な設定にする。
HTML/CSS/JavaScriptの練習 local Webサーバ – Slackware-12.0 spitz GPE メモ
https://shirakawa.weblike.jp/blog-page/2021/05/08…
でやった事を参考にする。namedは入れない、hostsで解決する。

SZ-12のApacheではdefでは以下以外全部Loadしていたが、15.0の方はかなりコメントアウトしてある。
#LoadModule ssl_module lib64/httpd/modules/mod_ssl.so (SZ-12ではこれだけコメント)

Apacheとhostsだけでlocalhostでこんな感じの設定にしてみよう。
$ sudo nvi /etc/httpd/httpd.conf
\Listen localhost:80
\LoadModule vhost_alias_module lib64/httpd/modules/mod_vhost_alias.so
\Include /etc/httpd/extra/httpd-vhost.conf
\#ServerAdmin you@example.com
\ServerName localhost:80
(練習用ローカル環境だし、個別にやらずに/だけ denied -> granted 変更した。後に、
\extra/httpd-vhosts.conf でディレクトリ毎に設定して、denied に戻した)
Include /etc/httpd/mod_php.conf
$ sudo nvi /etc/httpd/extra/httpd-vhost.conf
\<VirtualHost localhost:80>
\ ServerAdmin admin@slack150memo.blog.example.com
\ DocumentRoot /myproj/slack150memo
\ ServerName slack150memo.blog.example.com
\ ServerAlias www.slack150memo.blog.example.com
\ ErrorLog /var/log/httpd/slack150memo.blog.example.com-error_log
\ CustomLog /var/log/httpd/slack150memo.blog.example.com-access_log common
\</VirtualHost>
\<VirtualHost localhost:80>
\ ServerAdmin admin@slack142memo.blog.example.com
\ DocumentRoot /myproj/slack142memo
\ ServerName slack142memo.blog.example.com
\ ServerAlias www.slack142memo.blog.example.com
\ ErrorLog /var/log/httpd/slack142memo.blog.example.com-error_log
\ CustomLog /var/log/httpd/slack142memo.blog.example.com-access_log common
\</VirtualHost>

$ mkdir -p /myproj/{slack150memo,slack142memo}/blog
$ nvi /etc/passwd (mysqlとapacheがいる事は確認した)
$ echo ‘It works! (15.0)’ >> /myproj/slack150memo/index.html
$ echo ‘It works! (14.2)’ >> /myproj/slack142memo/index.html

Apache起動:
$ sudo ifconfig lo 127.0.0.1 netmask 255.0.0.0
$ sudo sh /etc/rc.d/rc.httpd start
AH00558: httpd: Could not reliably determine the server’s fully qualified domain name, using darkstar.example.net. Set the ‘ServerName’ directive globally to suppress this message
https://ex1.m-yabe.com/archives/2965
(同様に設定した)
$ ps ax | grep http
\16829 ? Ss 0:00 /usr/sbin/httpd -k start
\16830 ? Sl 0:00 /usr/sbin/httpd -k start
\16831 ? Sl 0:00 /usr/sbin/httpd -k start
\16832 ? Sl 0:00 /usr/sbin/httpd -k start

$ sudo installpkg n/curl-7.81.0-x86_64-1.txz
$ curl http://slack150memo.blog.example.com
It works! (15.0)

/*************************************************************************************/
CGI(PHP含む)を使う設定:
https://shirakawa.weblike.jp/blog-page/2021/05/08… を参考に、
apache scriptalias php、でググった:
https://riceplanting.hatenablog.jp/entry/2013/05/19/185808
$ sudo nvi /etc/httpd/httpd.conf
\#LoadModule proxy_fcgi_module lib64/httpd/modules/mod_proxy_fcgi.so
\#LoadModule proxy_scgi_module lib64/httpd/modules/mod_proxy_scgi.so
\ LoadModule cgid_module lib64/httpd/modules/mod_cgid.so
\ LoadModule cgi_module lib64/httpd/modules/mod_cgi.so
\ScriptAlias /cgi-bin/ “/srv/httpd/cgi-bin/”
\Scriptsock cgisock
\Options Indexes FollowSymLinks ExecGCI
\defのDocumentRootはそのまま、DocumentRootの制御設定については、気が向いたら追加しよう。
\AddHandler cgi-script .cgi
\Include /etc/httpd/mod_php.conf
$ sudo elvis /etc/httpd/extra/httpd-vhosts.conf
\/etc/httpd/httpd.confの<Directory “/srv/httpd/cgi-bin”>をコピーして<Directory “/myproj/slack150memo/blog”>に変更して追加。
$ sudo elvis /etc/httpd/mod_php.conf (以下を追加)
\AddHandler php7-script .php
\AddType text/html .php
\DirectoryIndex index.php

nviはたまに英数字で文字化けしてカーソル操作できない事があるなあ、やっぱelvis使う事にした。
$ sudo installpkg a/elvis-2.2_0-x86_64-8.txz
結局、日本語fontの時は読まなきゃならない場合の方が多いので、その時は当面emacs使うことに。
$ sudo removepkg /var/log/packages/nvi-1.81.6-x86_64-3
(その後、ja_jp.utf8表示がurxvtでできるようになったので、やっぱりnvi使うことにした)

/*************************************************************************************/
https化についてSSHとSSLで同じような方法で認証してるんだろと勘違いしていた件:2022.10
https://qiita.com/satodayo/items/b45573b7165235a15152 (ssh ssl、でググった)
SSLでは認証局が発行した証明書により、接続先が信頼できるかどうかを確かめることができた。
一方でSSHにおいては、接続先の証明書となるものが公開鍵である。
ssh keygen、でググった:
https://hana-shin.hatenablog.com/entry/2021/12/21/202454
フィンガープリント(署名)とは、公開鍵をハッシュ関数でハッシュした値です。(md5sumみたいなもの、公開鍵は初回login時にサーバーから渡される)
ssh 公開鍵は何のため、でググった:
https://pfs.nifcloud.com/navi/words/ssh.htm
SSH公開鍵認証のメリットは?
公開鍵認証のポイントは、パスワード認証とは違って、認証に必要な秘密情報(この場合は秘密鍵)そのものはネットワーク上に送出されないという点です。 また認証に使われる署名は一時的なものであり、再利用はできないため、署名そのものを盗んでも意味がありません。 そのためパスワード認証に比べ、通信の盗聴に強いと言えるでしょう。
\
参考:PC側のssh設定
https://shirakawa.weblike.jp/blog-page/2024/01/27…

/*************************************************************************************/
https://shirakawa.weblike.jp/blog-page/2022/09/28…
https://oldcygwin.web.fc2.com/index.html で、
RootDirの下のimagesディレクトリをGimp用(Linux環境)とXD用(Win環境)に分けてリンク貼ったら画像が
表示されなくなった:2023.03
httpd directory link 有効化、でググった:
https://kkayataka.hatenablog.com/entry/2013/05/13/221333
https://www.javadrive.jp/apache/ini/index24.html
(うまくいった。これまでCGIくらいしかいじってないな)
\
httpd コンテントネゴシエーション とは、でググった:
https://developer.mozilla.org/ja/docs/Web/HTTP/Content_negotiation
(色々とメンドクサそう。今はいいや)

/*************************************************************************************/
マジメにhttps化することにした:2023.08
https://shirakawa.weblike.jp/blog-page/2023/07/28…
で採ってきてもhttpsのサイトは再現できない事がある。WordPressのサイトはその可能性高い。httpsを
全部httpに変換すれば見れない事もないが、ファイル多くてメンドクサくなった。
秘密鍵を指定してhttp-serverを起動する apache、でググった:
https://qiita.com/jinnai73/items/638dcc1434d47b12e6ba
(最初にこっちを実行したが、なぜかエラーになる)
https://qiita.com/kaburankattara/items/87db221061a9a3528188
(こっちを実行した。警告は出るがアクセスできるようになる)
https://www.einspki.jp/support/apache_inst/

Apache停止
$ sudo sh /etc/rc.d/rc.httpd stop

秘密鍵とCSR(証明書発行要求)を作成
$ openssl req -new -out localhost.csr
(サンプルと同じ様に入れた)

CSRファイルでサーバ証明書を作成(local専用なので自分で署名)
$ openssl x509 -in localhost.csr -out localhost.crt -req -signkey privkey.pem -days 365
(省略)

ファイル置き場
$ find /etc/httpd/ -type f -exec grep -H key \{\} \;
/etc/httpd/extra/httpd-ssl.conf:SSLCertificateKeyFile “/etc/httpd/server.key”
/etc/httpd/extra/httpd-ssl.conf:#SSLCertificateKeyFile “/etc/httpd/server-dsa.key”
/etc/httpd/extra/httpd-ssl.conf:#SSLCertificateKeyFile “/etc/httpd/server-ecc.key”
$ find /etc/httpd/ -type f -exec grep -H crt \{\} \;
/etc/httpd/mime.types:application/x-x509-ca-cert der crt
/etc/httpd/extra/httpd-ssl.conf:SSLCertificateFile “/etc/httpd/server.crt”
/etc/httpd/extra/httpd-ssl.conf:#SSLCertificateFile “/etc/httpd/server-dsa.crt”
/etc/httpd/extra/httpd-ssl.conf:#SSLCertificateFile “/etc/httpd/server-ecc.crt”
/etc/httpd/extra/httpd-ssl.conf:#SSLCertificateChainFile “/etc/httpd/server-ca.crt”
/etc/httpd/extra/httpd-ssl.conf:#SSLCACertificatePath “/etc/httpd/ssl.crt”
/etc/httpd/extra/httpd-ssl.conf:#SSLCACertificateFile “/etc/httpd/ssl.crt/ca-bundle.crt”
\
$ sudo mv -i localhost.pem /etc/httpd/server.key
$ sudo mv -i localhost.crt /etc/httpd/server.crt
$ rm localhost.csr

SSLモジュールの有効化とApache起動
$ sudo nvi /etc/httpd/httpd.conf
LoadModule ssl_module lib64/httpd/modules/mod_ssl.so (コメントアウトを外した)
# Secure (SSL/TLS) connections
Include /etc/httpd/extra/httpd-ssl.conf (コメントアウトを外した)
localhost:80 -> localhost:443 (変更した)
#Listen 80 (コメントアウトした)
$ sudo nvi /etc/httpd/extra/httpd-vhost.conf
localhost:80 -> localhost:443 (変更した)

$ sudo sh /etc/rc.d/rc.httpd start
AH00526: Syntax error on line 92 of /etc/httpd/extra/httpd-ssl.conf:
SSLSessionCache: ‘shmcb’ session cache not supported (known names: ). Maybe you need to load the appropriate socache module (mod_socache_shmcb?).
$ sudo nvi /etc/httpd/httpd.conf
LoadModule socache_shmcb_module lib64/httpd/modules/mod_socache_shmcb.so (コメントアウトを外した)
\
$ sudo sh /etc/rc.d/rc.httpd start
(98)Address already in use: AH00072: make_sock: could not bind to address [::]:443
(98)Address already in use: AH00072: make_sock: could not bind to address 0.0.0.0:443
no listening sockets available, shutting down
AH00015: Unable to open logs
\
(98)Address already in use: AH00072: make_sock: could not bind to address [::]:443、でググった:
https://mebee.info/2020/03/19/post-7434/
$ sudo lsof -i | grep http
(そりゃ、Firefoxとぶつかるわな。localhost:443 にアクセス絞らないと)
$ sudo nvi /etc/httpd/extra/httpd-ssl.conf
#Listen 443 (コメントアウトした)
# General setup for the virtual host
#DocumentRoot “/srv/httpd/htdocs” (vhostで設定済み。コメントアウトした)
#ServerName www.example.com:443 (vhostで設定済み。コメントアウトした)
#ServerAdmin you@example.com (vhostで設定済み。コメントアウトした)
#ErrorLog “/var/log/httpd/error_log” (vhostで設定済み?。コメントアウトした)
#TransferLog “/var/log/httpd/access_log” (vhostで設定済み?。コメントアウトした)
$ sudo sh /etc/rc.d/rc.httpd start
(エラーは出なくなった)

ブラウザでhttpsアクセスしてみると、
安全な接続ができませんでした
oldcygwin.web.fc2.com への接続中にエラーが発生しました。SSL received a record that exceeded the maximum permissible length.
エラーコード: SSL_ERROR_RX_RECORD_TOO_LONG
https://knowledgebase.paloaltonetworks.com/KCSArticleDetail?id=kA10g000000CldaCAC&lang=ja
Errror コード: “SSL_ERROR_RX_RECORD_TOO_LONG” とは、web サーバーがセキュリティで保護された (HTTPS) データが web ブラウザによって期待されている非セキュア (HTTP) データを送信していることを意味します。
\
apache SSL received a record that exceeded the maximum permissible length.、でググった:
let’s encryptでハマる – CHOICE
https://choice-site.com/2019/11/07/lets-encrypt%E3…
https://choice-site.com/2012/09/10/virtualhost%e3%…
(要するにvhostの設定はsslの後ろにしろ、と)
$ sudo nvi /etc/httpd/httpd.conf
# Virtual hosts
#Include /etc/httpd/extra/httpd-vhosts.conf (コメントアウト)
\
# Secure (SSL/TLS) connections
Include /etc/httpd/extra/httpd-ssl.conf
Include /etc/httpd/extra/httpd-vhosts.conf (こっちに移動した)
\
(これでローカルのhttpsにアクセスできた)

/*************************************************************************************/
セキュリティー上、一般的に、Apache(httpd)にて、X-Content-Type-Options: nosniff を設定すべきか? (->ChatGPT)
はい、一般的にセキュリティ上の観点からは、Apache(httpd) においても `X-Content-Type-Options: nosniff` を設定すべきです。
これは、Webセキュリティの基本的なベストプラクティスの一つとして推奨されています。

## 🔒 理由:MIMEスニッフィング攻撃の防止
`X-Content-Type-Options: nosniff` は、ブラウザが **Content-Typeヘッダーを無視して中身を勝手に判定(スニッフィング)する挙動**を禁止します。
もしこれを設定していない場合:
* たとえば、本来 `text/plain` として配信したファイルが、ブラウザによって「実はHTMLやJavaScriptかも」と誤認識され、
* 結果的にスクリプトとして実行されてしまう(XSS脆弱性の誘発)
といったリスクがあります。

## ✅ 設定が推奨される対象
ほぼ **すべてのWebサイト**(静的/動的問わず)で設定を推奨されます。
特に:
* 公開サイト(ユーザーがファイルをアップロード・閲覧できる)
* ECサイトや会員制サイトなど、セッションやCookieを扱うサイト
* 複数のMIMEタイプ(HTML, JS, CSS, 画像, PDFなど)を返すサイト
では、**確実に有効化すべき**です。
⚙️ Apacheでの設定例
Apacheでは `mod_headers` モジュールを有効にして、以下を設定します。
### 全体設定(推奨)
`/etc/httpd/conf/httpd.conf` または各VirtualHost設定に:
“`apacheconf
<IfModule mod_headers.c>
Header always set X-Content-Type-Options “nosniff”
</IfModule>
“`
### `.htaccess`で設定する場合
“`apacheconf
<IfModule mod_headers.c>
Header set X-Content-Type-Options “nosniff”
</IfModule>/

“`

## 🧠 補足:他の関連ヘッダーも合わせて推奨
セキュリティ強化を行う場合は、以下も合わせて設定するのが一般的です。
| ヘッダー名                                    | 効果                                       |
| --------------------------------------------- | ------------------------------------------ |
| `X-Frame-Options: SAMEORIGIN`                 | クリックジャッキング対策                   |
| `X-XSS-Protection: 1; mode=block`             | 古いブラウザ向けのXSS防止(現代ではCSP推奨) |
| `Referrer-Policy: no-referrer-when-downgrade` | リファラ制御                               |
| `Content-Security-Policy`                     | スクリプト実行制限(XSS防止)              |
| `Strict-Transport-Security`                   | HTTPS通信を強制(HSTS)                    |

## 🧾 まとめ
| 項目         | 内容                                   |
| ------------ | -------------------------------------- |
| 設定推奨度   | ★★★★★(強く推奨)                 |
| 設定場所     | Apache設定または.htaccess              |
| 主な目的     | MIMEスニッフィング攻撃防止             |
| 対応ブラウザ | 主要ブラウザ全対応                     |
| 弊害         | 通常なし(適切なContent-Type指定が前提) |

/*************************************************************************************/

 

参考情報:
apache php linux 設定、でググった:
https://qiita.com/yukibe/items/06052357636b0a6fb2f4
https://www.ritolab.com/entry/2
Apacheで403 Forbidden、でググった:
https://engineers.weddingpark.co.jp/apache-403-forbidden/
wordpress apache 設定、でググった:
https://www.serotoninpower.club/archives/624/#%E7%92%B0%E5%A2%83
https://rfs.jp/sb/wordpress/wp-howto/wp01_install.html
certbot とは、でググった:
https://qiita.com/bnn848/items/ff398b98527700529f64
webサーバ アプリケーションサーバ 分離、でググった: (2023.11)
https://qiita.com/yohattor/items/7c56fa57e850c33ea47d
apach ファイルサーバ、でググった: (2023.11)
http://honana.com/apache/apache_22/webdav
https://bashalog.c-brains.jp/10/05/20-091807.php

installまたは展開時のメッセージ:
# curl (command line URL data transfer tool)
# Curl is a command line tool for transferring data specified with URL
# syntax. The command is designed to work without user interaction or
# any kind of interactivity. Curl offers a busload of useful tricks
# like proxy support, user authentication, ftp upload, HTTP post, SSL
# (https:) connections, cookies, file transfer resume and more.
# libcurl is a library that Curl uses to do its job. It is readily
# available to be used by your software, too.
#
# lsof (list open files)
# Lsof is a Unix-specific tool. Its name stands for “LiSt Open Files”,
# and it does just that. It lists information about files that are open
# by the processes running on the system.
# Victor A. Abell of Purdue University is the developer of lsof.
#

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です