Xサーバーの無料VPS機能シリーズ第3弾は「Let’s Encrypt」をセットするやり方についてです。

簡単に言うとサイトをSSL通信に対応させ、暗号化された状態でページを表示させる方法になります。

「Let’s Encrypt」でSSL/TLS証明書を取得する

Let’s Encryptとは?SSL/TLS証明書を無料で発行できる認証局

Let’s EncryptはSSL/TLS証明書を無料で発行できる認証局(CA)のことです。

SSL/TLS証明書を発行することでウェブサイトの通信を暗号化し、安全にデータ通信を行うことができるようになります。

ただし条件があります。

SSL/TLS証明書を発行する条件
サーバーにApacheまたはNginxがインストールされている
独自ドメインまたはXServerのサブドメインを取得している

XServerのサブドメインとは「*****.xvps.jp」のようなドメインのことです。

こちらも無料で取得できるので、まだの人は先に取得しておきましょう。

SSL/TLS証明書の取得準備とCertbotのインストール

Let’s Encryptより提供されている自動化ソフト「Certbot」を使って、SSL/TLS証明書を発行していきます。

ここではNginxがインストールされているwebサーバーに対して行います。

まずはCertbotをインストールしましょう。

# Certbotをインストール
sudo dnf install certbot python3-certbot-nginx -y

pythonと書かれているのはCertbotがPython製だからです。

python3-certbot-nginxはNginxとPythonの橋渡しをするプラグインで、Webサーバー内のhttps設定を自動で行うためのcertbotをnginxとをつないでくれます。

# Certbotのインストール失敗
No match for argument: certbot
No match for argument: python3-certbot-nginx
Error: Unable to find a match: certbot python3-certbot-nginx

こんな感じでインストールできないこともあります。

certbotパッケージが見つからないからなのですが、その場合は先にパッケージを追加してあげます。

# AlmaLinux10向けのEPELリポジトリを追加
sudo dnf install https://dl.fedoraproject.org/pub/epel/epel-release-latest-10.noarch.rpm -y
# DNFのキャッシュを全削除し、リポジトリ情報を更新
sudo dnf clean all
# リポジトリのメタデータをキャッシュに再作成
sudo dnf makecache
certbotを再度検索
sudo dnf search certbot

パッケージが追加できたことを確認したら、再び「install certbot python3-certbot-nginx」を実行して、certbotをインストールしましょう。

SSL/TLS証明書の取得準備とCertbotの実行

SSL/TLS証明書を取得するため、Certbotを実行します。

# certbot –nginx -d ドメイン名 -d www.ドメイン名
sudo certbot –nginx -d example.jp -d www.example.jp

ここからは対話形式で進みます。

# メールアドレスを聞かれる
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Enter email address or hit Enter to skip.
(Enter ‘c’ to cancel):

メールアドレスを聞かれますが、特になければ入力することをおすすめします。

推奨する理由
証明書の有効期限切れ前に通知メールが届く
Let’s Encryptからの重要なお知らせを受信
証明書の更新失敗時にアラートを受信

スキップする場合は何も入力せずEnterキーを押します。

# 利用規約に同意

Please read the Terms of Service at:
https://letsencrypt.org/documents/LE-SA-v1.5-February-24-2025.pdf
You must agree in order to register with the ACME server. Do you agree?

– – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –
(Y)es/(N)o:

利用規約に同意する必要があります。

# メール配信に同意

Would you be willing, once your first certificate is successfully issued, 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:

お知らせのメール配信に同意するか聞かれます。

# エラー発生
An unexpected error occurred:
too many certificates (50) already issued for “xvps.jp” in the last 168h0m0s, retry after 2025-08-21 15:29:29 UTC: see https://letsencrypt.org/docs/rate-limits/#new-certificates-per-registered-domain
Ask for help or search for solutions at https://community.letsencrypt.org. See the logfile /var/log/letsencrypt/letsencrypt.log or re-run Certbot with -v for more details.

こんなエラーが発生することがあります。

過去168時間(7日間)以内にxvps.jpドメインに対する証明書の発行が上限(50)を超えているようです。

この場合は時間を置いてCertbotを実行するか、他のドメインを使いましょう。

以下のようになれば成功です。

Successfully received certificate.
Certificate is saved at: /etc/letsencrypt/live/example.jp/fullchain.pem
Key is saved at: /etc/letsencrypt/live/example.jp/privkey.pem
This certificate expires on 2025-11-20.
These files will be updated when the certificate renews.
Certbot has set up a scheduled task to automatically renew this certificate in the background.

公開鍵と秘密鍵がどこに生成されているか、有効期限はいつまでかといったことが書かれています。

これをメモしておいて、後でSSL設定に使用します。

SSL/TLS証明書の自動更新設定

有効期限が近付くとメールでお知らせが来るようになっているとはいうものの、証明書の自動更新を設定しておくと便利です。

最近は自動更新があらかじめ備わっていたりするので、まずはそちらを確認しましょう。

# cronにエントリがあるか確認
sudo cat /etc/cron.d/certbot
# systemdタイマーを確認
systemctl list-timers | grep certbot

これで反応があればいいですが、何もない場合は自動更新が設定されていないことになります。

その場合は、自分で設定してあげます。

# root権限でcronジョブを作成
sudo vi /etc/cron.d/certbot_renew
# 設定内容を記入
0 2 * * * root certbot renew –quiet –deploy-hook “systemctl reload nginx”

一日一回証明書の確認を行い、有効期限が30日以内なら更新されるようになります。

設定ができたら、cronを再読み込みしてあげます。

# cronを再読み込み
sudo systemctl restart crond

これでとりあえずはOKですが、本当に更新ができるか、一度テストしてみましょう。

# 更新できるかテスト
sudo certbot renew –dry-run

これで「succeeded」が表示されれば、自動更新の設定は完了です。

webサーバーとSSL通信の設定

webサーバーにSSL証明書をセットする

SSL証明書が取得できたら、webサーバーの設定も行います。

# viエディタを起動
sudo vi /etc/nginx/conf.d/{domain_name}.conf

エディタを起動したら、SSL証明書に関連する設定を追加していきます。

# HTTPリダイレクト
server {
    listen 80;
    server_name {domain_name} www.{domain_name};
    return 301 https://{domain_name}$request_uri;
}

# HTTPS
server {
    listen 443 ssl;

    ssl_certificate /etc/letsencrypt/live/{domain_name}/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/{domain_name}/privkey.pem;
    include /etc/letsencrypt/options-ssl-nginx.conf;
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;

    server_name {domain_name} www.{domain_name}; 
    root /var/www/{domain_name}/public_html;
    index index.html index.htm;
    
    location / {
        try_files $uri $uri/ =404;
    }

    # ブラウザでの閲覧拒否 (.htaccess, .htpasswd)
    location ~ /\.(htaccess|htpasswd)$ {
        deny all;
    }

    # 文字コードの設定
    charset UTF-8;

    # indexなしでもファイル一覧を表示しない
    autoindex off;
}

特に公開鍵と秘密鍵を読み込めるように設定してあげましょう。

設定が一通りできたら、SSL通信でページが表示されるか確認してみましょう。

きちんと設定がなされていれば、http//にアクセスしてもhttps//にリダイレクトして表示されます。