CentOS7.9のnginxにLet’s Encrypt証明書をインストールする

2021年11月18日

OSバージョンを確認
# cat /etc/redhat-release
CentOS Linux release 7.9.2009 (Core)

1.ファイアウォール設定

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
現状の設定を確認
# firewall-cmd --list-all
httpsサービスを許可
# firewall-cmd --add-service=https --permanent
success
設定反映
# firewall-cmd --reload
success
設定確認
# firewall-cmd --list-all
現状の設定を確認 # firewall-cmd --list-all httpsサービスを許可 # firewall-cmd --add-service=https --permanent success 設定反映 # firewall-cmd --reload success 設定確認 # firewall-cmd --list-all
現状の設定を確認
# firewall-cmd --list-all
httpsサービスを許可
# firewall-cmd --add-service=https --permanent
success
設定反映
# firewall-cmd --reload
success
設定確認
# firewall-cmd --list-all

2. certbotパッケージのインストール
certbotパッケージの確認

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
# yum search certbot
# yum search certbot
# yum search certbot

certbotパッケージのインストール
# yum -y install certbot
certbotインストール後の確認

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
# yum list installed | grep certbot
certbot.noarch 1.11.0-2.el7 @epel
python2-certbot.noarch 1.11.0-2.el7 @epel
# yum list installed | grep certbot certbot.noarch 1.11.0-2.el7 @epel python2-certbot.noarch 1.11.0-2.el7 @epel
# yum list installed | grep certbot
certbot.noarch                         1.11.0-2.el7                   @epel
python2-certbot.noarch                 1.11.0-2.el7                   @epel

3.Let’s Encryptによるサーバー証明書の作成

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
# mkdir -p /home/ssl/www.sample.com
# certbot certonly --webroot -w /home/ssl/www.sample.com/ -d www.sample.com
# mkdir -p /home/ssl/www.sample.com # certbot certonly --webroot -w /home/ssl/www.sample.com/ -d www.sample.com
# mkdir -p /home/ssl/www.sample.com
# certbot certonly --webroot -w /home/ssl/www.sample.com/ -d www.sample.com

サーバー証明書ファイルの確認

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
# ls -l /etc/letsencrypt/live/www.sample.com
# ls -l /etc/letsencrypt/live/www.sample.com
# ls -l /etc/letsencrypt/live/www.sample.com

ファイルを確認
README cert.pem chain.pem fullchain.pem privkey.pem
ファイルの役割
cert.pem サーバー証明書本体
chain.pem 中間証明書
privkey.pem 秘密鍵ファイル
fullchain.pem サーバー証明書と中間証明書のセット

dhparam用ファイルの作成

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
# mkdir /etc/nginx/ssl
# openssl dhparam -out /etc/nginx/ssl/dhparam.pem 2048
# mkdir /etc/nginx/ssl # openssl dhparam -out /etc/nginx/ssl/dhparam.pem 2048
# mkdir /etc/nginx/ssl

# openssl dhparam -out /etc/nginx/ssl/dhparam.pem 2048

ファイルを確認
# ls -a /etc/nginx/ssl/
. .. dhparam.pem

4.nginxへのSSL通信用の設定
# vi /etc/nginx/conf.d/www.sample.com.conf

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
server {
listen 80;
server_name sample.com www.sample.com;
rewrite ^ https://$server_name$request_uri? permanent;
}
server {
listen 443 ssl;
server_name sample.com www.sample.com;
access_log /var/log/nginx/www.sample.com-access.log main;
error_log /var/log/nginx/www.sample.com-error.log;
root /home/www/www.sample.com;
ssl_certificate /etc/letsencrypt/live/www.sample.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/www.sample.com/privkey.pem;
ssl_session_timeout 1d;
ssl_session_cache shared:SSL:50m;
ssl_session_tickets on;
ssl_dhparam /etc/nginx/ssl/dhparam.pem;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
location / {
index index.php index.html index.htm;
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
server { listen 80; server_name sample.com www.sample.com; rewrite ^ https://$server_name$request_uri? permanent; } server { listen 443 ssl; server_name sample.com www.sample.com; access_log /var/log/nginx/www.sample.com-access.log main; error_log /var/log/nginx/www.sample.com-error.log; root /home/www/www.sample.com; ssl_certificate /etc/letsencrypt/live/www.sample.com/fullchain.pem; ssl_certificate_key /etc/letsencrypt/live/www.sample.com/privkey.pem; ssl_session_timeout 1d; ssl_session_cache shared:SSL:50m; ssl_session_tickets on; ssl_dhparam /etc/nginx/ssl/dhparam.pem; ssl_protocols TLSv1 TLSv1.1 TLSv1.2; ssl_ciphers HIGH:!aNULL:!MD5; ssl_prefer_server_ciphers on; location / { index index.php index.html index.htm; } location ~ \.php$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } }
server {
listen 80;
server_name sample.com www.sample.com;
rewrite ^ https://$server_name$request_uri? permanent;
}

server {
listen 443 ssl;
server_name sample.com www.sample.com;
access_log /var/log/nginx/www.sample.com-access.log main;
error_log /var/log/nginx/www.sample.com-error.log;
root /home/www/www.sample.com;

ssl_certificate /etc/letsencrypt/live/www.sample.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/www.sample.com/privkey.pem;
ssl_session_timeout 1d;
ssl_session_cache shared:SSL:50m;
ssl_session_tickets on;
ssl_dhparam /etc/nginx/ssl/dhparam.pem;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;

location / {
index index.php index.html index.htm;
}

location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}

5.nginxの再起動
配置ファイルの確認
# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
nginxを再起動
# systemctl restart nginx
状態の確認
# systemctl status nginx

6.httpsの動作確認
ブラウザでサ「https://www.sample.com」を入力し、、正常にHTTPSが動作していることを確認します。

CentOS 7

Posted by arkgame