お題
表題の通り。構築までの費用は「1円」。
個人で何かWebサービス作って公開してみようと思った時に、この手のイニシャルコストを極力下げようとするとどんな感じか。
いろんなサイトにこの手の情報は載っているけど、今時点で、この記事に記載の使用サービス、条件でやってみるとこんな感じになるという参考情報にはなるかと。
【使用サービス】
- 「お名前.com」・・・ドメイン取得(1円)
- 「Let’s Encrypt」・・・SSL証明書取得(無料)
- 「Google Cloud Platform」・・・DNSとサーバ取得(無料トライアル内では無料)
【お断り】
とにかくイニシャルコストを格安にしているだけ。
なので、(いくら個人のサービスとはいえ)プロダクション公開して継続してサービス提供していくのであれば、いずれはそれなりのコストが追加で発生していくことになる。
例えば。
- ドメインは1年更新で、1年目のコストが低い場合、次年度以降の継続は(たいてい)割高になる。
- GCPは契約後1年間は3万円分の無料トライアルがついているが、それを超えると、サービス継続するには当然課金が必要になる。
前提
- 「お名前.com」のアカウントは既に持っている。
- 「Google Cloud Platform」のアカウントは既に持っている。
- DNSやSSLについてそもそもそれはどういうものかは知っている。
実践
「お名前.com」で適当なドメインをゲット
ドメイン取ろうと思ったらここ、というくらい有名なレジストラですね。
とにかく格安でという方針なので、目標は「1円」。
試す用途なのでドメインは適当な名前。
お、「.work
」なら「1円」だ。
というわけで、「forssltrial.work
」に決定。
申し込み完了する。
※確認のメール(こんなタイトル:「【重要】[お名前.com] ドメイン 情報認証のお願い
」)が飛んでいるのでリンク踏んでおくこと。
管理画面で見ると、こんな感じ。
「Google Cloud Platform (GCP)」でDNSの設定
以下の通り、先ほど「お名前.com」でゲットしたドメイン「forssltrial.work
」のネームサーバは↓
$ dig forssltrial.work ns +short
dns1.onamae.com.
dns2.onamae.com.
※第3者でも確認する方法としては↑だけど、今回は自分で買ったやつだから「お名前.com」管理画面で見てもいい。。。
ドメイン買ったけど、自分のサイト(そもそもまだ構築してないけど)と紐付いているわけでもないので、このドメインをブラウザで見ても以下のようになるだけ。
今回は、サーバをGCPで立てる。あわせて、名前解決もGCPのサービスに任せるので、まずはその設定。
GCPの「Cloud DNS」のページからDNSゾーンを作る。
DNS名は「お名前.com」でゲットしたドメイン名。ゾーン名は適当。
これで、このドメイン名に対してCloud DNSで名前解決できるようになった。
おっ、ということは、先ほどは「お名前.com」のページが表示された「http://forssltrial.work
」をブラウザで表示すると、今度はGCPの何かしらのページが出るのか?
変わらない・・・。
$ dig forssltrial.work ns +short
dns1.onamae.com.
dns2.onamae.com.
変わらない・・・。
うん、このドメイン名、「お名前.com」というレジストラでゲットしたもので、このドメイン名に対して使うネームサーバが決められている様子。
確認しにいく。
「お名前.com」でネームサーバーを変更
この通り。(現在のネームサーバーについては dig
で確認してたけど)
で、「変更する」ボタンがあるので押下。
Cloud DNSのネームサーバーを指定する。
しばし、時間を置き。。。
$ 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
」をブラウザで表示すると?
「お名前.com」の名前解決でなくなったので、先ほどまで出ていた画面は当然出ず。
で、Cloud DNS上にも、A レコード作ってないのでドメイン名指定しても、飛び先がないということで。
「Google Cloud Platform (GCP)」でWebサーバを立てる
とりあえずGCP上でWebサーバを立てるということで、後にSSL証明書を使う都合も考慮し、作業しやすい Compute Engine のインスタンスを用意する。
別にスペックは何でもよかったけど、とりあえずOSは作業する上で使い慣れているUbuntuにしておいた。
Webサーバとする(さらに、後ほどSSL対応する)のでHTTP/HTTPSトラフィックは通しておく。
で、出来上がったインスタンスにSSH接続。
(インスタンス出来ただけじゃ、Webサーバ立ち上がってないので、SSHで入り、Webサーバ入れる。)
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/
」にアクセス。
ほい、出た。
が、「http://forssltrial.work/
」の方では、まだダメ。Cloud DNSでDNSゾーンは作った(そして、ネームサーバーとしては機能してる)けど、ドメイン→IPアドレスの正引き設定(いわゆる A レコード)してないから。
Cloud DNSで A レコードを追加
今は、まだ「http://forssltrial.work/
」に対するIPアドレスへのマッピング(いわゆる A レコード)は無い。
$ dig forssltrial.work a +short
では、A レコード作成。
はい、出来た。
で、先ほど無反応だった A レコードへの dig は。
$ dig forssltrial.work a +short
35.189.150.31
OK。
これで「http://forssltrial.work/
」で開くか?
はい、今度はOK。
以上で、HTTP版でのWebサイト表示が完了。次は、これをHTTPS版へ。
今のままでは、ブラウザに「Not secure
」(ブラウザによって出方は違う)と出てしまうので。
試しに、現時点で https
で開いてみると、以下のようになる。
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/
」を開いてみると、無事、表示されるようになっている。
警告も出ていないし。
Let’s Encryptが発行した証明書であることも明示されている。