LoginSignup
13
3

More than 3 years have passed since last update.

個人でドメインと証明書取ってhttps接続可能なWebサーバ構築を格安で試す

Last updated at Posted at 2020-03-15

お題

表題の通り。構築までの費用は「1円」。
個人で何かWebサービス作って公開してみようと思った時に、この手のイニシャルコストを極力下げようとするとどんな感じか。
いろんなサイトにこの手の情報は載っているけど、今時点で、この記事に記載の使用サービス、条件でやってみるとこんな感じになるという参考情報にはなるかと。

【使用サービス】

【お断り】
とにかくイニシャルコストを格安にしているだけ。
なので、(いくら個人のサービスとはいえ)プロダクション公開して継続してサービス提供していくのであれば、いずれはそれなりのコストが追加で発生していくことになる。
例えば。

  • ドメインは1年更新で、1年目のコストが低い場合、次年度以降の継続は(たいてい)割高になる。
  • GCPは契約後1年間は3万円分の無料トライアルがついているが、それを超えると、サービス継続するには当然課金が必要になる。

前提

  • お名前.com」のアカウントは既に持っている。
  • Google Cloud Platform」のアカウントは既に持っている。
  • DNSやSSLについてそもそもそれはどういうものかは知っている。

実践

「お名前.com」で適当なドメインをゲット

ドメイン取ろうと思ったらここ、というくらい有名なレジストラですね。

screenshot-www.onamae.com-2020.03.15-17_28_45.png

とにかく格安でという方針なので、目標は「1円」。
試す用途なのでドメインは適当な名前。
お、「.work」なら「1円」だ。

screenshot-www.onamae.com-2020.03.15-17_41_10.png

というわけで、「forssltrial.work」に決定。

screenshot-cart.onamae.com-2020.03.15-17_55_29.png

申し込み完了する。

screenshot-cart.onamae.com-2020.03.15-18_08_00.png

※確認のメール(こんなタイトル:「【重要】[お名前.com] ドメイン 情報認証のお願い」)が飛んでいるのでリンク踏んでおくこと。

管理画面で見ると、こんな感じ。

screenshot-www.onamae.com-2020.03.15-18_27_39.png

「Google Cloud Platform (GCP)」でDNSの設定

以下の通り、先ほど「お名前.com」でゲットしたドメイン「forssltrial.work」のネームサーバは↓

$ dig forssltrial.work ns +short
dns1.onamae.com.
dns2.onamae.com.

※第3者でも確認する方法としては↑だけど、今回は自分で買ったやつだから「お名前.com」管理画面で見てもいい。。。

ドメイン買ったけど、自分のサイト(そもそもまだ構築してないけど)と紐付いているわけでもないので、このドメインをブラウザで見ても以下のようになるだけ。

Screenshot from 2020-03-15 18-46-38.png

今回は、サーバをGCPで立てる。あわせて、名前解決もGCPのサービスに任せるので、まずはその設定。
GCPの「Cloud DNS」のページからDNSゾーンを作る。

screenshot-console.cloud.google.com-2020.03.15-18_55_01.png

DNS名は「お名前.com」でゲットしたドメイン名。ゾーン名は適当。

screenshot-console.cloud.google.com-2020.03.15-18_56_59.png

これで、このドメイン名に対してCloud DNSで名前解決できるようになった。

screenshot-console.cloud.google.com-2020.03.15-18_58_33.png

おっ、ということは、先ほどは「お名前.com」のページが表示された「http://forssltrial.work」をブラウザで表示すると、今度はGCPの何かしらのページが出るのか?

Screenshot from 2020-03-15 19-06-45.png

変わらない・・・。

$ dig forssltrial.work ns +short
dns1.onamae.com.
dns2.onamae.com.

変わらない・・・。

うん、このドメイン名、「お名前.com」というレジストラでゲットしたもので、このドメイン名に対して使うネームサーバが決められている様子。
確認しにいく。

「お名前.com」でネームサーバーを変更

screenshot-www.onamae.com-2020.03.15-20_20_02.png

この通り。(現在のネームサーバーについては dig で確認してたけど)
で、「変更する」ボタンがあるので押下。

screenshot-navi.onamae.com-2020.03.15-20_24_30.png

Cloud DNSのネームサーバーを指定する。

screenshot-navi.onamae.com-2020.03.15-20_26_40.png

screenshot-navi.onamae.com-2020.03.15-20_27_47.png

しばし、時間を置き。。。

$ dig forssltrial.work ns +short
ns-cloud-d3.googledomains.com.
ns-cloud-d4.googledomains.com.
ns-cloud-d1.googledomains.com.
ns-cloud-d2.googledomains.com.

はい。変更完了。
じゃあ、「http://forssltrial.work」をブラウザで表示すると?

Screenshot from 2020-03-15 21-53-25.png

「お名前.com」の名前解決でなくなったので、先ほどまで出ていた画面は当然出ず。
で、Cloud DNS上にも、A レコード作ってないのでドメイン名指定しても、飛び先がないということで。

「Google Cloud Platform (GCP)」でWebサーバを立てる

とりあえずGCP上でWebサーバを立てるということで、後にSSL証明書を使う都合も考慮し、作業しやすい Compute Engine のインスタンスを用意する。

screenshot-console.cloud.google.com-2020.03.15-22_06_55.png

別にスペックは何でもよかったけど、とりあえずOSは作業する上で使い慣れているUbuntuにしておいた。

screenshot-console.cloud.google.com-2020.03.15-22_15_32.png

Webサーバとする(さらに、後ほどSSL対応する)のでHTTP/HTTPSトラフィックは通しておく。

screenshot-console.cloud.google.com-2020.03.15-22_16_00.png

で、出来上がったインスタンスにSSH接続。
(インスタンス出来ただけじゃ、Webサーバ立ち上がってないので、SSHで入り、Webサーバ入れる。)

screenshot-console.cloud.google.com-2020.03.15-22_19_13.png

screenshot-console.cloud.google.com-2020.03.15-23_08_59.png

Nginx入れておく。

$ sudo apt-get install -y nginx
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following package was automatically installed and is no longer required:
  libnuma1
   〜〜〜 省略 〜〜〜
Setting up nginx (1.16.1-0ubuntu2.1) ...
Processing triggers for ufw (0.36-1ubuntu3) ...
Processing triggers for systemd (242-7ubuntu3.7) ...
Processing triggers for man-db (2.8.7-3) ...
Processing triggers for libc-bin (2.30-0ubuntu2.1) ...

確認。

$ curl localhost
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

これで、このCompute EngineインスタンスにWebサーバが立ち上がったので、80ポートでアクセスすればNginxの初期ページが表示されるはず。
Compute Engineインスタンスの「外部IP」欄に「35.189.150.31」とあるので、ブラウザで「http://35.189.150.31/」にアクセス。

Screenshot from 2020-03-15 23-42-21.png

ほい、出た。
が、「http://forssltrial.work/」の方では、まだダメ。Cloud DNSでDNSゾーンは作った(そして、ネームサーバーとしては機能してる)けど、ドメイン→IPアドレスの正引き設定(いわゆる A レコード)してないから。

Cloud DNSで A レコードを追加

今は、まだ「http://forssltrial.work/」に対するIPアドレスへのマッピング(いわゆる A レコード)は無い。

$ dig forssltrial.work a +short

では、A レコード作成。

screenshot-console.cloud.google.com-2020.03.16-00_17_11.png

screenshot-console.cloud.google.com-2020.03.16-00_20_26.png

screenshot-console.cloud.google.com-2020.03.16-00_21_02.png

はい、出来た。
で、先ほど無反応だった A レコードへの dig は。

$ dig forssltrial.work a +short
35.189.150.31

OK。
これで「http://forssltrial.work/」で開くか?

Screenshot from 2020-03-16 00-23-53.png

はい、今度はOK。
以上で、HTTP版でのWebサイト表示が完了。次は、これをHTTPS版へ。
今のままでは、ブラウザに「Not secure」(ブラウザによって出方は違う)と出てしまうので。
試しに、現時点で https で開いてみると、以下のようになる。

Screenshot from 2020-03-16 00-56-22.png

Let’s EncryptでSSL証明書をゲット

まっとうにやってくと、秘密鍵こさえて証明書の署名リクエスト(いわゆるCSR)作って、で、オレオレ署名を使うとかでなければ(昔は、とりあえず https が必要になったらオレオレ署名だったんだけど・・・)、どこぞの認証局にSSL証明書を発行してもらう手続きしてといった感じの流れが必要になる。(※お金もかかる
のだけど、最近は、無料でSSL証明書くれるとこがある。Let’s Encryptは、その代表格と言えると思う。(というか、他に知らない。)

certbot-auto を取得

Let’s Encryptは、certbot-auto というツールを使って、自動でSSL証明書を発行する。
まずは、このツールを取得

$ sudo wget https://dl.eff.org/certbot-auto -O /usr/sbin/certbot-auto
--2020-03-15 15:59:48--  https://dl.eff.org/certbot-auto
Resolving dl.eff.org (dl.eff.org)... 151.101.192.201, 151.101.128.201, 151.101.64.201, ...
Connecting to dl.eff.org (dl.eff.org)|151.101.192.201|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 80073 (78K) [application/octet-stream]
Saving to: ‘/usr/sbin/certbot-auto’

/usr/sbin/certbot-auto                            100%[============================================================================================================>]  78.20K  --.-KB/s    in 0.001s

2020-03-15 15:59:48 (104 MB/s) - ‘/usr/sbin/certbot-auto’ saved [80073/80073]

$ sudo chmod a+x /usr/sbin/certbot-auto
$

certbot-auto を使ってSSL証明書を取得

$ sudo certbot-auto certonly -d forssltrial.work
Bootstrapping dependencies for Debian-based OSes... (you can skip this with --no-bootstrap)
Hit:1 http://asia-northeast1.gce.archive.ubuntu.com/ubuntu eoan InRelease
Get:2 http://asia-northeast1.gce.archive.ubuntu.com/ubuntu eoan-updates InRelease [97.5 kB]
Get:3 http://asia-northeast1.gce.archive.ubuntu.com/ubuntu eoan-backports InRelease [88.8 kB]
   〜〜〜 省略 〜〜〜
Need to get 41.1 MB of archives.
After this operation, 170 MB of additional disk space will be used.
Do you want to continue? [Y/n]
   〜〜〜 省略 〜〜〜
How would you like to authenticate with the ACME CA?
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1: Nginx Web Server plugin (nginx)
2: Spin up a temporary webserver (standalone)
3: Place files in webroot directory (webroot)
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Select the appropriate number [1-3] then [enter] (press 'c' to cancel): 1
Plugins selected: Authenticator nginx, Installer None
Enter email address (used for urgent renewal and security notices) (Enter 'c' to
cancel): sky14cloud3@gmail.com

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Please read the Terms of Service at
https://letsencrypt.org/documents/LE-SA-v1.2-November-15-2017.pdf. You must
agree in order to register with the ACME server at
https://acme-v02.api.letsencrypt.org/directory
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(A)gree/(C)ancel: a

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Would you be willing to share your email address with the Electronic Frontier
Foundation, a founding partner of the Let's Encrypt project and the non-profit
organization that develops Certbot? We'd like to send you email about our work
encrypting the web, EFF news, campaigns, and ways to support digital freedom.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(Y)es/(N)o: n
Obtaining a new certificate
Performing the following challenges:
http-01 challenge for forssltrial.work
Waiting for verification...
   〜〜〜 ここで、しばらく時間がかかった。 〜〜〜
Cleaning up challenges

IMPORTANT NOTES:
 - Congratulations! Your certificate and chain have been saved at:
   /etc/letsencrypt/live/forssltrial.work/fullchain.pem
   Your key file has been saved at:
   /etc/letsencrypt/live/forssltrial.work/privkey.pem
   Your cert will expire on 2020-06-13. To obtain a new or tweaked
   version of this certificate in the future, simply run certbot-auto
   again. To non-interactively renew *all* of your certificates, run
   "certbot-auto renew"
 - Your account credentials have been saved in your Certbot
   configuration directory at /etc/letsencrypt. You should make a
   secure backup of this folder now. This configuration directory will
   also contain certificates and private keys obtained by Certbot so
   making regular backups of this folder is ideal.
 - If you like Certbot, please consider supporting our work by:

   Donating to ISRG / Let's Encrypt:   https://letsencrypt.org/donate
   Donating to EFF:                    https://eff.org/donate-le

Congratulations!」と出たので無事完了。
IMPORTANT NOTES」としていろいろ書かれてあるのだけど、とりあえず、https でアクセスできるよう、Webサーバ(Nginx)の設定をする上では下記が重要。

  • SSL証明書(と中間証明書をくっつけたもの)は「/etc/letsencrypt/live/forssltrial.work/fullchain.pem」にある。
  • 秘密鍵は「/etc/letsencrypt/live/forssltrial.work/privkey.pem」にある。

Nginxのhttps設定

とにかく https で接続できるようにするだけの設定をほどこす。
Nginxの設定の仕方としてのあるべき方法などは考慮しない。

$ pwd
/etc/nginx/sites-available
$
$ sudo cp ./default ./default_org
$ 
$ vim default
$
$ diff -s default_org default
72a73,85
> server {
>       listen 443;
>       ssl on;
>       ssl_certificate     /etc/letsencrypt/live/forssltrial.work/fullchain.pem;
>       ssl_certificate_key /etc/letsencrypt/live/forssltrial.work/privkey.pem;
>       root /var/www/html;
>       index index.html index.htm index.nginx-debian.html;
>       server_name _;
>       location / {
>               try_files $uri $uri/ =404;
>       }
> }
>
>
$
$ sudo nginx -t
nginx: [warn] the "ssl" directive is deprecated, use the "listen ... ssl" directive instead in /etc/nginx/sites-enabled/default:75
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
$
$ sudo systemctl restart nginx
$
$ sudo systemctl status nginx
● nginx.service - A high performance web server and a reverse proxy server
   Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
   Active: active (running) since Sun 2020-03-15 16:36:36 UTC; 13s ago
   〜〜〜 省略 〜〜〜

で、ここまでの設定で「https://forssltrial.work/」を開いてみると、無事、表示されるようになっている。

Screenshot from 2020-03-16 01-44-14.png

警告も出ていないし。

Screenshot from 2020-03-16 01-46-38.png

Let’s Encryptが発行した証明書であることも明示されている。

Screenshot from 2020-03-16 01-47-32.png

13
3
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
13
3