Apache HTTP Serverのインストール

2021年9月27日

動作確認済みシステムDebian 11

Webサーバとして、Apache HTTP Serverをインストールします。

apt install apache2

headersモジュールが必要なので、有効化します。

a2enmod headers

セキュリティ上、ディレクトリ一覧を表示させないために、autoindexを無効にします。

a2dismod -f autoindex

設定ファイルを編集します。

vi /etc/apache2/apache2.conf
# Global configuration
# 追記
ServerName example.com
ServerAdmin administrator@example.com
vi /etc/apache2/conf-available/security.conf
# OSからProdに変更
ServerTokens Prod

# OnからOffに変更
ServerSignature Off

# Offであることを確認
TraceEnable Off

# コメント解除
Header set X-Content-Type-Options: "nosniff"

# コメント解除
Header set X-Frame-Options: "sameorigin"

# 以下を追記
Header unset "X-Powered-By"
Header set X-XSS-Protection "1; mode=block"
RequestHeader unset Proxy

サイトの設定を行います。

vi /etc/apache2/sites-available/000-default.conf
<VirtualHost *:80>
# 修正
ServerAdmin administrator@example.com

# 以下を追記
<Directory "/var/www/html">
Options MultiViews SymLinksIfOwnerMatch IncludesNoExec
AllowOverride All
Require all granted
</Directory>
</VirtualHost>

/var/wwwの所有グループ、所有ユーザをApacheの実行グループ、実行ユーザに合わせます。

chown -R www-data:www-data /var/www/

設定に誤りがないか確認します。

apachectl configtest

「Action 'configtest' failed.」と表示された場合、問題があるということなので、設定を見直してください。

「Syntax OK」と表示された場合、問題がないことが確認できました。

TCP/80ポートの通信を許可します。

ufw allow 'WWW'
ufw reload

apache2サービスを再起動します。

systemctl restart apache2.service

ブラウザでhttp://[IPアドレス]/にアクセスしてみて、画面が表示されれば、OKです!