1.Apacheのインストールとセキュアな設定(Buster32bit)

Raspberry Pi OS Lite(32bit) buster時代の記事

Raspberry Pi OSがBuster(32bit)の時代に作成したものですが、当時のノウハウを残すために置いているページです。最新の記事は、ここを参照ください。

Raspberry Pi4にApache2をインストールして、最低限のセキュリティ設定をします。

Raspberry Pi OSのaptリポジトリには、2.4.38(リリース日:2019/01/22)と古いものが入っているので、最新版をSURY PPAリポジトリからインストールします。

ももぶろ
ももぶろ

最新版は2021/9/21時点、2.4.48(リリース日:2021/06/01)

SURY PPAリポジトリをaptリポジトリに追加

SURY PPAリポジトリを使用するため必要なパッケージをインストールします。

既にインストール済みであれば、この作業は不要です。

$ sudo apt install lsb-release apt-transport-https ca-certificates
Reading package lists... Done
Building dependency tree
Reading state information... Done
ca-certificates is already the newest version (20200601~deb10u2).
lsb-release is already the newest version (10.2019051400+rpi1).
lsb-release set to manually installed.
The following NEW packages will be installed:
  apt-transport-https
0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded.
Need to get 149 kB of archives.
After this operation, 156 kB of additional disk space will be used.
Do you want to continue? [Y/n] y
Get:1 http://ftp.tsukuba.wide.ad.jp/Linux/raspbian/raspbian buster/main armhf apt-transport-https all 1.8.2.3 [149 kB]
Fetched 149 kB in 1s (190 kB/s)
Selecting previously unselected package apt-transport-https.
(Reading database ... 42262 files and directories currently installed.)
Preparing to unpack .../apt-transport-https_1.8.2.3_all.deb ...
Unpacking apt-transport-https (1.8.2.3) ...
Setting up apt-transport-https (1.8.2.3) ...

Apache2のGPGキーを取得します。

$ sudo wget -O /etc/apt/trusted.gpg.d/apache2.gpg https://packages.sury.org/apache2/apt.gpg
--2021-09-11 08:45:59--  https://packages.sury.org/apache2/apt.gpg
Resolving packages.sury.org (packages.sury.org)... 172.67.182.150, 104.21.18.148, 2606:4700:3037::6815:1294, ...
Connecting to packages.sury.org (packages.sury.org)|172.67.182.150|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1769 (1.7K) [application/octet-stream]
Saving to: ‘/etc/apt/trusted.gpg.d/apache2.gpg’

/etc/apt/trusted.gpg.d/apache2.gpg      100%[==============================================================================>]   1.73K  --.-KB/s    in 0.001s

2021-09-11 08:46:00 (3.24 MB/s) - ‘/etc/apt/trusted.gpg.d/apache2.gpg’ saved [1769/1769]

SURY PPAリポジトリ のApache2をaptリポジトリに追加します。

$ echo "deb https://packages.sury.org/apache2/ $(lsb_release -sc) main" | sudo tee /etc/apt/sources.list.d/apache2.list
deb https://packages.sury.org/apache2/ buster main

パッケージ一覧をアップデートをします。

$ sudo apt-get update
Get:1 http://archive.raspberrypi.org/debian buster InRelease [32.7 kB]
Get:2 https://packages.sury.org/apache2 buster InRelease [6,750 B]
Get:3 http://archive.raspberrypi.org/debian buster/main armhf Packages [393 kB]
Get:4 http://raspbian.raspberrypi.org/raspbian buster InRelease [15.0 kB]
Get:5 https://packages.sury.org/apache2 buster/main armhf Packages [14.2 kB]
Fetched 461 kB in 3s (163 kB/s)
Reading package lists... Done
Building dependency tree
Reading state information... Done
1 package can be upgraded. Run 'apt list --upgradable' to see it.

インストールできるapache2のバージョンを確認します。

2.4.48なので最新です。

$ apt show apache2
Package: apache2
Version: 2.4.48-1.0+0~20210701.19+debian10~1.gbp4ca8fb
Priority: optional
Section: httpd
Maintainer: Debian Apache Maintainers <debian-apache@lists.debian.org>
Installed-Size: 625 kB
Provides: httpd, httpd-cgi
・
・  以降省略
・

パッケージの更新を行います。

$ sudo apt-get upgrade
Reading package lists... Done
Building dependency tree
Reading state information... Done
Calculating upgrade... Done
The following packages will be upgraded:
  libnghttp2-14
1 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
Need to get 72.3 kB of archives.
After this operation, 26.6 kB disk space will be freed.
Do you want to continue? [Y/n] y    <=y入力
Get:1 https://packages.sury.org/apache2 buster/main armhf libnghttp2-14 armhf 1.39.2-1+0~20200421.6+debian10~1.gbp3465e8 [72.3 kB]
Fetched 72.3 kB in 2s (42.2 kB/s)
・
・  以降省略
・

Apache2のインストール

下記のコマンドでApache2をインストールします。

出力メッセージの中で、SURY PPAのリポジトリが使われているのが確認できます。

$ sudo apt-get install apache2
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
  apache2-bin apache2-data apache2-utils libapr1 libaprutil1 libaprutil1-dbd-sqlite3 libaprutil1-ldap libbrotli1 libjansson4 liblua5.3-0 ssl-cert
Suggested packages:
  apache2-doc apache2-suexec-pristine | apache2-suexec-custom www-browser openssl-blacklist
The following NEW packages will be installed:
  apache2 apache2-bin apache2-data apache2-utils libapr1 libaprutil1 libaprutil1-dbd-sqlite3 libaprutil1-ldap libbrotli1 libjansson4 liblua5.3-0 ssl-cert
0 upgraded, 12 newly installed, 0 to remove and 0 not upgraded.
Need to get 2,514 kB of archives.
After this operation, 6,789 kB of additional disk space will be used.
Do you want to continue? [Y/n] y    <=y入力
Get:1 http://ftp.tsukuba.wide.ad.jp/Linux/raspbian/raspbian buster/main armhf libjansson4 armhf 2.12-1 [34.6 kB]
Get:2 http://ftp.tsukuba.wide.ad.jp/Linux/raspbian/raspbian buster/main armhf liblua5.3-0 armhf 5.3.3-1.1 [94.4 kB]
Get:3 https://packages.sury.org/apache2 buster/main armhf libapr1 armhf 1.7.0-8+0~20210825.10+debian10~1.gbp961fc5 [89.0 kB]
・
・  以降省略
・

念のためインストールされたApache2を確認します。

$ apachectl -v
Server version: Apache/2.4.48 (Debian)
Server built:   2021-07-01T19:20:24

最新です。

module_headersの活性化

HTTP のリクエストヘッダと応答ヘッダを制御し、 追加・変更・削除します。

セキュリティ対策の為必須です。

$ sudo a2enmod headers
Enabling module headers.
To activate the new configuration, you need to run:
  systemctl restart apache2

rewriteの活性化

アクセスのあったURLを、正規表現で書き換えてから処理する機能です。

書き換えで対処できることが一杯あるので有効にします。

$ sudo a2enmod rewrite
Enabling module rewrite.
To activate the new configuration, you need to run:
  systemctl restart apache2

セキュアな設定

有効と思われる全てのセキュアな設定を行っていきます。

インストール直後のHTTPヘッダの確認

インストール直後は下記の青い部分がHTTPヘッダとして出力されます。

$ curl -v  http://localhost | more
* Expire in 0 ms for 6 (transfer 0x11ba8b0)
* Expire in 1 ms for 1 (transfer 0x11ba8b0)
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0* Expire in 1 ms for 1 (transfer 0x11ba8b0)
* Expire in 1 ms for 1 (transfer 0x11ba8b0)
*   Trying 127.0.0.1...
* TCP_NODELAY set
* Expire in 149998 ms for 3 (transfer 0x11ba8b0)
* Expire in 200 ms for 4 (transfer 0x11ba8b0)
* Connected to localhost (127.0.0.1) port 80 (#0)
> GET / HTTP/1.1
> Host: localhost
> User-Agent: curl/7.64.0
> Accept: */*
>
< HTTP/1.1 200 OK
< Date: Sat, 11 Sep 2021 05:27:06 GMT
< Server: Apache/2.4.48 (Debian) <=余分な情報が出ている
< Last-Modified: Fri, 10 Sep 2021 23:56:21 GMT
< ETag: "29cd-5cbacdcb1c7dc"
< Accept-Ranges: bytes
< Content-Length: 10701
< Vary: Accept-Encoding
< Content-Type: text/html
・
・ 以降HTMLが続きます。
・

セキュアな設定

Apache2からできるセキュリティ対策を設定します。

$ sudo vi /etc/apache2/conf-enabled/security.conf
ももぶろ
ももぶろ

viの超簡単な使い方は、ここを見てね

下記の赤い値に変更してください。

無い行は追加をしてください。

行の頭に#が入っている行はコメントなので、コメントは追加する必要はありません。

設定の説明がコメントにあるので参考にしてください。

<Directory />
   AllowOverride None
   Require all denied
</Directory>
#
# ServerTokens
# This directive configures what you return as the Server HTTP response
# Header. The default is 'Full' which sends information about the OS-Type
# and compiled in modules.
# Set to one of:  Full | OS | Minimal | Minor | Major | Prod
# where Full conveys the most information, and Prod the least.
ServerTokens Prod

#
# Optionally add a line containing the server version and virtual host
# name to server-generated pages (internal error documents, FTP directory
# listings, mod_status and mod_info output etc., but not CGI generated
# documents or custom error documents).
# Set to "EMail" to also include a mailto: link to the ServerAdmin.
# Set to one of:  On | Off | EMail
ServerSignature Off

#
# Allow TRACE method
#
# Set to "extended" to also reflect the request body (only for testing and
# diagnostic purposes).
#
# Set to one of:  On | Off | extended
TraceEnable Off

#
# Setting this header will prevent MSIE from interpreting files as something
# else than declared by the content type in the HTTP headers.
# Requires mod_headers to be enabled.
#
Header set X-Content-Type-Options: "nosniff"

#
# Setting this header will prevent other sites from embedding pages from this
# site as frames. This defends against clickjacking attacks.
# Requires mod_headers to be enabled.
#
Header set X-Frame-Options: "sameorigin"

# バージョン情報の隠蔽
Header unset "X-Powered-By"

# XSS対策
Header set X-XSS-Protection "1; mode=block"

Apache2のサーバを再起動

$ sudo systemctl restart apache2.service

メッセージは何も出ません。

メッセージが出力されてるときは、エラーが有るのでしっかり対処しましょう。

セキュリティ設定後のHTTPヘッダの確認

$ curl -v http://localhost | more
* Expire in 0 ms for 6 (transfer 0x1b0a8b0)
* Expire in 1 ms for 1 (transfer 0x1b0a8b0)
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0* Expire in 2 ms for 1 (transfer 0x1b0a8b0)
* Expire in 2 ms for 1 (transfer 0x1b0a8b0)
*   Trying 127.0.0.1...
* TCP_NODELAY set
* Expire in 149996 ms for 3 (transfer 0x1b0a8b0)
* Expire in 200 ms for 4 (transfer 0x1b0a8b0)
* Connected to localhost (127.0.0.1) port 80 (#0)
> GET / HTTP/1.1
> Host: localhost
> User-Agent: curl/7.64.0
> Accept: */*
>
< HTTP/1.1 200 OK
< Date: Sat, 11 Sep 2021 20:05:56 GMT
< Server: Apache           <=バージョン情報なし
< Last-Modified: Fri, 10 Sep 2021 23:56:21 GMT
< ETag: "29cd-5cbacdcb1c7dc"
< Accept-Ranges: bytes
< Content-Length: 10701
< Vary: Accept-Encoding
< X-Content-Type-Options: nosniff   <=セキュリティ対策の為追加
< X-Frame-Options: sameorigin     <=セキュリティ対策の為追加
< X-XSS-Protection: 1; mode=block   <=セキュリティ対策の為追加
< Content-Type: text/html
・
・ 以降HTMLが続きます。
・
ももぶろ
ももぶろ

Apache2のインストールはこれで終了です。

タイトルとURLをコピーしました