FreeBSD 12.2 RELEASE - apache24 - 初期設定

 クラウディア
1. 設定ファイルについて
2. extra/httpd-default.conf
3. httpd.conf
4. Apache の起動

1. 設定ファイルについて

 インストール後に、以下の4ディレクトリ、32コンフィグレーションファイルが出来ています。

/usr/local/etc/apache24/
|-- Includes									←	インクルードファイル用
|   `-- no-accf.conf
|-- envvars.d
|-- extra										←	ベース設定以外のもの
|   |-- httpd-autoindex.conf
|   |-- httpd-autoindex.conf.sample
|   |-- httpd-dav.conf
|   |-- httpd-dav.conf.sample
|   |-- httpd-default.conf
|   |-- httpd-default.conf.sample
|   |-- httpd-info.conf
|   |-- httpd-info.conf.sample
|   |-- httpd-languages.conf
|   |-- httpd-languages.conf.sample
|   |-- httpd-manual.conf
|   |-- httpd-manual.conf.sample
|   |-- httpd-mpm.conf
|   |-- httpd-mpm.conf.sample
|   |-- httpd-multilang-errordoc.conf
|   |-- httpd-multilang-errordoc.conf.sample
|   |-- httpd-ssl.conf
|   |-- httpd-ssl.conf.sample
|   |-- httpd-userdir.conf
|   |-- httpd-userdir.conf.sample
|   |-- httpd-vhosts.conf
|   |-- httpd-vhosts.conf.sample
|   |-- proxy-html.conf
|   `-- proxy-html.conf.sample
|-- httpd.conf									←	ベース設定
|-- httpd.conf.sample
|-- magic
|-- magic.sample
|-- mime.types
|-- mime.types.sample
`-- modules.d
    `-- README_modules.d

2. extra/httpd-default.conf

 デフォルトの設定を編集します。

vi /usr/local/etc/apache24/extra/httpd-default.conf
 セキュリティ対策のために以下のように設定を変更します。

#
ServerTokens ProductOnly
 デフォルトの設定は「Full」になっていますが、「ProductOnly」に変更します。  「ProductOnly」にすることで、クライアントに送信するヘッダの apache の情報を最小限にします。  バージョン等により、セキュリティホールがある場合でも、バージョン情報が送信されないので、クライアント側では判断できません。

#
ServerSignature Off
 デフォルトのままですが、あえて意味を記述しておきます。  「Off」を設定しておくことで、サーバエラー発生時にブラウザにサーバアドレス・ポート番号を表示しません。

#
HostnameLookups On
 デフォルトは「Off」です。  「On」にすることでウェブサーバのログにクライアント側のドメイン名を記録します。  「Off」にするとほんの少しアクセスが早くなるかもしれません。

3. httpd.conf

 ベースのコンフィグレーションファイルを編集します。

vi /usr/local/etc/apache24/httpd.conf

#LoadModule dav_module libexec/apache24/mod_dav.so
 上記のコメントを外します(行先頭の [#] を削除する)。  ここ鶏が先か卵が先か卵が先かわかりませんが、「subversion」を使う時「mod_dav_svn」が有効なのに、ここが無効のままだと、後で「configtest」を行った時に

httpd: Syntax error on line 155 of /usr/local/etc/apache24/httpd.conf: Cannot load libexec/apache24/mod_dav_svn.so into server: /usr/local/libexec/apache24/mod_dav_svn.so: Undefined symbol "dav_register_provider"
 と言われてしまいます。  「subversion」を使う予定があれば、今のうちにはずしておきます。  以下、内部にコメントしますが

ServerAdmin you@example.com		←	ウェブサーバ管理者のメールアドレスに変更。

#
# ServerName gives the name and port that the server uses to identify itself.
# This can often be determined automatically, but we recommend you specify
# it explicitly to prevent problems during startup.
#
# If your host doesn't have a registered DNS name, enter its IP address here.
#
ServerName www.example.com:80	←	サーバのドメイン名と待ち受けポート番号の定義。
									ポート番号は通常 [80:HTTP] か [443:HTTPS]

DocumentRoot "/usr/local/www/apache24/data"		←	ドキュメントルート。変更する場合は書き換えます。
<Directory "/usr/local/www/apache24/data">		←	ドキュメントルートの動作を定義。
    												ドキュメントルートを書き換えている場合は同様に変更。
    # Possible values for the Options directive are "None", "All",
    # or any combination of:
    #   Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
    #
    # Note that "MultiViews" must be named *explicitly* --- "Options All"
    # doesn't give it to you.
    #
    # The Options directive is both complicated and important.  Please see
    # http://httpd.apache.org/docs/2.4/mod/core.html#options
    # for more information.
    #
    Options Indexes FollowSymLinks

    #
    # AllowOverride controls what directives may be placed in .htaccess files.
    # It can be "All", "None", or any combination of the keywords:
    #   AllowOverride FileInfo AuthConfig Limit
    #
    AllowOverride None

    #
    # Controls who can get stuff from this server.
    #
    Require all granted						←	アクセス許可についての記述
</Directory>									この件については後述しますが、この設定はすべてを許可します。
 263行目  「Indexes」は、「index.html」がないときの動作を定義。  「Index.html」がないときにディレクトリ自体を表示させないときは、「-Indexes」と記述。  ディレクトリの内容を表示させる場合は、「+Indexes」と記述。  「FollowSymLinks」は、シンボリックリンクを有効にするか無効にするかの定義。  有効にする場合は、「+FollowSymLinks」と記述。  無効にする場合は、「-FollowSymLinks」と記述。  エラーログの出力先を変更する場合は、下記を編集。

ErrorLog /var/log/httpd-error.log
 アクセスログの出力先を変更する場合は、下記を編集。

<IfModule log_config_module>
    #
    # The following directives define some format nicknames for use with
    # a CustomLog directive (see below).
    #
    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>

    #
    # 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 "/var/log/httpd-access.log" common

    #
    # If you prefer a logfile with access, agent, and referer information
    # (Combined Logfile Format) you can use the following directive.
    #
    #CustomLog "/var/log/httpd-access.log" combined
</IfModule>
 321行目、「LogFormat」というのが記述されていますがこれがログの出力形式を定義しています。  331行目をコメントアウト、337行目のコメントをはずして、「combined」にしておくといろいろな情報をログにとれますので、この設定をおすすめします。  ログはほうっておくとどんどん大きくなりますので、「メンテナンス・トラブルシュート - ログ」を参照して、ログのローテーション設定を行います。  下記は、「CGI」のスクリプトファイルを置く場所を指定しています。  「http://www.ドメイン名/cgi-bin/スクリプトファイル名.cgi」で、スクリプトファイルを実行できます。  スクリプトファイルを動作させることは、セキュリティ上、危険が発生しますので、充分に注意して行ってください。

<Directory "/usr/local/www/apache24/cgi-bin">
    AllowOverride None
    Options None
    Require all granted
</Directory>
 前項で設定した、「/usr/local/etc/apache24/extra/httpd-default.conf」への設定を有効にするために、下記の先頭の「#」を削除して、「Include」を有効にします。

#Include etc/apache24/extra/httpd-default.conf

4. apache の起動

 「apache」の起動を有効化します。

service apache24 enable
 起動停止用のスクリプトは

/usr/local/etc/rc.d/apache24
 です。  設定ファイルのチェックを行うには

service apache24 configtest
 と入力してください。設定した、http.conf の内容が正しく記述されているかどうか、チェックしてくれます。  記述が正しければ、Syntax OK と出力されるはずです。  記述に誤りがあれば、エラーを出力しますが、誤っている行番号やその内容などを、出力してくれるので訂正しやすいと思います。例えば

<Directory /usr/local/www/apache24/data>
 の綴りを誤って

<Directry /usr/local/www/apache24/data>
 と記述したとします。すると

Syntax error on line 1111 of /usr/local/etc/apache24/httpd.conf:
Invalid command '<Directry', perhaps mis-spelled or defined by a module not i
ncluded in the server configuration
 という具合に、出力されます。「1111 行目の ''Directry' ってのはミススペルじゃないの?」ってことですね。  Syntax OK が出力されれば、

service apache24 start	← 起動
 と入力してください。apache が起動されるはずです。  停止や再起動は以下のように入力します。

service apache24 stop		←	停止
service apache24 restart	←	再起動
 ただし、2018年12月20日現在、graceful というオプションもあります。  restart では、運用中のサーバでセッションが生きている場合、そのセッションを切断してしまいます。

service apache24 graceful	←	再起動
 にすると、現在のセッションは前の設定を保持したまま、接続が維持され、切断中のプロセスのみ新規の設定で起動することになりますので、運用中のサーバは graceful オプションを使用すべきです。  起動スクリプトは特に設定を変更しない限りそのままにしておいてかまいません。  システムが正常に起動すれば、「apache」も同時に起動することになります。  クライアントから、サーバにアクセスして
「FreeBSD 12.2 RELEASE」-「apache24」「Its work!」

 こういう表示が出るようであれば、初期設定はうまくいっていることになります。

ハイスピードプランTSUKUMOネットショップ【usus ウズウズ】メンズミレットマイニングベース