ラベル AWS の投稿を表示しています。 すべての投稿を表示
ラベル AWS の投稿を表示しています。 すべての投稿を表示

2016/01/23

EC2でLet's Encrypt + Nginx を試してみる


Let's Encryptという誰でも無料で使えるSSL/TLS証明書発行サービスがPublic Betaを始めました。
https://letsencrypt.org/


前回、AWSのEC2インスタンスでNgnix+PHP7環境を作ったので、この環境にLet's Encryptの証明書を入れてみます。

Gitクライアントを入れて、letsencryptのライブラリを持ってくる。
 
sudo yum install git
git clone https://github.com/letsencrypt/letsencrypt

Nginxを一旦停止する
 
sudo service nginx stop

証明書を発行する
./letsencrypt-auto certonly --standalone -d [ドメイン名] --debug

※Nginxが既に動いていると怒られる。(stopする)
※ドメインの設定が完了してないと怒られる。(nslookup出来るか確認)

メールアドレス、ドメインを設定する。

Nginxの設定

 
    # HTTPS server
    #
    server {
        listen       443;
        server_name  dev.digitra.net;
        root         html;

        ssl                  on;
        #ssl_certificate      cert.pem;
        ssl_certificate      /etc/letsencrypt/live/[ドメイン名]/cert.pem;
        #ssl_certificate_key  cert.key;
        ssl_certificate_key  /etc/letsencrypt/live/[ドメイン名]/privkey.pem;

        ssl_session_timeout  5m;

        ssl_protocols  SSLv2 SSLv3 TLSv1;
        ssl_ciphers  HIGH:!aNULL:!MD5;
        ssl_prefer_server_ciphers   on;

        #charset koi8-r;

        location / {
                index index.html index.php;
        }

        # redirect server error pages to the static page /40x.html
        #
        error_page 404 /404.html;
        location = /40x.html {
        }

        # redirect server error pages to the static page /50x.html
        #
        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        location ~ \.php$ {
            root           html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /var/www/html$fastcgi_script_name;
            include        fastcgi_params;
        }
   }

Nginxを起動する

$ sudo service ngnix start

証明書を更新する

Let's Encryptの証明書の有効期限は90日となっている。(推奨は60日)
なので、定期的に証明書を更新する必要がある。

以下の様なスクリプトをcronに仕込んで自動更新するようにする。

#スクリプト
/home/ec2-user/letsencrypt/letsencrypt-auto certonly --webroot -w /var/www/html -d [ドメイン名] --renew-by-default --debug
service httpd restart

#cronの設定
00 05 01 * * /home/ec2-user/letsencrypt_renew.sh





EC2にNginxとPHP7で爆速環境を作る

ずっとApacheしか使ってなかったのですが、最近仕事でApacheが詰まりまくるので、前から気になっていたNginx(エンジンエックスって読むんだね)と、最近リリースされたPHP7が従来のPHPと比べて2倍ぐらい早いらしいので、Nginx+PHP7の環境をEC2(t2.micro)に構築してみる。

EC2のインスタンス作成とアップデート

AmazonLinuxのAIMでt2.microインスタンスを作成。
セキュリティグループでは、HTTPを追加する。

インスタンスが生成されたら、SSHでログインし、ミドルウェアパッケージの更新を行う。
$ sudo yum update -y


PHP7のインストール

yumのCentOS6用のリポジトリを追加し、PHP7をインストール。
$ sudo rpm -Uvh https://mirror.webtatic.com/yum/el6/latest.rpm

$ sudo yum install --enablerepo=webtatic-testing php70w php70w-devel php70w-fpm php70w-mysql php70w-mb

Nginxをインストール

$ sudo yum install nginx

/etc/nginx/nginx.conf の以下の項目を修正
        #ドキュメントルートを変更
        root         /var/www/html;

        #indexの設定を追加
        location / {
                index index.html index.php;
        }

        #コメントアウトを外す,ディレクトリを修正
        location ~ \.php$ {
            root           html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /var/www/html$fastcgi_script_name;
            include        fastcgi_params;
        }


サービスの設定

Nginxの場合、Apacheとは違い、Nginxを起動してもPHPが動くわけではない。
PHP-fpmのプロセスを起動する必要がある。

$ sudo service nginx start
$ sudo service php-fpm start

サーバが再起動した際に自動的にサービスが起動するようにする
$ sudo chkconfig nginx on
$ sudo chkconfig php-fpm on

phpinfoの内容を確認

vi /var/www/html/index.php
<?php
phpinfo();


本当に爆速なのか?

Apache Benchを使ってベンチーマークしてみる。

ベンチマーク用のプログラム(for文で1000回 echoしてみる)
<?php
for ( $i=0; $i < 1000; $i++ ) {
    echo "Hello world! \n";
}
どこか別のサーバからabコマンドで計測する
$ ab -n 10 -c 10 http://dev.digitra.net/bench.php
Requests per second:    688.47 [#/sec] (mean)
ちなみに同じt2.microのEC2でApache+PHP5.3で計測すると
Requests per second:    467.31 [#/sec] (mean)
おお、たしかに早い。もっとクライアント数を増やしてみるとおそらく更に差が顕著になるかと思われ。

参考にしたサイト

・Amazon EC2(Amazon Linux)にPHP7をyumでインストールし、Nginxで表示
http://owani.net/php/php7-nginx/285/