CentOS7 の Nginx で Let's Encrypt を使う


CentOS 7.6 に Nginx をインストールして Let's Encrypt の設定をします。
abc.example.com に設定するとします。

1) http(80),https(443) のポートにアクセスできるようにします。

sudo firewall-cmd --permanent --add-service=http --zone=public
sudo firewall-cmd --permanent --add-service=https --zone=public
sudo firewall-cmd --reload

2) Nginx をインストールします。

sudo yum -y install nginx

設定ファイルは、
/etc/nginx/nginx.conf です。

root /usr/share/nginx/html;

root /var/www/html;
に変更しています。

3) Nginx の起動

sudo systemctl start nginx
sudo systemctl enable nginx

ブラウザーで、http://abc.example.com にアクセスします。

4) certbot のインストール

sudo yum -y install certbot

5) 証明書の生成

sudo certbot certonly --webroot -w /var/www/html -d abc.example.com --email [email protected]

6) /etc/nginx/nginx.conf の編集

/etc/nginx/nginx.conf
server {
listen  443 ssl;
server_name     abc.example.com;
ssl_certificate         /etc/letsencrypt/live/abc.example.com/fullchain.pem;
ssl_certificate_key     /etc/letsencrypt/live/abc.example.com/privkey.pem;
}

7) Nginx の再起動

sudo systemctl restart nginx

https://abc.example.com にブラウザーでアクセス

AWS などだと、OS の外でポートのアクセスに制限がかけることができるので、外部から 443 ポートにアクセスできるか確認します。

nmap -sT abc.example.com

次のように表示されれば、アクセスが出来ます。

PORT     STATE  SERVICE
80/tcp   open   http
443/tcp  open   https