介紹Let's Encrypt證明書取得流程並設定到EC2+Nginx伺服器。
# amazon-linux-extras install epel
# yum install epel-release
# yum install certbot python-certbot-apache
確認Certbot是否安裝成功
# certbot -h
透過AWS security group開放80(http), 443(https) port。
# certbot certonly --webroot -w /home/appuser/public_html/ -d hoge-staging.example.com
-w: 根目錄
-d: 網域名
在 /etc/nginx/conf.d/ 中,創建ssl.config檔案,並將以下內容填上。
{YOUR DOMAIN}:網域名稱 {YOUR SERVER NAME}:網路服務器名稱 {YOUR ROOT DIRECTORY}:根目錄名稱
server {
listen 443;
ssl on;
ssl_certificate /etc/letsencrypt/live/{YOUR DOMAIN}/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/{YOUR DOMAIN}/privkey.pem;
server_name {YOUR SERVER NAME};
port_in_redirect off;
root {YOUR ROOT DIRECTORY};
index index.php index.html index.htm;
#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;
access_log /var/log/nginx/access.log main;
error_log /var/log/nginx/error.log notice;
client_max_body_size 100M;
error_page 404 /404.html;
error_page 500 502 504 /500.html;
#location / {
#}
location / {
try_files $uri $uri/ /index.php$is_args$args;PUT, DELETE';
}
# redirect server error pages to the static page /40x.html
#
error_page 404 /404.html;
location = /40x.html {
}
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
fastcgi_pass unix:/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_read_timeout 180;
include fastcgi_params;
client_max_body_size 10M;
}
location ~* ^.+\.(jpg|js|jpeg|png|ico|gif|txt|js|css|swf|zip|rar|avi|exe|mpg|mp3|wav|mpeg|asf|wmv)$ {
try_files $uri /index.php$is_args$args;
expires 24h;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
用以下指令檢查設置檔案是否正確。
# nginx -t
以下指令可以更新證明書。
# certbot renew
// test
# certbot --dry-run renew
// force
# certbot --force-renewal renew
證明書的有效期限為三個月,可以透過cron來設置定時處理。
0 1 1 * * certbot renew --force-renewal && service nginx restart