LoginSignup
0
1

More than 5 years have passed since last update.

GCEで、Nginx+Tomcatインストール後、最初に行うSSL設定

Last updated at Posted at 2018-11-28

手順

  1. Let's Encrypt で証明書を発行
  2. Tomcat のHTTPS通信用ポートを開ける
  3. Nginx の設定を変更
  4. Tomcat の設定を変更
  5. SSL安全性チェック
  6. SSL証明書の更新設定

Let's Encrypt で証明書を発行

sudo apt-get install certbot

Let's Encrypt は Python で動いており、実行時に文字コードエラーが出る場合があるので、確認・変更しておく。

python
>>> import sys
>>> sys.getdefaultencoding()
'ascii'

これを utf-8 に変える。

sudo vi /usr/lib/python2.7/sitecustomze.py

最後に2行を追加。

# install the apport exception handler if available
try:
import apport_python_hook
except ImportError:
pass
else:
apport_python_hook.install()

import sys
sys.setdefaultencoding('utf-8')

また、ドメイン所有者の認証のために TCP ポートの 80 番と 443 番に接続されるので、開放されてなければ開放しておく。

Nginx と Tomcat は一旦停止しておく。

sudo /etc/init.d/nginx stop
sudo /etc/init.d/tomcat stop

certbot を実行する。

certbot certonly --standalone -d example.jp

example.jp は自分のドメインに置き換える。

Tomcat のHTTPS通信用ポートを開ける

Nginx で443ポートを待ち受け、Tomcat で設定する58080ポートに繋ぐようにするため、GCEファイアーウォール設定で、58080ポートを開けておく。

  1. Google Cloud Platform のサイドメニュー→VPCネットワーク→ファイアウォールルール
  2. 上側にある「ファイアウォールルールを作成」をクリック
  3. 内容をそれぞれ設定し、作成をクリック
    • 名前:firewall-58080(任意)
    • ターゲット:指定されたターゲットタグ
    • ターゲットタグ:firewall-58080
    • ソースIDの範囲:0.0.0.0/0
    • プロトコルとポート:tcp:58080
  4. VMインスタンスの画面で、公開するインスタンスの名前をクリック
  5. 上側にある「編集」をクリック
  6. 中段付近にある「ネットワークタグ」に「firewall-58080」を追加
  7. 保存をクリック

Nginx の設定を変更

/etc/nginx/sites-enabled/tomcat.conf を作成。

tomcat.conf
# HTTP
server {
  client_max_body_size 20M;
  listen 80;
  server_name example.jp;

  root /var/www/tomcat;

  access_log /var/log/nginx/tomcat_access.log;
  error_log /var/log/nginx/tomcat_error.log;

  location / { 
    proxy_pass http://127.0.0.1:8080/;
    proxy_set_header X-Forwarded-Proto $scheme;
  }
}

# HTTPS
server {
  client_max_body_size 20M;
  listen 443 ssl;
  server_name example.jp;

  root /var/www/tomcat;

  ssl on;
  ssl_prefer_server_ciphers on;
  ssl_protocols     TLSv1 TLSv1.1 TLSv1.2;
  ssl_ciphers       'ECDH !aNULL !eNULL !SSLv2 !SSLv3';
  ssl_certificate   /etc/letsencrypt/live/example.jp/fullchain.pem;
  ssl_certificate_key   /etc/letsencrypt/live/example.jp/privkey.pem;
  ssl_trusted_certificate /etc/letsencrypt/live/example.jp/chain.pem;

  access_log /var/log/nginx/tomcat_access.log;
  error_log /var/log/nginx/tomcat_error.log;

  location / {
    proxy_pass http://127.0.0.1:58080/;
  }
}

example.jp(5カ所)は自分のドメインに変更する。

設定ファイルの文法チェック。

sudo nginx -t
> nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
> nginx: configuration file /etc/nginx/nginx.conf test is successful

問題があれば、設定ファイルを見直す。

Tomcat の設定を変更

/etc/tomcat8/server.xml を編集する。

server.xml
...中略
    <Connector port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               maxThreads="450"
               URIEncoding="UTF-8" />
    <Connector port="58080" protocol="HTTP/1.1"
               proxyPort="443" redirectPort="443"
               scheme="https"
               secure="true"
               connectionTimeout="20000"
               maxThreads="450"
               URIEncoding="UTF-8" />
...中略
     <Valve className="org.apache.catalina.valves.RemoteIpValve"
           protocolHeader="x-forwarded-proto"/>

Nginx、Tomcat を起動

sudo /etc/init.d/nginx start
sudo /etc/init.d/tomcat8 start

ブラウザで、https://example.jp(自分のドメイン)に接続できればOK。

SSL安全性チェック

SSL Server Test で自分のドメインを入力し、A判定以上ならOK。

SSL証明書の更新設定

/etc/cron.d/certbot を Nginx 用に編集する。

編集前
0 */12 * * * root test -x /usr/bin/certbot -a \! -d /run/systemd/system && perl -e 'sleep int(rand(3600))' && certbot -q renew
編集後
0 */12 * * * root certbot -q renew --pre-hook "systemctl stop nginx" --post-hook "systemctl start nginx"

certbot の更新作業前後で、Nginx を停止する設定に変更している。

0
1
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
0
1