LoginSignup
18
26

More than 5 years have passed since last update.

ローカル開発環境でSSLを使う

Last updated at Posted at 2018-04-03

ローカル開発環境で本番と同じアドレスでテストしたいということで
構成を考えてみました。

◆前提

Windows10+virtualbox+vagrant+centos7+apache2.4の
構成で組んでいきます。

細かいツールのことやコマンドについては解説しておりません。
また今回はvirtualbox,vagrant,centosのセッティング済みということで書いており
Openssl,apacheのバーチャルホストに焦点を置いて書いています。
osやサーバのバージョンによってコマンドが違ったりすることがありますので
そのあたりは適宜検索して調整してみて下さい。
なおセキュリティに関しては配慮しておりませんのでご注意ください。

◆作業手順

1.必要なパッケージを検索します。

bash.sh
yum list installed | grep -e openssl  -e mod_ssl

見つからなかったらインストールします。

bash.sh
yum -y install openssl
yum -y install mod_ssl

2.SSL保管用のディレクトリを作成し移動します。

以下は例です。セキュリティに関わりますので適宜検討してください。
/etc/pki/tls/

3.秘密鍵を作成します

genrsa1.sh
   openssl genrsa -out apachessl.key 2048
   #apachessl.keyというファイルができていると思います。
genrsa2.sh
   #genrsa1と同じコマンドです。久々に実行したらエラーが出たのでgenrsa1を追加しています。
   #こちらがエラーになる場合はgenrsa1をお試しください。
   openssl genrsa 2048 > apachessl.key
   #apachessl.keyというファイルができていると思います。

4.証明書署名要求(CSR / Certificate Signing Request)の作成

一つずつ入力を求められる形で登録していきますが
必要最低限で大丈夫です。
以下は1234、6番目の項目を入力していますが
4と6だけでも大丈夫だと思います。
証明書が増えた時判別できるようにすることだけ注意が必要です。

req1.sh
openssl req -new -key apachessl.key -out apachessl.csr
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) [XX]:JP
State or Province Name (full name) []:Tokyo
Locality Name (eg, city) [Default City]:Shinjuku-ku
Organization Name (eg, company) [Default Company Ltd]:Company name
Organizational Unit Name (eg, section) []:
Common Name (eg, your name or your server's hostname) []:Company.com
Email Address []:

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:
req2.sh
#req1と同じコマンドです。久々に実行したらエラーが出たのでreq1を追加しています。
#こちらがエラーになる場合はreq1をお試しください。
openssl req -new -key apachessl.key > apachessl.csr

5.サーバ証明書の作成

req1.sh
sudo openssl x509 -req -days 3650 -signkey apachessl.key -in apachessl.csr -out apachessl.crt
req2.sh
#req1と同じコマンドです。久々に実行したらエラーが出たのでreq1を追加しています。
#こちらがエラーになる場合はreq1をお試しください。
openssl x509 -req -days 3650 -signkey apachessl.key < apachessl.csr > apachessl.crt

6.apacheのバーチャルホストを設定します

/etc/httpd/conf.d/example.conf
<VirtualHost *:443>
    ServerName www.example.com
    DocumentRoot /var/www/html/www.example.com/

    SSLEngine on
    SSLCertificateFile /etc/pki/tls/apachessl.crt
    SSLCertificateKeyFile /etc/pki/tls/apachessl.key

    ErrorLog logs/www.example.com-error_log
    CustomLog logs/www.example.com-access_log combined env=!no_log
</VirtualHost>

7.apacheを再起動します

bash.sh
systemctl restart httpd.service

8.windowsのhostsファイルを編集します。

 メモ帳を管理者で起動(右クリックで出てきます)して
 C:\Windows\System32\drivers\etcを開き
 すべてのファイルを表示とすると出てくるhostsというファイル最下部に
 192.168.33.10 www.example.comと追記します。
 192~はcentosにログインした状態で
ifconfigコマンドを実行するとinetという項目に出てきます。

9.www.example.comにアクセスします。

ここで警告が出ますが続行とすると表示されます。
またfirefoxではエラー内容→例外を追加でドメインを登録すると
警告を除外して表示することができます。
今回はテスト環境ということでfirefoxに例外を登録
という形で終了としていますが
オレオレCAというものを作ってサーバと
ブラウザに設定することで本番と同じようにすることも可能なようです。

18
26
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
18
26