LoginSignup
4
5

More than 5 years have passed since last update.

Amazon Linux にWEBサーバ(Nginx + Tomcat + Let's Encrypt)を構築する

Posted at

Amazon Linux に WEBサーバ (Nginx + Tomcat + Let's Encrypt) を構築してみました。

Amazon Linux のインスタンス作成

Amazon Linux AMI 2016.09.0 (HVM), SSD Volume Type - ami-0c11b26d
を使用してインスタンスを構築する。

  • セキュリティグループの設定でHttp/Httpsでの通信を可能にしておく。
  • Tomcatの起動確認用に8080も通信可能にしておく。
  • Elastic IPを割り当て、DNSの設定を行っておく。

Nginx のインストール

yumのアップデートをした上でインストールを行う。

sudo yum update -y
sudo yum install -y nginx

nginxを起動し、自動起動の設定を行う。

sudo service nginx start
sudo chkconfig nginx on

nginxのWelcome画面が開けることを確認する。

http://xxx.example.com

Java のインストール

jdk1.8を使用するためインストールする。

sudo yum install -y java-1.8.0-openjdk-devel

既に1.7がインストールされているため、alternatives でJavaのバージョンを切り替える。

sudo alternatives --config java

以下のような画面が表示されるので、2を選択する。

2 プログラムがあり 'java' を提供します。

  選択       コマンド
-----------------------------------------------
*+ 1           /usr/lib/jvm/jre-1.7.0-openjdk.x86_64/bin/java
   2           /usr/lib/jvm/jre-1.8.0-openjdk.x86_64/bin/java

Enter を押して現在の選択 [+] を保持するか、選択番号を入力します:

Tomcat のインストール

yumでインストールする。
(tomcat8.5ではなくtomcat8がインストールされるため、8.5がよい場合は調べて対応してください。)

sudo yum install -y tomcat8 tomcat8-webapps

※tomcat8-webapps は起動確認用にインストールしている。省略可。

インスタンスを起動し、自動起動の設定を行う。

sudo service tomcat8 start
sudo chkconfig tomcat8 on

tomcatの画面が開けることを確認する(tomcat8-webappsをインストールした場合)。

http://xxx.example.com:8080

Certbotクライアント (旧称:Let's Enryptクライアント)のインストール

wget https://dl.eff.org/certbot-auto
chmod a+x certbot-auto
./certbot-auto --debug

参照
https://letsencrypt.jp/usage/install-certbot.html#OtherUNIX

Amazon Linux は正式対応が済んでいないので --debug オプションが必要。

SSL/TLSサーバ証明書を取得

./certbot certonly --standalone -d ドメイン名

途中、メールアドレスを求められる。

作成されたファイルは以下に保存される。

/etc/letsencrypt/live/

Nginx の設定

/etc/nginx/nginx.conf を以下のように設定する。

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /var/run/nginx.pid;

include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    include /etc/nginx/conf.d/*.conf;

    index   index.html index.htm;

    upstream tomcat {
        server 127.0.0.1:8080 fail_timeout=0;
    }

    server {
        listen       80;
        server_name  xxx.example.com;
        return 301 https://$host$request_uri;
    }

    server {
        listen       443 ssl;
        server_name  xxx.example.com;
        ssl_certificate /etc/letsencrypt/live/xxx.example.com/fullchain.pem;
        ssl_certificate_key /etc/letsencrypt/live/xxx.example.com/privkey.pem;

        location / {
            proxy_set_header X-Forwarded-Host $host;
            proxy_set_header X-Forwarded-Server $host;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Proto https;
            proxy_redirect http:// https://;
            proxy_pass http://tomcat/;
        }

    }
}

nginx を再起動する。

sudo service nginx restart

以下のようにアクセスするとhttpsでリダイレクトされてtomcatの画面が表示される。

http://xxx.example.com

4
5
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
4
5