LoginSignup
2
3

More than 1 year has passed since last update.

シェルスクリプトでLet's encryptの無料SSL証明書を発行(CentOS6対応)

Last updated at Posted at 2022-03-01

Let's encrypt の無料SSL証明書を使いたいけど・・・

最近は開発中であってもhttpsが必要なケースが増えてきました。
以前は高価だったSSL証明書が、無料で取得・使用できる Let's encrypt があり大助かりです。
ですが、推奨される取得インストール方法が Certbot を使用とされており、CentOS6 には現在インストールできるパッケージがありません。
パッケージを使わず Certbot をインストールするには、Pythonのバージョンをあげて合わせなくてはならず、既にサービスが稼働しているサーバーでPythonのバージョン変更や共存は、危険で難易度が高いです。

ACME クライアント?

Certbotを使わずとも、ACMEクライアントを使用すればSSL証明書の発行ができるようです。
詳細は省きますが、多くのACMEクライアントがラインアップされています。
日本語ではCertbot以外の情報がとても少なくいです・・・試すしかないか!

各種言語を使ったものがあるようですが、インストールが不要で環境依存が少なそうな、シェルスクリプトを使った、その名も「getssl」を試してみたいと思います。

getsslのインストール

WEBサーバーにシェルログインして、getsslをインストールします。
と、言ってもファイルを1つダウンロードするだけです。

#curl --silent https://raw.githubusercontent.com/srvrco/getssl/latest/getssl > getssl ; chmod 700 getssl

root ユーザーで、ホームディレクトリ /root にログインしたとして進めています。

設定ファイルの生成

getsslを -c オプションをつけて実行し、設定ファイルを生成します。
SSL証明書を発行するドメインは「yourdomain.com」とし、このWEBサーバーは www.yourdomain.com でhttpアクセスできるものとします。

#./getssl -c yourdomain.com
creating main config file /root/.getssl/getssl.cfg
Making domain directory - /root/.getssl/yourdomain.com
creating domain config file in /root/.getssl/yourdomain.com/getssl.cfg
Adding SANS=www.yourdomain.com from certificate installed on yourdomain.com to new configuration file
created domain config file in /root/.getssl/yourdomain.com/getssl.cfg
#

カレントディレクトリに、.getssl/yourdomain.com/ ディレクトリが作成され、getssl.cfg ファイルが生成されました。

設定ファイルの編集

ドメインのディレクトリにある設定ファイル(getssl.cfg)を編集します。
いくつかの項目がありますが、以下の行を追加します。

.getssl/yourdomain.com/getssl.cfg
CA="https://acme-v02.api.letsencrypt.org"
ACL=('/var/www/html/.well-known/acme-challenge')

1行目はデフォルトではテストサーバーを使う設定を本番サーバーに変更します。
2行目はこのWEBサーバーのドキュメントルートが、/var/www/html であるという前提です。

証明書の生成

getsslを実行し、証明書ファイルを生成します。

#./getssl yourdomain.com
yourdomain.com: no certificate obtained from host
creating account key /root/.getssl/account.key
creating key - /root/.getssl/account.key
Generating RSA private key, 4096 bit long modulus
.......................................................++
e is 65537 (0x10001)
creating key - /root/.getssl/yourdomain.com/yourdomain.com.key
Generating RSA private key, 4096 bit long modulus
.................................................++
e is 65537 (0x10001)
creating domain csr - /root/.getssl/yourdomain.com/yourdomain.com.csr
Registering account
Registered
Verify each domain
Verifying yourdomain
copying challenge token to /var/www/html/.well-known/acme-challenge/WmQvH0WP1KBPSfT----1hwFBT7oxV0
sending request to ACME server saying we're ready for challenge
checking if challenge is complete
Pending
checking if challenge is complete
Verified yourdomain.com
Verification completed, obtaining certificate.
Requesting Finalize Link
Requesting Order Link
Requesting certificate
Certificate saved in /root/.getssl/yourdomain.com/yourdomain.com.crt
/root/.getssl/yourdomain.com/yourdomain.com.crt not returned by server
getssl: yourdomain.com - rsa certificate obtained but not installed on server
# 

これで証明書が作成されました。
エラーになる場合、ブラウザで
http://yourdomain.com/.well-known/acme-challenge/表示された文字列
にアクセス(表示)できるか確認してみて下さい。

証明書発行

/root/.getssl/yourdomain.com/ に証明書ファイル等が発行されています。

  • chain.crt ....中間証明書
  • yourdomain.crt ....証明書
  • fullchain.crt ....秘密鍵+中間証明書+証明書を1つにまとめたもの
  • yourdomain.csr ....署名リクエスト→不要
  • yourdomain.key ....秘密鍵

多くのWebサーバーでは、秘密鍵・証明書・中間証明書の3つ設定すれば https 通信が可能になるはずです。

証明書の有効期限は3ヶ月ですが、getsslには自動的に証明書を更新するようなオプションもあり、色々活用できると思います。

getsslシェルスクリプトなのであまり環境を選ばないので使い勝手がいいのではないでしょうか。

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