目次
CentOS 7 + Apache 2.4 + 無料SSL で、
HTTP/2 の導入方法を細かく丁寧にご紹介します!
HTTP/2、体感でも速くなっているのがわかります。
※Apacheは、2.4.17以降から HTTP/2 に対応しています。
1、開発ツールの準備
CentOS7.1を「最小限のインストール」で導入されている場合は、
入れといてください。
1 2 |
yum -y groupinstall base yum -y groupinstall development |
一応、yumのアップグレードも
1 |
yum -y upgrade |
2、http と https の許可設定
まず、現在の設定の確認をしてみます。
1 |
firewall-cmd --list-all |
1 2 3 4 5 6 7 |
------------------------------ public (default, active) interfaces: enp0s3 enp0s8 sources: services: dhcpv6-client ssh (略) ------------------------------ |
ssh のみが許可されている状態です。
続いて、http と https を許可設定します。
1 2 |
firewall-cmd --add-service=http --zone=public --permanent firewall-cmd --add-service=https --zone=public --permanent |
設定を読込みなおします。
1 |
firewall-cmd --reload |
再度、設定を確認します。
1 |
firewall-cmd --list-services --zone=public |
1 2 3 |
---(下記の表示があればOK)--------------------------- dhcpv6-client http https ssh -------------------------------------------------------------- |
3、リポジトリの追加
・RPMForge
1 2 |
rpm -ivh http://pkgs.repoforge.org/rpmforge-release/rpmforge-release-0.5.3-1.el7.rf.x86_64.rpm rpm --import http://dag.wieers.com/rpm/packages/RPM-GPG-KEY.dag.txt |
・EPEL
1 2 |
rpm -ivh http://dl.fedoraproject.org/pub/epel/7/x86_64/e/epel-release-7-5.noarch.rpm rpm --import http://dl.fedoraproject.org/pub/epel/RPM-GPG-KEY-EPEL-7 |
・remi
1 2 |
rpm -ivh http://rpms.famillecollet.com/enterprise/remi-release-7.rpm rpm --import http://rpms.famillecollet.com/RPM-GPG-KEY-remi |
一部、下のように変更しておきます。
1 |
vim /etc/yum.repos.d/remi.repo |
1 2 3 4 5 6 7 8 9 |
--------------------------------- [remi] name=Les RPM de remi pour Enterprise Linux 7 - $basearch #baseurl=http://rpms.famillecollet.com/enterprise/7/remi/$basearch/ mirrorlist=http://rpms.famillecollet.com/enterprise/7/remi/mirror enabled=0 ↓ enabled=1 ------------------------------ |
4、HTTP/2のために必要なモジュールのインストール
①OpenSSL 1.0.2 のインストール
・OpenSSLのコンパイルに zlib が必要なので、インストールします。
1 |
yum -y install zlib-devel |
・ついでにCコンパイラもインストール
1 2 |
yum -y install gcc yum -y install gcc-c++ |
・OpenSSL本体のインストール
1 2 3 4 5 6 7 8 |
cd /usr/local/src/ wget https://www.openssl.org/source/openssl-1.0.2h.tar.gz tar xvzf openssl-1.0.2h.tar.gz cd openssl-1.0.2h/ ./config --prefix=/usr/local/openssl-1.0.2h shared zlib make make test make install |
・OpenSSL1.0.2h のライブラリにパスを通します。
1 2 |
echo /usr/local/openssl-1.0.2h/lib > /etc/ld.so.conf.d/openssl102h.conf ldconfig |
②Nghttp2 のインストール
・Nghttp2 が必要とするライブラリをインストール
1 |
yum -y install libev-devel |
・Nghttp2 本体のダウンロード
1 2 |
cd /usr/local/src/ wget https://github.com/tatsuhiro-t/nghttp2/releases/download/v1.10.0/nghttp2-1.10.0.tar.gz |
・Nghttp2 のインストール
環境変数 OPENSSL_CFLAGS と OPENSSL_LIBS に先ほどインストールした、
OpenSSL1.0.2h のディレクトリパスを指定してコンパイルします。
1 2 3 4 5 6 |
tar xvzf nghttp2-1.10.0.tar.gz cd nghttp2-1.10.0/ env OPENSSL_CFLAGS="-I/usr/local/openssl-1.0.2h/include" OPENSSL_LIBS="-L/usr/local/openssl-1.0.2h/lib -lssl -lcrypto" ./configure make make test make install |
/usr/local/lib 配下に「libnghttp2」がインストールされるので、
こちらもライブラリのパスに追加しておきます。
1 2 |
echo /usr/local/lib > /etc/ld.so.conf.d/usr-local-lib.conf ldconfig |
5、Apache 2.4.20のインストール
①ARP 、 ARP-util 、PCREライブラリのインストール
Apache 2.4系を、ソースコードからインストールする場合は、
ARP と ARP-util が必要になるので、インストールします。
ARP
1 2 3 4 5 6 7 |
cd /usr/local/src/ wget http://ftp.jaist.ac.jp/pub/apache/apr/apr-1.5.2.tar.gz tar xvzf apr-1.5.2.tar.gz cd apr-1.5.2/ ./configure make make install |
ARP-util
1 2 3 4 5 6 7 |
cd /usr/local/src/ wget http://ftp.jaist.ac.jp/pub/apache/apr/apr-util-1.5.4.tar.gz tar xvzf apr-util-1.5.4.tar.gz cd apr-util-1.5.4/ ./configure --with-apr=/usr/local/apr make make install |
また、PCREライブラリも必要になるので、インストールします。
1 |
yum -y install pcre-devel |
②Apache 2.4.20 本体のインストール
Apache 2.4.20 ソースコードのダウンロード
1 2 |
cd /usr/local/src/ wget http://ftp.jaist.ac.jp/pub/apache/httpd/httpd-2.4.20.tar.gz |
ダウンロードしたソースコードを解凍して、ディレクトリを移動します。
1 2 |
tar xvzf httpd-2.4.20.tar.gz cd httpd-2.4.20/ |
HTTP/2 を使うために必要なモジュール mod_http2 と mod_ssl を有効にし、
–with-ssl オプションで、OpenSSL1.0.2h のディレクトリパスを指定してインストールします。
1 2 3 4 5 6 7 8 9 10 11 |
./configure \ --enable-http2 \ --enable-ssl \ --with-ssl=/usr/local/openssl-1.0.2h \ --with-apr=/usr/local/apr \ --with-apr-util=/usr/local/apr/bin/apu-1-config \ --enable-so \ --enable-mpms-shared=all make make install |
以上で Apache 2.4.20 が /usr/local/apache2/ 以下にインストールされました。
③シンボリックリンクを作成する
1 2 |
ln -s /usr/local/apache2/bin/apxs /usr/sbin ln -s /usr/local/apache2/bin/httpd /usr/sbin |
ついでに、設定ファイルも作成
1 2 |
mkdir -p /etc/httpd/conf ln -s /usr/local/apache2/conf/httpd.conf /etc/httpd/conf/httpd.conf |
④自動起動の設定
1 |
\cp -f /usr/local/src/httpd-2.4.20/build/rpm/httpd.init /etc/rc.d/init.d/httpd |
httpdファイルを以下のように変更します。
1 |
vim /etc/init.d/httpd |
1 2 3 4 5 6 7 8 9 |
--------------------------------- httpd=${HTTPD-/usr/sbin/httpd} ↓ httpd=${HTTPD-/usr/local/apache2/bin/httpd} pidfile=${PIDFILE-/var/log/httpd/${prog}.pid} //versionによってちょっと違うけど下に変更 ↓ pidfile=${PIDFILE-/var/run/${prog}.pid} --------------------------------- |
1 2 3 4 |
systemctl daemon-reload chkconfig --add httpd chkconfig httpd on |
6、httpd.confファイルの設定
1 |
vim /etc/httpd/conf/httpd.conf |
・以下のモジュールを有効にします。
1 2 3 4 5 6 7 8 9 |
------------------------------------------------------------------ #LoadModule socache_shmcb_module modules/mod_socache_shmcb.so #LoadModule http2_module modules/mod_http2.so #LoadModule ssl_module modules/mod_ssl.so ↓ LoadModule socache_shmcb_module modules/mod_socache_shmcb.so LoadModule http2_module modules/mod_http2.so LoadModule ssl_module modules/mod_ssl.so ------------------------------------------------------------------ |
・以下をhttpd.confの行頭に追加
1 2 3 4 5 6 7 8 9 10 |
--------------------------------------- ServerTokens Prod ServerSignature Off HostnameLookups Off Timeout 120 KeepAlive On MaxKeepAliveRequests 100 KeepAliveTimeout 15 --------------------------------------- |
ついでに、起動時にアラートが出るので、
ServerName のコメントも外しておきます。
1 2 3 4 5 |
--------------------------------------- #ServerName www.example.com:80 ↓ ServerName www.example.com:80 --------------------------------------- |
・設定ファイルのインクルード
1 2 3 4 5 6 7 8 9 |
--------------------------------------- #Include conf/extra/httpd-vhosts.conf ↓ Include conf/extra/httpd-vhosts.conf #Include conf/extra/httpd-mpm.conf ↓ Include conf/extra/httpd-mpm.conf --------------------------------------- |
・初期のドキュメントルートのコメントアウト
以下の箇所をコメントアウトします。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
------------------------------------------------------------------------------ #DocumentRoot "/usr/local/apache2/htdocs" #<Directory "/usr/local/apache2/htdocs"> # # 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> ------------------------------------------------------------------------------ |
・バーチャルホストの設定
1 |
vim /usr/local/apache2/conf/extra/httpd-vhosts.conf |
まずは、デフォルトのバーチャルホストの記述をコメントアウトします。
その後、
YourDomainNameを取得したドメイン
YourDirectoryNameをドキュメントルートのディレクトリ名
に置き換えて以下を追加
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
------------------------------------------------------------------------------ <VirtualHost *:80> ServerName YourDomainName:80 DocumentRoot "/var/www/vhosts/YourDirectoryName/" <Directory "/var/www/vhosts/YourDirectoryName/"> Options +Indexes +Includes +FollowSymLinks AllowOverride All Require all granted </Directory> SetEnvIf Request_URI "\.(gif|jpg|png|css|js)$" nolog ErrorLog /var/log/error_log CustomLog /var/log/access_log combined env=!nolog </VirtualHost> ------------------------------------------------------------------------------ |
ドキュメントルートも一緒に作成しておきます。
1 2 |
mkdir -p /var/www/vhosts/YourDirectoryName/ echo "<h1>This is a test.</h1>" > /var/www/vhosts/YourDirectoryName/index.html |
・httpd-mpm.confの設定
1 |
vim /usr/local/apache2/conf/extra/httpd-mpm.conf |
以下のように変更
1 2 3 4 5 6 7 8 9 |
----------------------------------------------- <IfModule !mpm_netware_module> PidFile "logs/httpd.pid" </IfModule> ↓ <IfModule !mpm_netware_module> PidFile "/var/run/httpd.pid" </IfModule> ----------------------------------------------- |
・httpdの再起動
シンタックスエラーチェック
1 |
httpd -t |
エラーがなければ再起動
1 |
systemctl restart httpd |
ブラウザでドメインにアクセスして
This is a test.
が表示されればオッケー!
7、SSL(無料で使えるLet’s Encrypt 証明書)の導入
・まずは、gitクライアントのインストール
1 |
yum install git |
・Lets Encrypt クライアントのインストール
1 2 |
cd /usr/local/ git clone https://github.com/letsencrypt/letsencrypt |
・Lets Encrypt クライアントが依存するパッケージのインストール
1 2 |
cd letsencrypt/ ./letsencrypt-auto --help |
・証明書取得コマンドの実行
YourDirectoryNameの部分をドキュメントルートに置き換えて
以下を実行します。
1 2 3 4 |
./letsencrypt-auto certonly --webroot \ -w /var/www/vhosts/YourDirectoryName/ -d YourDirectoryName \ -m yourmailaddress@xxx.co.jp \ --agree-tos |
・SSLの設定ファイルのインクルード
1 |
vim /etc/httpd/conf/httpd.conf |
1 2 3 4 5 |
----------------------------------------------- #Include conf/extra/httpd-ssl.conf ↓ Include conf/extra/httpd-ssl.conf ----------------------------------------------- |
・httpd-ssl.confの設定
1 |
vim /usr/local/apache2/conf/extra/httpd-ssl.conf |
まず、SSLの暗号化方式を変更します。
1 2 3 4 5 |
----------------------------------------------- SSLCipherSuite HIGH:MEDIUM:!MD5:!RC4 ↓ SSLCipherSuite ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-SHA384:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-SHA256:DHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES256-SHA256:ECDHE-ECDSA-AES128-SHA256:DHE-DSS-AES128-GCM-SHA256:DHE-RSA-AES128-GCM-SHA256:DHE-DSS-AES128-SHA256:DHE-RSA-AES128-SHA256:ECDHE-RSA-AES256-SHA:DHE-RSA-AES256-SHA:ECDHE-RSA-AES128-SHA:DHE-DSS-AES256-SHA:DHE-RSA-AES128-SHA:!aNULL:!eNULL:!EXPORT:!DES:!RC4:!3DES:!MD5:!PSK ----------------------------------------------- |
脆弱性のある SSLプロトコルを無効にします。
1 2 3 4 5 |
----------------------------------------------- SSLProtocol all -SSLv3 ↓ SSLProtocol -All +TLSv1 +TLSv1.1 +TLSv1.2 ----------------------------------------------- |
HTTP/2 を有効にするため「Protocols h2 http/1.1」ディレクティブを追加します。
デフォルトのバーチャルホストの記述をコメントアウトして、
YourDomainName
YourDirectoryName
を置き換えて、
以下のようにバーチャルホストを追加します。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
----------------------------------------------- <VirtualHost *:443> <IfModule http2_module> ProtocolsHonorOrder On Protocols h2 http/1.1 </IfModule> ServerName YourDomainName:443 DocumentRoot "/var/www/vhosts/YourDirectoryName/" SSLEngine on SSLCertificateFile "/etc/letsencrypt/live/YourDomainName/cert.pem" SSLCertificateKeyFile "/etc/letsencrypt/live/YourDomainName/privkey.pem" SSLCertificateChainFile "/etc/letsencrypt/live/YourDomainName/chain.pem" <Directory "/var/www/vhosts/YourDirectoryName/"> Options +Indexes +Includes +FollowSymLinks AllowOverride All Require all granted </Directory> BrowserMatch "MSIE [2-5]" \ nokeepalive ssl-unclean-shutdown \ downgrade-1.0 force-response-1.0 SetEnvIf Request_URI "\.(gif|jpg|png|css|js)$" nolog ErrorLog /var/log/error_log CustomLog /var/log/access_log combined env=!nolog Header add Strict-Transport-Security "max-age=15552000" </VirtualHost> ----------------------------------------------- |
・Lets Encryptの証明書の自動更新設定
Lets Encryptの証明書は、有効期限が90日であるため、
自動更新の設定を行います。
毎月、1日のAM:5:00に更新されるように設定します。
1 |
crontab -u root -e |
以下を追加
1 2 3 4 5 |
---------------------------------------------------------------------------------------------- 00 05 01 * * /usr/local/letsencrypt/letsencrypt-auto renew --force-renew && systemctl reload httpd ---------------------------------------------------------------------------------------------- |
・httpd再起動
シンタックスエラーチェック
1 |
httpd -t |
エラーがなければ再起動
1 |
systemctl restart httpd |
・HTTP/2の確認
httpsでドメインにアクセスします。
ブラウザのデバッグツールで
HTTP/2.0通信になっていればオッケーです!
ちなみに下の画像では、FireFoxで確認しています。
最後に
Apache 2.4.20 に Lets Encryptで無料のSSLを使って、
HTTP/2 プロトコルを設定しました。
2016年6月2日時点では、yumで一気にインストールとはいかず、
ソースからコンパイルしないと導入できないのですが、
HTTP/2は、やっぱり速いです!
苦労するだけの価値があると思います!
次回は、2倍の高速化を達成したといわれる
PHP 7 の導入方法を紹介します!
デミ
最新記事 by デミ (全て見る)
- 【20分で完了】MacにDocker for Macのインストール - 2017/02/02
- 【2017年版】Web接客ツール9社を比較してみた - 2017/01/26
- 【昼休み中に完了!】Macで最新Ruby、Railsのインストールから画面表示まで - 2017/01/19