albatrosary's blog

UI/UXとエンタープライズシステム

AWS への Apache2.4 インストール と ssl設定

AWS(EC2) へ Apache をインストールする手順を記載します。https での通信を前提としていますので、http アクセスがあった場合には https ヘリダイレクトさせます。

全体的な流れは次の通りです。

 

1. EC2 でインスタンス生成

2. gcc* のインストール

3. apr,apr-util,pcre のインストール

4. ssl 関連モジュールのインストール

5. httpd(Apache2.4.3) のインストール

6. ssl証明書の発行と設定

7. Apacheの設定

8. Apacheの起動と停止

9. 起動確認

Appendix:ダイジェスト認証

 

EC2 で インスタンス生成

「 Launch Instance」から「Classic Wizard」を選択し「Amazon Linux AMI 64bit」にてインスタンスを生成します。以降の設定はそれぞれのルールに従って行ってください。

gcc* のインストール

Apache等をビルドする際、gcc関連でエラーが発生しますのであらかじめインストールしておきます。

$ sudo su - root
# yum install gcc*
# exit

apr,apr-util,pcre のインストール

Apache Portable Runtimeとそのユーティリティをインストールします。参考までに wgetで取得したファイルですが amazon を利用するときには基本 ec2-user 直下にダウンロードしています。

$ wget http://ftp.kddilabs.jp/infosystems/apache/apr/apr-1.4.6.tar.gz
$ tar xozf apr-1.4.6.tar.gz
$ cd apr-1.4.6
$ ./configure
$ make
$ sudo su - root
# cd /home/ec2-user/apr-1.4.6
# make install
# exit
$ cd ~
$ wget http://ftp.jaist.ac.jp/pub/apache/apr/apr-util-1.5.1.tar.gz
$ tar xozf apr-util-1.5.1.tar.gz
$ cd apr-util-1.5.1
$ ./configure --with-apr=/home/ec2-user/apr-1.4.6
$ make
$ sudo su - root
# cd /home/ec2-user/apr-util-1.5.1
# make install
# exit
$ cd ~
wget http://sourceforge.net/projects/pcre/files/pcre/8.31/pcre-8.31.tar.gz
$ tar xozf pcre-8.31.tar.gz
$ cd pcre-8.31
$ ./configure
$ make
$ sudo su - root
# cd /home/ec2-user/pcre-8.31
# make install
# exit
$ cd ~ 

ssl 関連モジュールのインストール

$ sudo su - root
# yum -y install mod_ssl
# yum -y install openssl
# yum -y install openssl-devel
# exit

httpd のインストール

wget http://ftp.riken.jp/net/apache/httpd/httpd-2.4.3.tar.gz
$ tar xozf httpd-2.4.3.tar.gz
$ cd httpd-2.4.3
$ ./configure --enable-rewrite --enable-shared=yes --enable-ssl
$ make
$ sudo su - root
# cd /home/ec2-user/httpd-2.4.3
# make install
# exit
$ cd ~

ssl証明書の発行と設定

インストールが終わったら証明書を発行します。ただし、今回はいわゆるオレオレ証明書です。厳密には第三者機関への登録が必要となります。

1. 秘密鍵の生成

2. CSRファイルの作成

3. CSRへの署名(CRTファイルの作成)

を行います。/etc/httpd/conf に

 ssl.crt

 ssl.key

というディレクトリを作って秘密鍵等々を格納します。

秘密鍵の生成
$ sudo su - root
# cd /etc/httpd/conf
# mkdir ssl.crt
# mkdir ssl.key
# cd ssl.key
# openssl genrsa -des3 -out server.key 1024
Generating RSA private key, 1024 bit long modulus
.....................................................++++++
...........++++++
e is 65537 (0x10001)
Enter pass phrase for server.key:秘密鍵のパスワード設定
Verifying - Enter pass phrase for server.key:パスワードの確認
# chmod 400 server.key
CSRファイルの作成

続いて、csrファイルの作成です 

# openssl req -new -key server.key -out server.csr
Enter pass phrase for server.key:秘密鍵作成時に入力したパスワード
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [GB]:JP 国コード
State or Province Name (full name) [Berkshire]:Tokyo 都道府県名
Locality Name (eg, city) [Newbury]:Chiyoda-ku 市区町村名
Organization Name (eg, company) [My Company Ltd]:hogehoge, inc. 企業名・組織名
Organizational Unit Name (eg, section) : <--部署名を入力(省略可)
Common Name (eg, your name or your server's hostname) :www.hogehoge.jp FQDNホスト名
Email Address :hogehoge@gmail.com <--担当者のemail

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password : パスワードを入力(省略)
An optional company name []: 確認
# chmod 400 server.csr
# ls -l
server.csr
server.key
CSRへの署名(CRTファイルの作成)

今度は、サーバ認証(CRTファイル作成)です

# cd /etc/httpd/conf/ssl.key
# openssl x509 -in server.csr -out ../ssl.crt/server.crt -req -signkey server.key -days 365
Signature ok
subject=/C=JP/ST=Tokyo/L=Suginami/O=server-memo Corp/CN=www.server-memo.net
Getting Private key
Enter pass phrase for server.key:秘密鍵作成時に入力したパスワード
# cd /etc/httpd/conf/ssl.crt
# chmod 400 server.crt

Apacheの設定

鍵が一通りできたのでApacheの設定をします。設定するファイルは

/usr/local/apache2/conf/httpd.conf

/usr/local/apache2/conf/extra/httpd-ssl.conf

です。

# cd /usr/local/apache2/conf
# cp httpd.conf httpd.conf.org
# vi httpd.conf

httpd.conf はsslモジュールを読み込ませるのと httpd-ssl.conf をインポートします。また、http でのリクエストに対して https ヘリダイレクトさせます。

LoadModule socache_shmcb_module modules/mod_socache_shmcb.so
LoadModule ssl_module modules/mod_ssl.so
LoadModule rewrite_module modules/mod_rewrite.so

を有効にします。参考として Apache2.4 で RewriteLog と RewriteLogLevel はなくなっています。

<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteCond %{SERVER_PORT} !^443$
  RewriteRule ^/(.*)?$ https://%{HTTP_HOST}/$1 [L,R]
</IfModule>
Include conf/extra/httpd-ssl.conf

を追加します。ここまでが httpd.conf の設定内容です。httpd-ssl.conf には鍵情報を登録します。

# cd extra
# cp httpd-ssl.conf httpd-ssl.conf.org
# vi httpd-ssl.conf
SSLCertificateFile /etc/httpd/conf/ssl.crt/server.crt
SSLCertificateKeyFile /etc/httpd/conf/ssl.key/server.key

httpd.conf と httpd-ssl.conf にあるサーバ名等も変更してください。

ServerName www.hogehoge.jp:443
ServerAdmin hogehoge@gmail.com

Apacheの起動と停止

起動時にパスワードを聞いてきますので鍵を作った時のパスワード入力してください。

$ sudo su - root
# /usr/local/apache2/bin/apachectl start
Apache/2.4.3 mod_ssl (Pass Phrase Dialog) Some of your private key files are encrypted for security reasons. In order to read them you have to provide the pass phrases. Server www.caplab.jp:443 (RSA) Enter pass phrase:秘密鍵作成時に入力したパスワード OK: Pass Phrase Dialog successful. # # /usr/local/apache2/bin/apachectl stop # exit

起動確認

ブラウザで http://hogehoge.jp/ と叩くと https://hogehoge.jp/ にリダイレクトされます。

Appendix:ダイジェスト認証

必要に応じてダイジェスト認証を利用します。サイト全体に対してダイジェスト認証を行うのはまれですが、今回はサイト全体に認証を行います。

ダイジェスト認証用のパスワードファイルを格納するディレクトリを作成します。

$ sudo su - root
# cd /etc/httpd/conf/
# mkdir digest.auth

htdigest コマンドを使用しパスワードファイルを作成します。パスワードを聞いてきますので入力してください。

# htdigest -c /etc/httpd/conf/digest.auth/.htdigest "Digest Auth" management

次に Apache の設定を行います。 httpd.conf を編集します。モジュール auth_digest_module を追加します。

LoadModule auth_digest_module modules/mod_auth_digest.so

ダイジェスト認証を行うディレクトリに対して下記設定を行います。

<Location />
  AuthType Digest
  AuthName "Digest Auth"
  AuthUserFile /etc/httpd/conf/digest.auth/.htdigest
  Require valid-user
</Location>