facebook twitter hatena line email

「Linux/LetsEncrypt導入」の版間の差分

提供: 初心者エンジニアの簡易メモ
移動: 案内検索
(Python 2.7のインストール)
 
(同じ利用者による、間の42版が非表示)
行1: 行1:
 +
==let's encryptとは==
 +
無料sslサービス
 +
 
==certbot-autoのインストール==
 
==certbot-autoのインストール==
 
  # cd /usr/local
 
  # cd /usr/local
行6: 行9:
  
 
==証明書取得==
 
==証明書取得==
 +
cd /usr/local/certbot
 
  ./certbot-auto certonly --webroot \
 
  ./certbot-auto certonly --webroot \
 
  -w /var/www/example/public -d example.com \
 
  -w /var/www/example/public -d example.com \
行13: 行17:
 
以下エラーが発生する場合はPythonが2.6になってる可能性があるので、Python 2.7を入れる必要がある
 
以下エラーが発生する場合はPythonが2.6になってる可能性があるので、Python 2.7を入れる必要がある
 
  ./certbot-auto: line 558: virtualenv
 
  ./certbot-auto: line 558: virtualenv
 +
 +
その他エラーが起こった際
 +
*-dのオプションに設定した"example.com"にhttpアクセスできることを事前に確認しておく。
 +
*document_rootのdirがapache権限やnginx権限でアクセスできるか確認する
  
 
==Python 2.7のインストール==
 
==Python 2.7のインストール==
行24: 行32:
 
  Python 2.7.8
 
  Python 2.7.8
  
==ログイン時にpythonが2.7.8となるように==
+
ログイン時にpythonが2.7.8となるように
 
  # vi /etc/profile.d/enablepython27.sh
 
  # vi /etc/profile.d/enablepython27.sh
 
  #!/bin/bash
 
  #!/bin/bash
行32: 行40:
 
参考:http://blog.offline-net.com/2016/05/08/apache_24_with_letsencrypt/
 
参考:http://blog.offline-net.com/2016/05/08/apache_24_with_letsencrypt/
  
==証明書取得が出来たら以下ファイルが出来てることを確認==
+
==証明書取得が出来たら以下のようなファイルが出来てることを確認==
  # ls /etc/letsencrypt/live/[ドメイン]/
+
  # ls /etc/letsencrypt/
  cert.pem       chain.pem     fullchain.pem  privkey.pem
+
├── accounts
 +
│   └── acme-v01.api.letsencrypt.org
 +
│      └── directory
 +
│          └── 1111111111111111111111111111111
 +
│              ├── meta.json
 +
│              ├── private_key.json
 +
│              └── regr.json
 +
├── archive
 +
│   └── example.net
 +
│      ├── cert1.pem
 +
│      ├── cert2.pem
 +
│      ├── chain1.pem
 +
│      ├── chain2.pem
 +
│      ├── fullchain1.pem
 +
│      ├── fullchain2.pem
 +
│      ├── privkey1.pem
 +
│      └── privkey2.pem
 +
├── csr
 +
│   ├── 0000_csr-certbot.pem
 +
│   └── 0001_csr-certbot.pem
 +
├── keys
 +
│   ├── 0000_key-certbot.pem
 +
│   └── 0001_key-certbot.pem
 +
├── live
 +
  │   └── example.net
 +
│      ├── cert.pem -> ../../archive/example.net/cert2.pem
 +
│      ├── chain.pem -> ../../archive/example.net/chain2.pem
 +
│      ├── fullchain.pem -> ../../archive/example.net/fullchain2.pem
 +
  │      └── privkey.pem -> ../../archive/example.net/privkey2.pem
 +
└── renewal
 +
    └── example.net.conf
 +
 
 +
==Too many certificates already issued for exact set of domainsエラー==
 +
短時間に証明書取得コマンドをリクエストしすぎ。しばらくすると戻る
 +
 
 +
同一ドメインは週に20個まで
 +
 
 +
参考:https://community.letsencrypt.org/t/rate-limits-for-lets-encrypt/6769/2
  
 
==nginx設定に追加==
 
==nginx設定に追加==
行48: 行93:
 
     ssl_certificate_key /etc/letsencrypt/live/example.net/privkey.pem;
 
     ssl_certificate_key /etc/letsencrypt/live/example.net/privkey.pem;
  
==sslのfirewallを切る==
+
==SSLのfirewallを切る==
 
  $ sudo vi /etc/sysconfig/iptables
 
  $ sudo vi /etc/sysconfig/iptables
 
  -A INPUT -m state --state NEW -m tcp -p tcp --dport 443 -j ACCEPT
 
  -A INPUT -m state --state NEW -m tcp -p tcp --dport 443 -j ACCEPT
行54: 行99:
  
 
==nginxのproxyを使ってる場合==
 
==nginxのproxyを使ってる場合==
http://qiita.com/ywatai@github/items/a179186a458a42b3c7f0
+
server {
 +
      listen        80;
 +
      server_name    example.net;
 +
      rewrite        ^ https://$http_host$request_uri? permanent;
 +
}
 +
server {
 +
      listen 443 ssl;
 +
      server_name example.net;
 +
      ssl_certificate /etc/letsencrypt/live/example.net/fullchain.pem;
 +
      ssl_certificate_key /etc/letsencrypt/live/example.net/privkey.pem;
 +
      location / {
 +
            proxy_pass http://example.localhost/;
 +
            proxy_redirect http:// https://;
 +
            proxy_set_header Host $host;
 +
            proxy_set_header X-Real-IP $remote_addr;
 +
            proxy_set_header X-Forwarded-Proto https;
 +
            proxy_set_header X-Forwarded-Host $host;
 +
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
 +
      }
 +
      location ^~ /.well-known/acme-challenge/ {
 +
            root /var/www/html/public;
 +
      }
 +
}
 +
証明書を取る前は80側のrewriteを外し、ssl設定も以下のように外しておけばよい
 +
server {
 +
    listen        80;
 +
    server_name  example.net;
 +
    access_log  /var/log/nginx/example.access.log  main;
 +
#    rewrite        ^ https://$http_host$request_uri? permanent;
 +
    location ^~ /.well-known/acme-challenge/ {
 +
          root /var/www/laravel/exmple/public;
 +
    }
 +
}
 +
server {
 +
    listen 443 ssl http2;
 +
    server_name example.net;
 +
#    ssl_certificate /etc/letsencrypt/live/example.net/fullchain.pem;
 +
#    ssl_certificate_key /etc/letsencrypt/live/example.net/privkey.pem;
 +
    location / {
 +
            proxy_pass http://example.localhost:80;
 +
            proxy_redirect http:// https://;
 +
            proxy_set_header Host $host;
 +
            proxy_set_header X-Real-IP $remote_addr;
 +
            proxy_set_header X-Forwarded-Proto https;
 +
            proxy_set_header X-Forwarded-Host $host;
 +
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
 +
    }
 +
    location ^~ /.well-known/acme-challenge/ {
 +
          root /var/www/laravel/example/public;
 +
    }
 +
}
 +
 
 +
参考:http://qiita.com/ywatai@github/items/a179186a458a42b3c7f0
 +
 
 +
===以下error while loading shared libraries: libpython2.7.so.1.0エラーが出た場合===
 +
/home/[user1]/.local/share/letsencrypt/bin/python2.7: error while loading shared libraries: libpython2.7.so.1.0: cannot open shared object file: No such file or directory
 +
sudoでなくsuでrootになって実行すると良い。
 +
 
 +
===nginx: [emerg] no "ssl_certificate" is defined for the "listen ... ssl" directive in /etc/nginx/conf.d/~.conf:15===
 +
となる場合は一旦443のserverを全てコメントアウトしたあと
 +
<pre>
 +
#server {
 +
#    listen 443 ssl http2;
 +
#    server_name siyoz.net;
 +
#}
 +
</pre>
 +
 
 +
上の項目の証明書取得の ./certbot-auto ~を実行する
 +
 
 +
===nginx: [emerg] module "/usr/lib64/nginx/modules/ngx_http_geoip_module.so" エラーの場合===
 +
 
 +
$ sudo yum remove nginx-mod*
 +
$ sudo yum install nginx-module-*
 +
$ sudo systemctl restart nginx
 +
 
 +
参考:https://nopistash.wordpress.com/2018/05/03/centos7-nginx-conflicting-module-versions/
 +
 
 +
==='FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream,'エラーの場合===
 +
移動先のnginxの設定が、直でアクセスしてるドメインじゃなくて、url欄に表示されてるドメインで、されてない可能性あり
  
 
==sslの自動更新==
 
==sslの自動更新==
行64: 行187:
 
更新を手動で行いnginxの設定を再読込する
 
更新を手動で行いnginxの設定を再読込する
 
  # crontab -e
 
  # crontab -e
  0 5 1 * * /usr/local/certbot/certbot-auto certonly --webroot -w /var/www/example -d example.com --renew-by-default && /etc/rc.d/init.d/nginx reload
+
 
 +
全ドメインを更新するには
 +
0 5 1 * * /usr/local/certbot/certbot-auto renew --force-renew && /etc/rc.d/init.d/nginx restart
 +
 
 +
個別ドメインを更新するには
 +
  0 5 1 * * /usr/local/certbot/certbot-auto certonly --webroot -w /var/www/example -d example.com --renew-by-default && /etc/rc.d/init.d/nginx restart
  
 
参考:https://blog.apar.jp/linux/3619/
 
参考:https://blog.apar.jp/linux/3619/
  
==httpsするにあたって==
+
===注意===
 +
更新の部分はサーバに対応する読み込みコマンドを記述すること
 +
 
 +
0 5 1 * * /usr/local/certbot/certbot-auto renew --force-renew && /etc/rc.d/init.d/nginx restart
 +
 
 +
0 5 1 * * /usr/local/certbot/certbot-auto renew --force-renew && service nginx restart
 +
 
 +
==proxyサーバとwebサーバのconfについて==
 +
example.comで以下設定だった場合
 +
*proxyサーバから見たwebサーバ
 +
example.webを/etc/hostsに設定
 +
*proxyサーバのnginx設定
 +
example.com.confを作りserver_nameをexample.comにしてrewrite先としてwebサーバにexample.webを指定する
 +
*webサーバのngxin設定
 +
server_nameをexample.comにする。(server_nameをexample.webに設定しても見に行かない)
 +
 
 +
==httpからhttpsにする際のwebサイト変更箇所==
 +
*httpsの自サイトから外部httpへリダイレクトしている箇所は修正が必要
 
*scriptなどのhttp記述をhttpsへ書き直す
 
*scriptなどのhttp記述をhttpsへ書き直す
 +
*sitemap.xml,robots.txtなどのhttpを書き直す
 +
 +
==ついでにhttp2化(nginx)==
 +
$ sudo vim /etc/yum.repos.d/nginx.repo
 +
[nginx]
 +
name=nginx repo
 +
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
 +
gpgcheck=0
 +
enabled=1
 +
 +
$ yum list nginx
 +
Installed Packages
 +
nginx.x86_64                                              1.8.0-1.el6.ngx                                              @nginx
 +
Available Packages
 +
nginx.x86_64                                              1.11.1-1.el6.ngx
 +
 +
$ sudo yum update nginx
 +
Complete!
 +
$ nginx -v
 +
nginx version: nginx/1.11.1
 +
 +
参考:https://tsuchikazu.net/lets-encrypt-nginx/
 +
 +
==実行時の権限警告==
 +
以下警告が出るとき
 +
<pre>
 +
/usr/local/certbot/certbot-auto has insecure permissions!
 +
To learn how to fix them, visit https://community.letsencrypt.org/t/certbot-auto-deployment-best-practices/91979/
 +
</pre>
 +
以下実行
 +
<pre>
 +
sudo chown root:root /usr/local/certbot/certbot-auto
 +
sudo chmod 755 /usr/local/certbot/certbot-auto
 +
</pre>
 +
 +
==サーバ引っ越し方法==
 +
証明書をごっそり持っていけば使える。証明書の作り直しは不要。
 +
引越し先サーバのfirewallでhttpsのportを開放すれば何もすることはない。
 +
 +
==セキュリティ評価サイト==
 +
https://www.ssllabs.com/ssltest/analyze.html
 +
 +
httpsにした段階でBでした。http2にしたらA-にあがりました。
  
 
==参考==
 
==参考==

2022年9月15日 (木) 18:10時点における最新版

let's encryptとは

無料sslサービス

certbot-autoのインストール

# cd /usr/local
# git clone https://github.com/certbot/certbot
# cd ./certbot
# ./certbot-auto

証明書取得

cd /usr/local/certbot
./certbot-auto certonly --webroot \
-w /var/www/example/public -d example.com \
-m sample@example.com \
--agree-tos

以下エラーが発生する場合はPythonが2.6になってる可能性があるので、Python 2.7を入れる必要がある

./certbot-auto: line 558: virtualenv

その他エラーが起こった際

  • -dのオプションに設定した"example.com"にhttpアクセスできることを事前に確認しておく。
  • document_rootのdirがapache権限やnginx権限でアクセスできるか確認する

Python 2.7のインストール

$ sudo yum install centos-release-scl
$ sudo yum install python27 python27-python-tools
$ python -V
Python 2.6.6
# デフォだと2.6なので以下コマンドで一時的に2.7へ
$ sudo scl enable python27 bash
$ python -V
Python 2.7.8

ログイン時にpythonが2.7.8となるように

# vi /etc/profile.d/enablepython27.sh
#!/bin/bash
source /opt/rh/python27/enable
export X_SCLS="`scl enable python27 'echo $X_SCLS'`" 

参考:http://blog.offline-net.com/2016/05/08/apache_24_with_letsencrypt/

証明書取得が出来たら以下のようなファイルが出来てることを確認

# ls /etc/letsencrypt/
├── accounts
│   └── acme-v01.api.letsencrypt.org
│       └── directory
│           └── 1111111111111111111111111111111
│               ├── meta.json
│               ├── private_key.json
│               └── regr.json
├── archive
│   └── example.net
│       ├── cert1.pem
│       ├── cert2.pem
│       ├── chain1.pem
│       ├── chain2.pem
│       ├── fullchain1.pem
│       ├── fullchain2.pem
│       ├── privkey1.pem
│       └── privkey2.pem
├── csr
│   ├── 0000_csr-certbot.pem
│   └── 0001_csr-certbot.pem
├── keys
│   ├── 0000_key-certbot.pem
│   └── 0001_key-certbot.pem
├── live
│   └── example.net
│       ├── cert.pem -> ../../archive/example.net/cert2.pem
│       ├── chain.pem -> ../../archive/example.net/chain2.pem
│       ├── fullchain.pem -> ../../archive/example.net/fullchain2.pem
│       └── privkey.pem -> ../../archive/example.net/privkey2.pem
└── renewal
    └── example.net.conf

Too many certificates already issued for exact set of domainsエラー

短時間に証明書取得コマンドをリクエストしすぎ。しばらくすると戻る

同一ドメインは週に20個まで

参考:https://community.letsencrypt.org/t/rate-limits-for-lets-encrypt/6769/2

nginx設定に追加

server {
 listen 80;
 server_name example.net;
 rewrite ^ https://$server_name$request_uri? permanent;
}
server {
   listen 443 ssl;
   server_name example.net;
   ssl_certificate /etc/letsencrypt/live/example.net/fullchain.pem;
   ssl_certificate_key /etc/letsencrypt/live/example.net/privkey.pem;

SSLのfirewallを切る

$ sudo vi /etc/sysconfig/iptables
-A INPUT -m state --state NEW -m tcp -p tcp --dport 443 -j ACCEPT
$ sudo /etc/rc.d/init.d/iptables restart

nginxのproxyを使ってる場合

server {
     listen         80;
     server_name    example.net;
     rewrite        ^ https://$http_host$request_uri? permanent;
}
server {
     listen 443 ssl;
     server_name example.net;
     ssl_certificate /etc/letsencrypt/live/example.net/fullchain.pem;
     ssl_certificate_key /etc/letsencrypt/live/example.net/privkey.pem;
     location / {
            proxy_pass http://example.localhost/;
            proxy_redirect http:// https://;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-Proto https;
            proxy_set_header X-Forwarded-Host $host;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
     }
     location ^~ /.well-known/acme-challenge/ {
           root /var/www/html/public;
     }
}

証明書を取る前は80側のrewriteを外し、ssl設定も以下のように外しておけばよい

server {
    listen         80;
    server_name  example.net;
    access_log  /var/log/nginx/example.access.log  main;
#     rewrite        ^ https://$http_host$request_uri? permanent;
    location ^~ /.well-known/acme-challenge/ {
          root /var/www/laravel/exmple/public;
    }
}
server {
    listen 443 ssl http2;
    server_name example.net;
#     ssl_certificate /etc/letsencrypt/live/example.net/fullchain.pem;
#     ssl_certificate_key /etc/letsencrypt/live/example.net/privkey.pem;
    location / {
           proxy_pass http://example.localhost:80;
           proxy_redirect http:// https://;
           proxy_set_header Host $host;
           proxy_set_header X-Real-IP $remote_addr;
           proxy_set_header X-Forwarded-Proto https;
           proxy_set_header X-Forwarded-Host $host;
           proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
    location ^~ /.well-known/acme-challenge/ {
          root /var/www/laravel/example/public;
    }
}

参考:http://qiita.com/ywatai@github/items/a179186a458a42b3c7f0

以下error while loading shared libraries: libpython2.7.so.1.0エラーが出た場合

/home/[user1]/.local/share/letsencrypt/bin/python2.7: error while loading shared libraries: libpython2.7.so.1.0: cannot open shared object file: No such file or directory

sudoでなくsuでrootになって実行すると良い。

nginx: [emerg] no "ssl_certificate" is defined for the "listen ... ssl" directive in /etc/nginx/conf.d/~.conf:15

となる場合は一旦443のserverを全てコメントアウトしたあと

#server {
#     listen 443 ssl http2;
#     server_name siyoz.net;
#}

上の項目の証明書取得の ./certbot-auto ~を実行する

nginx: [emerg] module "/usr/lib64/nginx/modules/ngx_http_geoip_module.so" エラーの場合

$ sudo yum remove nginx-mod*
$ sudo yum install nginx-module-*
$ sudo systemctl restart nginx

参考:https://nopistash.wordpress.com/2018/05/03/centos7-nginx-conflicting-module-versions/

'FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream,'エラーの場合

移動先のnginxの設定が、直でアクセスしてるドメインじゃなくて、url欄に表示されてるドメインで、されてない可能性あり

sslの自動更新

有効期限が90日間しかないので自動更新する必要がある

以下で更新できるが、nginx, apacheなどを再読込しなければならないためcrontabによる手動更新が推奨される様子。

# ./certbot-auto renew

更新を手動で行いnginxの設定を再読込する

# crontab -e

全ドメインを更新するには

0 5 1 * * /usr/local/certbot/certbot-auto renew --force-renew && /etc/rc.d/init.d/nginx restart

個別ドメインを更新するには

0 5 1 * * /usr/local/certbot/certbot-auto certonly --webroot -w /var/www/example -d example.com --renew-by-default && /etc/rc.d/init.d/nginx restart

参考:https://blog.apar.jp/linux/3619/

注意

更新の部分はサーバに対応する読み込みコマンドを記述すること

0 5 1 * * /usr/local/certbot/certbot-auto renew --force-renew && /etc/rc.d/init.d/nginx restart
0 5 1 * * /usr/local/certbot/certbot-auto renew --force-renew && service nginx restart

proxyサーバとwebサーバのconfについて

example.comで以下設定だった場合

*proxyサーバから見たwebサーバ
example.webを/etc/hostsに設定
*proxyサーバのnginx設定
example.com.confを作りserver_nameをexample.comにしてrewrite先としてwebサーバにexample.webを指定する
*webサーバのngxin設定
server_nameをexample.comにする。(server_nameをexample.webに設定しても見に行かない)

httpからhttpsにする際のwebサイト変更箇所

  • httpsの自サイトから外部httpへリダイレクトしている箇所は修正が必要
  • scriptなどのhttp記述をhttpsへ書き直す
  • sitemap.xml,robots.txtなどのhttpを書き直す

ついでにhttp2化(nginx)

$ sudo vim /etc/yum.repos.d/nginx.repo
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=0
enabled=1

$ yum list nginx
Installed Packages
nginx.x86_64                                              1.8.0-1.el6.ngx                                               @nginx
Available Packages
nginx.x86_64                                              1.11.1-1.el6.ngx

$ sudo yum update nginx
Complete!
$ nginx -v
nginx version: nginx/1.11.1

参考:https://tsuchikazu.net/lets-encrypt-nginx/

実行時の権限警告

以下警告が出るとき

/usr/local/certbot/certbot-auto has insecure permissions!
To learn how to fix them, visit https://community.letsencrypt.org/t/certbot-auto-deployment-best-practices/91979/

以下実行

sudo chown root:root /usr/local/certbot/certbot-auto
sudo chmod 755 /usr/local/certbot/certbot-auto

サーバ引っ越し方法

証明書をごっそり持っていけば使える。証明書の作り直しは不要。 引越し先サーバのfirewallでhttpsのportを開放すれば何もすることはない。

セキュリティ評価サイト

https://www.ssllabs.com/ssltest/analyze.html

httpsにした段階でBでした。http2にしたらA-にあがりました。

参考