[Linux] CentOSにApache(httpd)をインストールし基本的な設定を行う

CentOSにyumを使ってApacheをインストールし、基本的な項目の設定を行います。
[環境] Apache:2.4.x

インストール

ターミナルから以下のコマンドを実行します。

# yum -y install httpd

自働でインストールが開始されます。

読み込んだプラグイン:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 * base: mirror.fairway.ne.jp
 * extras: mirror.fairway.ne.jp
 * updates: mirror.fairway.ne.jp
依存性の解決をしています
--> トランザクションの確認を実行しています。
---> パッケージ httpd.x86_64 0:2.4.6-45.el7.centos を インストール
--> 依存性の処理をしています: httpd-tools = 2.4.6-45.el7.centos のパッケージ: httpd-2.4.6-45.el7.centos.x86_64
--> 依存性の処理をしています: /etc/mime.types のパッケージ: httpd-2.4.6-45.el7.centos.x86_64
--> トランザクションの確認を実行しています。
---> パッケージ httpd-tools.x86_64 0:2.4.6-45.el7.centos を インストール
---> パッケージ mailcap.noarch 0:2.1.41-2.el7 を インストール
--> 依存性解決を終了しました。

依存性を解決しました

================================================================================
 Package            アーキテクチャー
                                  バージョン                  リポジトリー
                                                                           容量
================================================================================
インストール中:
 httpd              x86_64        2.4.6-45.el7.centos         base        2.7 M
依存性関連でのインストールをします:
 httpd-tools        x86_64        2.4.6-45.el7.centos         base         84 k
 mailcap            noarch        2.1.41-2.el7                base         31 k

トランザクションの要約
================================================================================
インストール  1 パッケージ (+2 個の依存関係のパッケージ)

総ダウンロード容量: 2.8 M
インストール容量: 9.6 M
Downloading packages:
(1/3): httpd-tools-2.4.6-45.el7.centos.x86_64.rpm          |  84 kB   00:00
(2/3): mailcap-2.1.41-2.el7.noarch.rpm                     |  31 kB   00:00
(3/3): httpd-2.4.6-45.el7.centos.x86_64.rpm                | 2.7 MB   00:02
--------------------------------------------------------------------------------
合計                                               1.3 MB/s | 2.8 MB  00:02
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  インストール中          : mailcap-2.1.41-2.el7.noarch                     1/3
  インストール中          : httpd-tools-2.4.6-45.el7.centos.x86_64          2/3
  インストール中          : httpd-2.4.6-45.el7.centos.x86_64                3/3
  検証中                  : httpd-tools-2.4.6-45.el7.centos.x86_64          1/3
  検証中                  : mailcap-2.1.41-2.el7.noarch                     2/3
  検証中                  : httpd-2.4.6-45.el7.centos.x86_64                3/3

インストール:
  httpd.x86_64 0:2.4.6-45.el7.centos

依存性関連をインストールしました:
  httpd-tools.x86_64 0:2.4.6-45.el7.centos     mailcap.noarch 0:2.1.41-2.el7

完了しました!

インストールが終了したので設定に移ります。

設定

設定ファイル「httpd.conf」を開く

  # vi /etc/httpd/conf/httpd.conf
オリジナルファイルの確認

httpd.confはとても長いですが、基本的な設定は以下の部分になります。
主な設定項目は「ServerAdmin」「ServerName」「Directroy」の3つです。

#
# ServerAdmin: Your address, where problems with the server should be
# e-mailed.  This address appears on some server-generated pages, such
# as error documents.  e.g. admin@your-domain.com
#
ServerAdmin root@localhost

#
# 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

#
# Deny access to the entirety of your server's filesystem. You must
# explicitly permit access to web content directories in other
# <Directory> blocks below.
#
<Directory />
    AllowOverride none
    Require all denied
</Directory>

#
# Note that from this point forward you must specifically allow
# particular features to be enabled - so if something's not working as
# you might expect, make sure that you have specifically enabled it
# below.
#

#
# DocumentRoot: The directory out of which you will serve your
# documents. By default, all requests are taken from this directory, but
# symbolic links and aliases may be used to point to other locations.
#
DocumentRoot "/var/www/html"

#
# Relax access to content within /var/www.
#
<Directory "/var/www">
    AllowOverride None
    # Allow open access:
    Require all granted
</Directory>

# Further relax access to the default document root:
<Directory "/var/www/html">
    #
    # 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:
    #   Options FileInfo AuthConfig Limit
    #
    AllowOverride None

    #
    # Controls who can get stuff from this server.
    #
    Require all granted
</Directory>
ServerAdminの設定

サーバー管理者のメールアドレスを設定します。

# ServerAdmin: Your address, where problems with the server should be
# e-mailed.  This address appears on some server-generated pages, such
# as error documents.  e.g. admin@your-domain.com
#
ServerAdmin root@localhost
ServerNameの設定

ServerNameを設定します。

# 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 192.168.56.101:80

ローカルでサーバーを構築した場合は「192.168.56.101」のようにIPアドレスを指定。
ドメインがある場合はIPアドレスの代わりにドメイン名を指定します。

Directoryの設定
# Deny access to the entirety of your server's filesystem. You must
# explicitly permit access to web content directories in other
# <Directory> blocks below.
#
<Directory />
    AllowOverride none
    Require all denied
</Directory>

#
# Note that from this point forward you must specifically allow
# particular features to be enabled - so if something's not working as
# you might expect, make sure that you have specifically enabled it
# below.
#

#
# DocumentRoot: The directory out of which you will serve your
# documents. By default, all requests are taken from this directory, but
# symbolic links and aliases may be used to point to other locations.
#
DocumentRoot "/var/www/html"

#
# Relax access to content within /var/www.
#
#<Directory "/var/www">
#    AllowOverride none
#    # Allow open access:
#    Require all granted
#</Directory>

# Further relax access to the default document root:
<Directory "/var/www/html">
    #
    # 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:
    #   Options FileInfo AuthConfig Limit
    #
    AllowOverride All

    #
    # Controls who can get stuff from this server.
    #
    Require all granted
</Directory>

5~8行目:設定がこのようになっているか確認。
27-31行目:DocumentRootを「/var/www/html」(デフォルト)にする場合、コメントアウト
45行目:ドキュメントルートのDirectory設定。「.htaccess」が使えるように「AllowOverride All」を設定



Author: webmaster