忘備録

日々の調べ物をまとめる。アウトプットする。基本自分用。

【CentOS6】Apache2.2インストール&設定

環境

インストール

Apache本体の他に以下パッケージもインストールしておく

  • httpd-devel: apxsなどの開発用ツールが含まれるパッケージ
  • httpd-tools: httpd の監視、統計、ログ分析、DoS の検出、および防衛のためのツールが含まれるパッケージ
  • httpd-manual: httpdのドキュメントページを含むパッケージ
$ yum -y install httpd httpd-devel httpd-tools  httpd-manual
読み込んだプラグイン:fastestmirror
インストール処理の設定をしています
Loading mirror speeds from cached hostfile
 * base: ftp.iij.ad.jp
 * epel: ftp.iij.ad.jp
 * extras: ftp.iij.ad.jp
 * rpmforge: ftp.kddilabs.jp
 * updates: ftp.iij.ad.jp
依存性の解決をしています
--> トランザクションの確認を実行しています。
---> Package httpd.x86_64 0:2.2.15-47.el6.centos.3 will be インストール
--> 依存性の処理をしています: apr-util-ldap のパッケージ: httpd-2.2.15-47.el6.centos.3.x86_64
--> 依存性の処理をしています: /etc/mime.types のパッケージ: httpd-2.2.15-47.el6.centos.3.x86_64
--> 依存性の処理をしています: libaprutil-1.so.0()(64bit) のパッケージ: httpd-2.2.15-47.el6.centos.3.x86_64
--> 依存性の処理をしています: libapr-1.so.0()(64bit) のパッケージ: httpd-2.2.15-47.el6.centos.3.x86_64
---> Package httpd-devel.x86_64 0:2.2.15-47.el6.centos.3 will be インストール
--> 依存性の処理をしています: apr-util-devel のパッケージ: httpd-devel-2.2.15-47.el6.centos.3.x86_64
--> 依存性の処理をしています: apr-devel のパッケージ: httpd-devel-2.2.15-47.el6.centos.3.x86_64
---> Package httpd-manual.noarch 0:2.2.15-47.el6.centos.3 will be インストール
---> Package httpd-tools.x86_64 0:2.2.15-47.el6.centos.3 will be インストール
--> トランザクションの確認を実行しています。
---> Package apr.x86_64 0:1.3.9-5.el6_2 will be インストール
---> Package apr-devel.x86_64 0:1.3.9-5.el6_2 will be インストール
---> Package apr-util.x86_64 0:1.3.9-3.el6_0.1 will be インストール
---> Package apr-util-devel.x86_64 0:1.3.9-3.el6_0.1 will be インストール
--> 依存性の処理をしています: openldap-devel のパッケージ: apr-util-devel-1.3.9-3.el6_0.1.x86_64
--> 依存性の処理をしています: expat-devel のパッケージ: apr-util-devel-1.3.9-3.el6_0.1.x86_64
--> 依存性の処理をしています: db4-devel のパッケージ: apr-util-devel-1.3.9-3.el6_0.1.x86_64
---> Package apr-util-ldap.x86_64 0:1.3.9-3.el6_0.1 will be インストール
---> Package mailcap.noarch 0:2.1.31-2.el6 will be インストール
--> トランザクションの確認を実行しています。
---> Package db4-devel.x86_64 0:4.7.25-20.el6_7 will be インストール
--> 依存性の処理をしています: db4-cxx = 4.7.25-20.el6_7 のパッケージ: db4-devel-4.7.25-20.el6_7.x86_64
--> 依存性の処理をしています: libdb_cxx-4.7.so()(64bit) のパッケージ: db4-devel-4.7.25-20.el6_7.x86_64
---> Package expat-devel.x86_64 0:2.0.1-11.el6_2 will be インストール
---> Package openldap-devel.x86_64 0:2.4.40-7.el6_7 will be インストール
--> 依存性の処理をしています: cyrus-sasl-devel >= 2.1 のパッケージ: openldap-devel-2.4.40-7.el6_7.x86_64
--> トランザクションの確認を実行しています。
---> Package cyrus-sasl-devel.x86_64 0:2.1.23-15.el6_6.2 will be インストール
---> Package db4-cxx.x86_64 0:4.7.25-20.el6_7 will be インストール
--> 依存性解決を終了しました。

依存性を解決しました

==============================================================================================================================================================================================
 パッケージ                                       アーキテクチャ                         バージョン                                             リポジトリー                             容量
==============================================================================================================================================================================================
インストールしています:
 httpd                                            x86_64                                 2.2.15-47.el6.centos.3                                 updates                                 831 k
 httpd-devel                                      x86_64                                 2.2.15-47.el6.centos.3                                 updates                                 155 k
 httpd-manual                                     noarch                                 2.2.15-47.el6.centos.3                                 updates                                 788 k
 httpd-tools                                      x86_64                                 2.2.15-47.el6.centos.3                                 updates                                  77 k
依存性関連でのインストールをします。:
 apr                                              x86_64                                 1.3.9-5.el6_2                                          base                                    123 k
 apr-devel                                        x86_64                                 1.3.9-5.el6_2                                          base                                    176 k
 apr-util                                         x86_64                                 1.3.9-3.el6_0.1                                        base                                     87 k
 apr-util-devel                                   x86_64                                 1.3.9-3.el6_0.1                                        base                                     69 k
 apr-util-ldap                                    x86_64                                 1.3.9-3.el6_0.1                                        base                                     15 k
 cyrus-sasl-devel                                 x86_64                                 2.1.23-15.el6_6.2                                      base                                    303 k
 db4-cxx                                          x86_64                                 4.7.25-20.el6_7                                        updates                                 588 k
 db4-devel                                        x86_64                                 4.7.25-20.el6_7                                        updates                                 6.6 M
 expat-devel                                      x86_64                                 2.0.1-11.el6_2                                         base                                    120 k
 mailcap                                          noarch                                 2.1.31-2.el6                                           base                                     27 k
 openldap-devel                                   x86_64                                 2.4.40-7.el6_7                                         updates                                 1.1 M

トランザクションの要約
==============================================================================================================================================================================================
インストール        15 パッケージ

総ダウンロード容量: 11 M
インストール済み容量: 41 M
パッケージをダウンロードしています:
(1/15): apr-1.3.9-5.el6_2.x86_64.rpm                                                                                                                                   | 123 kB     00:00     
(2/15): apr-devel-1.3.9-5.el6_2.x86_64.rpm                                                                                                                             | 176 kB     00:00     
(3/15): apr-util-1.3.9-3.el6_0.1.x86_64.rpm                                                                                                                            |  87 kB     00:00     
(4/15): apr-util-devel-1.3.9-3.el6_0.1.x86_64.rpm                                                                                                                      |  69 kB     00:00     
(5/15): apr-util-ldap-1.3.9-3.el6_0.1.x86_64.rpm                                                                                                                       |  15 kB     00:00     
(6/15): cyrus-sasl-devel-2.1.23-15.el6_6.2.x86_64.rpm                                                                                                                  | 303 kB     00:00     
(7/15): db4-cxx-4.7.25-20.el6_7.x86_64.rpm                                                                                                                             | 588 kB     00:00     
(8/15): db4-devel-4.7.25-20.el6_7.x86_64.rpm                                                                                                                           | 6.6 MB     00:02     
(9/15): expat-devel-2.0.1-11.el6_2.x86_64.rpm                                                                                                                          | 120 kB     00:00     
(10/15): httpd-2.2.15-47.el6.centos.3.x86_64.rpm                                                                                                                       | 831 kB     00:00     
(11/15): httpd-devel-2.2.15-47.el6.centos.3.x86_64.rpm                                                                                                                 | 155 kB     00:00     
(12/15): httpd-manual-2.2.15-47.el6.centos.3.noarch.rpm                                                                                                                | 788 kB     00:00     
(13/15): httpd-tools-2.2.15-47.el6.centos.3.x86_64.rpm                                                                                                                 |  77 kB     00:00     
(14/15): mailcap-2.1.31-2.el6.noarch.rpm                                                                                                                               |  27 kB     00:00     
(15/15): openldap-devel-2.4.40-7.el6_7.x86_64.rpm                                                                                                                      | 1.1 MB     00:00     
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
合計                                                                                                                                                          1.1 MB/s |  11 MB     00:10     
rpm_check_debug を実行しています
トランザクションのテストを実行しています
トランザクションのテストを成功しました
トランザクションを実行しています
  インストールしています  : apr-1.3.9-5.el6_2.x86_64                                                                                                                                     1/15 
  インストールしています  : apr-util-1.3.9-3.el6_0.1.x86_64                                                                                                                              2/15 
  インストールしています  : apr-devel-1.3.9-5.el6_2.x86_64                                                                                                                               3/15 
  インストールしています  : apr-util-ldap-1.3.9-3.el6_0.1.x86_64                                                                                                                         4/15 
  インストールしています  : httpd-tools-2.2.15-47.el6.centos.3.x86_64                                                                                                                    5/15 
  インストールしています  : mailcap-2.1.31-2.el6.noarch                                                                                                                                  6/15 
  インストールしています  : httpd-2.2.15-47.el6.centos.3.x86_64                                                                                                                          7/15 
  インストールしています  : db4-cxx-4.7.25-20.el6_7.x86_64                                                                                                                               8/15 
  インストールしています  : db4-devel-4.7.25-20.el6_7.x86_64                                                                                                                             9/15 
  インストールしています  : expat-devel-2.0.1-11.el6_2.x86_64                                                                                                                           10/15 
  インストールしています  : cyrus-sasl-devel-2.1.23-15.el6_6.2.x86_64                                                                                                                   11/15 
  インストールしています  : openldap-devel-2.4.40-7.el6_7.x86_64                                                                                                                        12/15 
  インストールしています  : apr-util-devel-1.3.9-3.el6_0.1.x86_64                                                                                                                       13/15 
  インストールしています  : httpd-devel-2.2.15-47.el6.centos.3.x86_64                                                                                                                   14/15 
  インストールしています  : httpd-manual-2.2.15-47.el6.centos.3.noarch                                                                                                                  15/15 
  Verifying               : httpd-devel-2.2.15-47.el6.centos.3.x86_64                                                                                                                    1/15 
  Verifying               : httpd-manual-2.2.15-47.el6.centos.3.noarch                                                                                                                   2/15 
  Verifying               : apr-devel-1.3.9-5.el6_2.x86_64                                                                                                                               3/15 
  Verifying               : cyrus-sasl-devel-2.1.23-15.el6_6.2.x86_64                                                                                                                    4/15 
  Verifying               : apr-util-ldap-1.3.9-3.el6_0.1.x86_64                                                                                                                         5/15 
  Verifying               : openldap-devel-2.4.40-7.el6_7.x86_64                                                                                                                         6/15 
  Verifying               : apr-1.3.9-5.el6_2.x86_64                                                                                                                                     7/15 
  Verifying               : apr-util-devel-1.3.9-3.el6_0.1.x86_64                                                                                                                        8/15 
  Verifying               : httpd-tools-2.2.15-47.el6.centos.3.x86_64                                                                                                                    9/15 
  Verifying               : db4-devel-4.7.25-20.el6_7.x86_64                                                                                                                            10/15 
  Verifying               : apr-util-1.3.9-3.el6_0.1.x86_64                                                                                                                             11/15 
  Verifying               : expat-devel-2.0.1-11.el6_2.x86_64                                                                                                                           12/15 
  Verifying               : db4-cxx-4.7.25-20.el6_7.x86_64                                                                                                                              13/15 
  Verifying               : mailcap-2.1.31-2.el6.noarch                                                                                                                                 14/15 
  Verifying               : httpd-2.2.15-47.el6.centos.3.x86_64                                                                                                                         15/15 

インストール:
  httpd.x86_64 0:2.2.15-47.el6.centos.3     httpd-devel.x86_64 0:2.2.15-47.el6.centos.3     httpd-manual.noarch 0:2.2.15-47.el6.centos.3     httpd-tools.x86_64 0:2.2.15-47.el6.centos.3    

依存性関連をインストールしました:
  apr.x86_64 0:1.3.9-5.el6_2                   apr-devel.x86_64 0:1.3.9-5.el6_2                  apr-util.x86_64 0:1.3.9-3.el6_0.1            apr-util-devel.x86_64 0:1.3.9-3.el6_0.1      
  apr-util-ldap.x86_64 0:1.3.9-3.el6_0.1       cyrus-sasl-devel.x86_64 0:2.1.23-15.el6_6.2       db4-cxx.x86_64 0:4.7.25-20.el6_7             db4-devel.x86_64 0:4.7.25-20.el6_7           
  expat-devel.x86_64 0:2.0.1-11.el6_2          mailcap.noarch 0:2.1.31-2.el6                     openldap-devel.x86_64 0:2.4.40-7.el6_7      

完了しました!

ファイアウォールの設定

http通信に利用される80番ポートを開けておきます。

$ iptables -A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT

iptables再起動

$ service iptables restart
iptables: チェインをポリシー ACCEPT へ設定中filter         [  OK  ]
iptables: ファイアウォールルールを消去中:                  [  OK  ]
iptables: モジュールを取り外し中:                          [  OK  ]
iptables: ファイアウォールルールを適用中:                  [  OK  ]

確認

$ iptables -L -n | grep 80
ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           state NEW tcp dpt:80

ポートマッピング

自分のサーバのネットワークはブロードバンドルータをかませているので、ポートマッピング(ポートフォワーディング)を設定します。

これは利用しているルータによって設定方法が異なると思うので割愛します。

Apacheの設定

バーチャルホスト

今回はネームベースでバーチャルホストを設定します。

まずはhttpd.confから

/etc/httpd/conf/httpd.conf

#NameVirtualHost *:80

コメントアウトされている箇所を

NameVirtualHost *:80

コメントアウトを外します。

次にバーチャルホストの設定。

バーチャルホストの設定はhttpd.confに書いてもいいのですが、数が増えてくると管理しにくいので、/etc/httpd/conf.d/vhosts/ 配下にバーチャルホストごとに設定ファイルを作る(1バーチャルホスト=1ファイル)という運用にします。

というわけで、今回はvhostsディレクトリ配下にバーチャルホスト毎の設定ファイルを置くことにします。

/etc/httpd/conf.d/httpd-vhosts.conf

#
# Include virtual host based config file.
#
Include conf.d/vhosts/*.conf # vhostsディレクトリ配下の〜.confファイルを読み込むようにする

バーチャルホストごとに設定ファイルを作成

/etc/httpd/conf.d/vhosts/example.conf

#
# example.co.jp
#
<VirtualHost *:80>
    ServerAdmin root@example
    DocumentRoot /home/apache/example/html/
    ServerName example.co.jp
    ServerAlias www.example.co.jp
    ErrorLog /home/apache/example/logs/error_log
    CustomLog /home/apache/example/logs/access_log common

    <Directory />
        Options Indexes
        AllowOverride None
        Order allow,deny
        Allow from all
    </Directory>
</VirtualHost>

アクセス

上記の設定の場合、http://example.co.jp もしくは http://www.example.co.jp でアクセスできるはずです。

f:id:mktktmr:20160301035915p:plain

上記のテストページが出ればOKです。

ちなみに、インストール時にhttpd-manualも入れておけば、http://example.co.jp/manual にアクセスした時に、Apacheのマニュアルに飛べるはずです。

f:id:mktktmr:20160309045526p:plain

自動起動設定

OSを再起動した時もサーバが自動的に立ち上がるように、設定を変更します。

# 確認
chkconfig | grep httpd
httpd           0:off  1:off  2:off  3:off  4:off  5:off  6:off # デフォルトだとoffなので...
# chkconfig を on に
$ chkconfig httpd on
# 確認
$ chkconfig | grep httpd
httpd           0:off  1:off  2:on   3:on   4:on   5:on   6:off

参考

Apache HTTP サーバ バージョン 2.2 ドキュメント - Apache HTTP サーバ バージョン 2.2

Apache 2.2.xでネームベースのVirtual Hostの設定 - Qiita

apache2.2 バーチャルホストの設定 前編

Apache httpd tools freecode日本語情報ページ - OSDN

Chapter 17. Web Servers