• 投稿日:2023年06月09日 10時56分48秒
  • 更新日:2023年06月09日 10時56分48秒
無料でサイトをSSL化する

無料でサイトをSSL化する

詳細

Let’s Encryptをインストールと実行

無料のSSLサーバ証明書を発行するフリーソフト「Let’s Encrypt」を使います。

今回はNginxサーバを使っている前提です。

apt install certbot python3-certbot-nginx

certbot --nginx -d [対象のドメイン]

初回はメールアドレスを入力して規約に同意しろと言われますが、これは素直に従えばいいのかなと思います。

注意すべきはその後に以下のような英文が表示されます。

Please choose whether or not to redirect HTTP traffic to HTTPS, removing HTTP access.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1: No redirect - Make no further changes to the webserver configuration.
2: Redirect - Make all requests redirect to secure HTTPS access. Choose this for
new sites, or if you're confident your site works on HTTPS. You can undo this
change by editing your web server's configuration.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Select the appropriate number [1-2] then [enter] (press 'c' to cancel):

これをgoogle翻訳にぶち込むと

HTTPトラフィックをHTTPSにリダイレクトして、HTTPアクセスを削除するかどうかを選択してください。
---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
1:リダイレクトなし-Webサーバー構成にそれ以上の変更を加えません。
2:リダイレクト-すべてのリクエストを安全なHTTPSアクセスにリダイレクトします。のためにこれを選択してください
新しいサイト、またはサイトがHTTPSで動作することを確信している場合。これを元に戻すことができます
Webサーバーの構成を編集して変更します。
---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
適切な番号[1-2]を選択してから[Enter]を選択します(キャンセルするには「c」を押します)。

となります。

断然「2」にしておくべきだと思います。

あとは自動更新設定を有効にしてください。

systemctl status certbot.timer

Nginxに設定

SSL化するドメインにSSLの設定を追記します。

sudo nano /etc/nginx/sites-available/[対象ドメインのconf]
server {
    server_name [ドメイン名];
    root [公開ディレクトリ];

    index index.html index.htm index.php;

    location / {
        #try_files $uri $uri/ =404;
        proxy_pass http://backend;
    }

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
     }

    location ~ /\.ht {
        deny all;
    }

    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/[ドメイン名]/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/[ドメイン名]/privkey.pem;
    include /etc/letsencrypt/options-ssl-nginx.conf;
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;

}
server {
    if ($host = boki.addnew.tech) {
        return 301 https://$host$request_uri;
    } # managed by Certbot

    listen 80;
    server_name [ドメイン名];
    return 404; # managed by Certbot

}

もしかしたら既に追記されているかもしれませんのでご参考程度にどうぞ。

最後にNginxを再起動してください。

systemctl restart nginx.server

ファイヤーウォールの設定

もしファイヤーウォールを設定している場合、Nginxをフルで許可して下さい。

sudo ufw enable

ufw allow 'Nginx Full'
ufw delete allow 'Nginx HTTP'

SPECIAL THANKS

  1. Let’s Encrypt 自動更新失敗の復旧録 -ぶらっとブログ-
  2. Ubuntu 20.04でLet’s Encryptを使用してNginxを保護する方法 -Desital Ocean-
【LINUXコマンド】grep
Laravel Sailでサクッと環境構築
Ubuntuからインストーラを使ってインストールしたNode.jsを削除する
無料でサイトをSSL化する
WSL2を使ってDcoker/Ubuntu環境を構築する
CakePHPの環境を構築する
cURLをインストールする
UbuntuにApacheをインストール