CentOS7にyumでHTTP/2.0対応Apache/OpenSSLを導入してみる

CentOS6のEOLまであと2年半、そろそろOS更改を意識しはじめる時期になってきました。次もCentOSの予定ですがCentOS7標準レポジトリではHTTP/2.0を利用できないのが気になります。CentOS8を待つのも手ですが、ここはひとつCentOS7にレポジトリを追加してHTTP/2.0に対応してみます。

CodeITレポジトリを導入する

HTTP/2.0にApacheで対応するためにはApache2.4.17以上とOpenSSL1.0.2以上の両方が必要ですが、CentOS7の標準レポジトリでは導入可能なApacheは2.4.6、OpenSSLはhttpd専用モジュールのmod_ssLとして1.0.2k (共にバックポート版)まで。対応バージョンを導入するには自分でコンパイルする手もありますが、今回は運用面を考えて今回は追加レポジトリを追加してHTTP/2.0に対応させます。

HTTP/2.0対応バージョンのApache2.4を導入可能なレポジトリとしてはIUSSCLがあります。最新版でなくても良ければこのどちらかで良いのですが将来的にTLS1.3にも対応させたいところ。最新版のOpenSSLにも対応しているレポジトリがないか探してみたところ、CodeITというレポジトリが見つかりました。

CentOS/RHEL repository – CodeIT Technical blog
https://codeit.guru/en_US/

このレポジトリはハリコフ(ウクライナ)のソフトウェア開発会社CodeIT社が自社の開発業務で利用するために構築したリポジトリとのこと。公開開始が2016年5月とのことで歴史が浅いため今後のサポートがどれくらい続くのか未知数ですが、自分で確認するためにも使ってみることにします。

CodeITレポジトリの導入
## 前提条件のepelレポジトリを導入
# yum install -y epel-release

## CodeITレポジトリを1行で導入
# cd /etc/yum.repos.d && wget https://repo.codeit.guru/codeit.el`rpm -q --qf "%{VERSION}" $(rpm -q --whatprovides redhat-release)`.repo
Apache2.4インストール
# yum install httpd
インストール:
  httpd.x86_64 0:2.4.33-3.codeit.el7                                                                                            

依存性関連をインストールしました:
  apr.x86_64 0:1.5.2-1.el7.codeit            apr-util.x86_64 0:1.5.2-6.el7      httpd-filesystem.noarch 0:2.4.33-3.codeit.el7  
  httpd-tools.x86_64 0:2.4.33-3.codeit.el7   libnghttp2.x86_64 0:1.31.1-1.el7   mailcap.noarch 0:2.1.41-2.el7                  
  mod_http2.x86_64 0:1.10.12-1.codeit

完了しました!

httpdのインストールだけではmod_sslが導入されないようなので追加インストールします。

# yum install mod_ssl
インストール:
  mod_ssl.x86_64 1:2.4.33-3.codeit.el7                                                                                          

完了しました!

次に導入したmod_sslが本当にOpenSSL1.1.0hに対応しているのか確かめてみます。

# strings /usr/lib64/httpd/modules/mod_ssl.so | grep -E 'OpenSSL\s+1[.0-9a-z]+' | uniq
OpenSSL 1.1.0h  27 Mar 2018

ばっちりOpenSSL1.1.0hが入っていますね。そろそろhttpdを起動したいところですが、このままでは秘密鍵がないと怒られてしまうので作成して所定のディレクトリにコピーします。

# cd /etc/pki/tls/certs/
# make localhost.crt
# cd ../private/
# cp localhost.key localhost.key.orig

## 秘密鍵のパスフレーズを解除
# openssl rsa -in localhost.key.orig -out localhost.key
PHP7.0インストール

ついでにPHPも入れてしまってWordPressが起動できるか確認しようと思います。過去の記事を参考にまずはremiレポジトリをインストールし、PHP7.0を導入します。

# yum -y install http://rpms.famillecollet.com/enterprise/remi-release-7.rpm

# yum --enablerepo=remi-php70 install php php-bcmath php-cli php-common php-gd php-mbstring php-mcrypt php-mysqlnd php-pdo php-pear.noarch php-pecl-apc php-php-gettext.noarch php-process php-tcpdf.noarch php-tcpdf-dejavu-sans-fonts.noarch php-tidy php-xml php-opcache
Apache2.4起動 & 自動起動設定

ここまでくればようやくhttpdを起動できます。systemdなCentOS7なので以下のコマンドでhttpdを起動し、ついでに自動起動設定もしておきましょう。

# systemctl start httpd
# systemctl enable httpd
Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service.
mariadbインストール & 自動起動設定 & 初期設定

WordPressのためにはSQLデータベースも必要です。CentOS7からmysqlからmariadbに変わりましたが基本は同じ。

# yum install mariadb mariadb-server 

# systemctl start mariadb
# systemctl enable mariadb
Created symlink from /etc/systemd/system/multi-user.target.wants/mariadb.service to /usr/lib/systemd/system/mariadb.service.

本来はWordPress専用ユーザを作るべきなのですが、今回は動作すればよいのでrootにパスワードを設定しておしまい。

# mysql -u root
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 5.5.56-MariaDB MariaDB Server

Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]>update mysql.user set password=password('PASSWORD') where user = 'root';
Query OK, 4 rows affected (0.00 sec)
Rows matched: 4  Changed: 4  Warnings: 0

MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.00 sec)

WP用データベース名も適当に。

MariaDB [(none)]> create database wp_db;
Query OK, 1 row affected (0.00 sec)

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
| wp_db              |
+--------------------+
5 rows in set (0.00 sec)

MariaDB [(none)]>exit;
WordPressインストール

最後にWordPress本体をダウンロードし、DocumentRootに設置してwp-config.phpファイルを用意して準備完了です。

# cd /tmp
# wget http://ja.wordpress.org/wordpress-4.9.6-ja.tar.gz

# tar -zxvf wordpress-4.9.6-ja.tar.gz -C /var/www/html/

# cd /var/www/html
# chown -R apache:apache *

# cd /var/www/html/wordpress/
# cp wp-config-sample.php wp-config.php
# vi wp-config.php
HTTP/2.0アクセスを確認する

ブラウザでCentOS7サーバにアクセスしwp-config.phpで初期設定を行った後、WordPressにアクセスしてHTTPレスポンスを確認します。

Upgrade:h2と表示されているので、しっかりとHTTPS/2.0アクセス可能なことを確認できました。

サーバ側でもアクセスログを見てみると、”GET /***** HTTP/2.0″とあるのでHTTP/2.0なことを確認できました。

# tail /var/log/httpd/ssl_access_log

192.168.1.101 - - [10/Jun/2018:21:40:56 +0900] "GET /wordpress/ HTTP/2.0" 200 53255
192.168.1.101 - - [10/Jun/2018:21:40:56 +0900] "GET /wordpress/wp-content/themes/twentyseventeen/style.css?ver=4.9.6 HTTP/2.0" 200 83401
192.168.1.101 - - [10/Jun/2018:21:40:56 +0900] "GET /wordpress/wp-includes/js/jquery/jquery.js?ver=1.12.4 HTTP/2.0" 200 97184
192.168.1.101 - - [10/Jun/2018:21:40:56 +0900] "GET /wordpress/wp-includes/js/jquery/jquery-migrate.min.js?ver=1.4.1 HTTP/2.0" 200 10056
192.168.1.101 - - [10/Jun/2018:21:40:56 +0900] "GET /wordpress/wp-content/themes/twentyseventeen/assets/images/header.jpg HTTP/2.0" 200 114854
192.168.1.101 - - [10/Jun/2018:21:40:56 +0900] "GET /wordpress/wp-includes/js/wp-emoji-release.min.js?ver=4.9.6 HTTP/2.0" 200 11721
192.168.1.101 - - [10/Jun/2018:21:40:56 +0900] "GET /wordpress/wp-content/themes/twentyseventeen/assets/js/jquery.scrollTo.js?ver=2.1.2 HTTP/2.0" 200 5836
192.168.1.101 - - [10/Jun/2018:21:40:56 +0900] "GET /wordpress/wp-includes/js/wp-embed.min.js?ver=4.9.6 HTTP/2.0" 200 1398
192.168.1.101 - - [10/Jun/2018:21:40:56 +0900] "GET /wordpress/wp-content/themes/twentyseventeen/assets/js/skip-link-focus-fix.js?ver=1.0 HTTP/2.0" 200 683
192.168.1.101 - - [10/Jun/2018:21:40:56 +0900] "GET /wordpress/wp-content/themes/twentyseventeen/assets/js/global.js?ver=1.0 HTTP/2.0" 200 7682
192.168.1.101 - - [10/Jun/2018:21:40:56 +0900] "GET /favicon.ico HTTP/2.0" 404 209
あとはCodeIT次第

結構簡単にHTTP/2.0対応のApache2.4とOpenSSLを導入できました。ただ気になるのはCodeITレポジトリがどれくらいの期間サポートしてくれるのかどうか。これは自分で追っていこうと思います。