Apache最新版をCentOS 7 にインストール (httpd2.4)

公開日:2019年07月19日
最終更新日:2019年07月21日
レベル:★★★☆☆

CentOS 7 の標準リポジトリに含まれるApache(httpd)のバージョンは最新より古いものとなっている。また、Apacheの修正版がリリースされてもCentOS 7 標準リポジトリのパッケージに反映されるタイミングがやや遅い傾向がある。
Apacheの最新の機能を利用したい、最新の修正版を早く適用したいという要件がある場合、下記の手順により標準リポジトリではなく別のリポジトリからApacheを導入することができる。

今回利用するリポジトリは「IUS(Inline with Upstream Stable) Community Project」から提供されている。
IUSとは、公式サイトに書かれているように

IUS is a community project that provides RPM packages for newer versions of select software for Enterprise Linux distributions.
(IUSは、Enterprise Linuxディストリビューション用の特定のソフトウェアの新しいバージョン用のRPMパッケージを提供するコミュニティプロジェクトです。)

CentOSなどの RPMパッケージを利用するディストリビューション向けに最新のパッケージを提供してくれるコミュニティです。

事前確認

SELinuxを利用している場合、現在の設定を確認しておく

# getenforce 
Enforcing <--------- SELinux有効

SELinuxが「Enforcing」(有効)である場合、「/etc/selinux/targeted/contexts/files/file_contexts.local」ファイルの内容も確認しておく。
※httpdを削除したタイミングで本設定も削除されることがあるため。

# cat /etc/selinux/targeted/contexts/files/file_contexts.local
## This file is auto-generated by libsemanage <--------- 以下のような表示があれば記録しておく
# Do not edit directly.

/var/mysql(/.*)?    system_u:object_r:mysqld_db_t:s0
/var/www/hoge(/.*)?    system_u:object_r:httpd_sys_rw_content_t:s0
  • SELinuxの設定が入っていない場合は下記メッセージを表示する
# cat /etc/selinux/targeted/contexts/files/file_contexts.local
cat: /etc/selinux/targeted/contexts/files/file_contexts.local: そのようなファイルやディレクトリはありません

IUSリポジトリを追加

epelリポジトリの追加

IUSリポジトリを追加するためにはepelリポジトリが必要になるため、先にインストールしておく。

# yum list installed |grep epel <------------- mysqlが入っているなら既にインストール済みかも?
epel-release.noarch     7-11     @extras  <--- 出てこなければインストールする

# yum install epel-release.noarch
 :
インストール  1 パッケージ

総ダウンロード容量: 15 k
インストール容量: 24 k
Is this ok [y/d/N]: y <----------------------- yを入力(以降インストールの際には問い合わせがある)
 :
インストール:
  epel-release.noarch 0:7-11                                                    

完了しました!

IUSリポジトリの追加

# yum -y install https://centos7.iuscommunity.org/ius-release.rpm

IUSリポジトリをデフォルトで無効にする

IUSに入っているパッケージは新しいものなので安定性に問題がある場合も考えられる。よって通常はIUSリポジトリからのパッケージを使用しないようにしておき、必要時にインストールオプションをつけることで利用できるようにする。

# yum repolist all |grep ius
ius/x86_64                    IUS for Enterprise Linux 7 - x86_64   有効:    659 <---- デフォルト有効になっている
ius-archive/x86_64            IUS for Enterprise Linux 7 - Archive  無効
ius-archive-debuginfo/x86_64  IUS for Enterprise Linux 7 - Archive  無効
ius-archive-source            IUS for Enterprise Linux 7 - Archive  無効
ius-debuginfo/x86_64          IUS for Enterprise Linux 7 - x86_64 - 無効
ius-source                    IUS for Enterprise Linux 7 - Source   無効
ius-testing/x86_64            IUS for Enterprise Linux 7 - Testing  無効
ius-testing-debuginfo/x86_64  IUS for Enterprise Linux 7 - Testing  無効
ius-testing-source            IUS for Enterprise Linux 7 - Testing  無効

# vi /etc/yum.repos.d/ius.repo
------------------------------------------------------------------------------------
[ius]
name = IUS for Enterprise Linux 7 - $basearch
baseurl = https://repo.ius.io/7/$basearch/
enabled = 0 <----------------------------------------------- 1→0に変更
repo_gpgcheck = 0
gpgcheck = 1
gpgkey = file:///etc/pki/rpm-gpg/RPM-GPG-KEY-IUS-7
 :
------------------------------------------------------------------------------------

最新版Apache(httpd)をインストール

現在インストールされているhttpdを削除

CentOS 7 標準パッケージでインストールされたApache(httpd)が既にインストールされている場合、一旦削除する。

# yum list installed |grep httpd

あれば

# yum remove httpd httpd-tools

================================================================================
 Package          アーキテクチャー
                              バージョン                    リポジトリー   容量
================================================================================
削除中:
 httpd            x86_64      2.4.6-89.el7.centos           @updates      9.4 M
 httpd-tools      x86_64      2.4.6-89.el7.centos           @updates      169 k
依存性関連での削除をします:
 mod_ssl          x86_64      1:2.4.6-89.el7.centos         @updates      224 k

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

インストール容量: 9.7 M
上記の処理を行います。よろしいでしょうか? [y/N] y
  • phpも入っている場合は、phpも一旦アンインストールすること。
# rpm -qa |grep php
 php-mysqlnd-7.1.29-1.el7.remi.x86_64 <-------- インストールされているものをメモっておくこと
 php-mbstring-7.1.29-1.el7.remi.x86_64
  :
 php-gd-7.1.29-1.el7.remi.x86_64

# yum remove php* httpd httpd-tools

httpdのインストール

下記コマンドでhttpdをインストールする。

# yum install  --enablerepo=ius httpd24u httpd24u-mod_ssl
================================================================================
 Package                  アーキテクチャー
                                      バージョン                リポジトリー
                                                                           容量
================================================================================
インストール中:
 httpd24u                 x86_64      2.4.39-2.el7.ius          ius       1.4 M
 httpd24u-mod_ssl         x86_64      1:2.4.39-2.el7.ius        ius       115 k
依存性関連でのインストールをします:
 apr15u                   x86_64      1.5.2-2.ius.el7           ius       112 k
 apr15u-util              x86_64      1.5.4-3.ius.el7           ius        93 k
 brotli                   x86_64      1.0.7-5.el7               epel      318 k
 httpd24u-filesystem      noarch      2.4.39-2.el7.ius          ius        27 k
 httpd24u-tools           x86_64      2.4.39-2.el7.ius          ius        90 k
 libnghttp2               x86_64      1.31.1-1.el7              epel       66 k
 mailcap                  noarch      2.1.41-2.el7              base       31 k

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

総ダウンロード容量: 2.2 M
インストール容量: 6.8 M
Is this ok [y/d/N]: y
 :
合計                                               632 kB/s | 2.2 MB  00:03     
file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7 から鍵を取得中です。
Importing GPG key 0x352C64E5:
 Userid     : "Fedora EPEL (7) <epel@fedoraproject.org>"
 Fingerprint: 91e9 7d7c 4a5e 96f1 7f3e 888f 6a2f aea2 352c 64e5
 Package    : epel-release-7-11.noarch (@extras)
 From       : /etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
上記の処理を行います。よろしいでしょうか? [y/N]y
file:///etc/pki/rpm-gpg/RPM-GPG-KEY-IUS-7 から鍵を取得中です。
Importing GPG key 0x4B274DF2:
 Userid     : "IUS (7) <dev@ius.io>"
 Fingerprint: c958 7a09 a11f d706 4f0c a0f4 e558 0725 4b27 4df2
 Package    : ius-release-2-1.el7.ius.noarch (installed)
 From       : /etc/pki/rpm-gpg/RPM-GPG-KEY-IUS-7
上記の処理を行います。よろしいでしょうか? [y/N]y
  • 「--enablerepo=ius」オプションを忘れないように。
    これをつけないと次のようになる。
# yum install httpd24u httpd24u-mod_ssl
読み込んだプラグイン:fastestmirror
Loading mirror speeds from cached hostfile
 * base: ftp.iij.ad.jp
 * epel: ftp.riken.jp
 * extras: ftp.iij.ad.jp
 * updates: ftp.iij.ad.jp
パッケージ httpd24u は利用できません。
パッケージ httpd24u-mod_ssl は利用できません。
エラー: 何もしません

インストール状態確認

# yum list installed |grep httpd
httpd24u.x86_64                       2.4.39-2.el7.ius                 @ius     
httpd24u-filesystem.noarch            2.4.39-2.el7.ius                 @ius     
httpd24u-mod_ssl.x86_64               1:2.4.39-2.el7.ius               @ius     
httpd24u-tools.x86_64                 2.4.39-2.el7.ius                 @ius     

# which httpd
/usr/sbin/httpd

# httpd -v
Server version: Apache/2.4.39 (IUS)
Server built:   Jun 13 2019 17:55:56

httpdの設定

/etc/httpd/conf/httpd.conf」ファイルを次のように編集する。

# vi /etc/httpd/conf/httpd.conf
------------------------------------------------------------------------------------
 :
# 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 www.example.com:80 <-------------- ②
 :
ServerTokens Prod <-------------------------- ③
ServerSignature off <------------------------ ④
TraceEnable off <---------------------------- ⑤
 :
DocumentRoot "/var/www/html" <--------------- ⑥
 :
#
# Relax access to content within /var/www.
#
#>>> Commented by K'z Style >>>
#<Directory "/var/www"> <-------------------- ⑦
#    AllowOverride None
#    # Allow open access:
#    Require all granted
#</Directory>
 :
# Further relax access to the default document root:
<Directory "/var/www/html"> <---------------- ⑧
 :
    #Options Indexes FollowSymLinks <-------- ⑨
    Options None
 :
------------------------------------------------------------------------------------
  • ① サーバ管理者の連絡先を記載
  • ② サーバ名を記載(先頭#でコメントアウトされているのでコメントを外す)
  • ③ セキュリティ対策:Apacheのバージョン隠蔽 Part.1
  • ④ セキュリティ対策:Apacheのバージョン隠蔽 Part.2
  • ⑤ セキュリティ対策:
  • ⑥ ドキュメントルート(Webコンテンツを置く場所)を指定
  • ⑦ 特に必要なければ<Directory >〜</Directory>までをコメントアウトして無効にする
  • ⑧ ⑥のドキュメントルートの設定。⑥とディレクトリの場所を合わせること。
  • ⑨ セキュリティ対策:特に理由がなければコメントアウトし、「Options None」としておく。最低限「Indexes」は外す。

設定確認

# httpd -t
Syntax OK <--------------------- 設定に問題なければこれが返る

httpdの起動

下記コマンドでApache(httpdデーモン)を起動する。

# systemctl start httpd

# systemctl -t service
UNIT                  LOAD   ACTIVE SUB     DESCRIPTION
auditd.service        loaded active running Security Auditing Service
 :
httpd.service         loaded active running The Apache HTTP Server <---- 「running」であればOK

ただし、この状態でブラウザでアクセスしても接続できない。

CentOS 7のファイアウォールが閉じているため。
/etc/firewalld/zones/public.xml」を次のように編集する。

# vi /etc/firewalld/zones/public.xml
----------------------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<zone>
  <short>Public</short>
  <description>For use in public areas. You do not trust the other computers on networks to not harm your computer. Only selected incoming connections are accepted.</description>
  <service name="ssh"/>
  <service name="http"/> <------------------------ この行を追加
  <service name="https"/> <----------------------- httpsを有効にする場合はこの行も追加
  <service name="dhcpv6-client"/>
</zone>
----------------------------------------------------------------------

firewalldを再起動する

# systemctl restart firewalld

今度は接続できるはず。下記ページが表示されればOK。

自動起動の設定

このままではApache(httpd)は再起動しても自動的に起動しない。
次のコマンドでOS起動時にhttpdも自動起動するように設定する。

# systemctl enable httpd

CentOS 7 を再起動し、httpdが自動起動していることを確認する。

 

ー 以上 ー

 CMSを使ったホームページ制作
 VPS, AWS等クラウドシステム構築
等々のご依頼承っております

Contents