Let's Encrypt เป็นฟรี SSL ที่ทำให้เว็บมี HTTPS เป็นของตัวเองไม่พูดพร่ำทำเพลง มาเริ่มวิธีติดตั้งสำหรับ Nginx บน CentOS 7/6 เลยแล้วกันครับ ตรงนี้จะมีแทรกพวก Path Config เล็กน้อย เพราะว่าตัวผมเองใช้ VESTACP พวกที่เก็บไฟล์โฟลเดอร์อาจจะมีต่างบ้าง แต่จะยังคง Path เดิมของ Nginx
วิธีติดตั้ง Let's Encrypt
1. ถ้าไม่มี git ให้ติดตั้งก่อนเลย
yum install git
2. เมื่อติดตั้ง git เสร็จให้เข้าโฟลเดอร์ /opt และ clone letsencrypt มาไว้ใน server
cd /opt
git clone https://github.com/letsencrypt/letsencrypt
3. ให้เข้าโฟลเดอร์ /opt/letsencrypt เพื่อเริ่มให้ Let's Encrypt สร้างใบรับรองให้กับ Domain ของเรา (อย่าลืมเปลี่ยน path ที่เก็บเว็บและชื่อเว็บ)
cd /opt/letsencrypt
./letsencrypt-auto certonly -a webroot --webroot-path=/usr/share/nginx/html -d example.com -d www.example.com
4. จะมีหน้าจอสีน้ำเงินให้กรอกอีเมล์ของเรา และปุ่ม Agree ยอมรับข้อตกลง ก็ทำตามขั้นตอนไป ถ้าสำเร็จจะได้รูปแบบประมาณนี้
IMPORTANT NOTES:
- Congratulations! Your certificate and chain have been saved at
/etc/letsencrypt/live/example.com/fullchain.pem.
Your cert will expire on 2017-03-31.
5. ตรวจสอบว่าได้สร้างไฟล์ Certificate แล้วหรือยัง
ls /etc/letsencrypt/live/
ls -al /etc/letsencrypt/live/example.com
ถ้ามีจะแสดงไฟล์ Certificate ประมาณนี้
lrwxrwxrwx cert.pem -> ../../archive/example.com/cert1.pem
lrwxrwxrwx chain.pem -> ../../archive/example.com/chain1.pem
lrwxrwxrwx fullchain.pem -> ../../archive/example.com/fullchain1.pem
lrwxrwxrwx privkey.pem -> ../../archive/example.com/privkey1.pem
6. ทำการติดตั้งใน Nginx และเปิดการใช้งาน SSL ให้เข้าไปที่ไฟล์ /etc/nginx/nginx.conf
vi /etc/nginx/nginx.conf
สำหรับใครใช้ VESTACP path จะเป็นลักษณะนี้
/home/$user/conf/web/nginx.conf(แก้ไข$userตาม Server ของคุณ)
ทำการแก้ไข/เพิ่ม SSL configuration ใน Server block ตามด้านล่างนี้ (เปลี่ยน path ให้ตรงกับที่สร้างมาด้านบน)
# SSL configuration
listen 443 ssl default_server;
ssl_certificate /etc/letsencrypt/live/your_domain.tld/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/your_domain.tld/privkey.pem;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
ssl_ciphers 'EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH';
# SSL configuration
ทำการ Reload Nginx
service nginx reload
วิธีต่ออายุ Let's Encrypt อัตโนมัติ
เนื่องจาก Cert นั้นมีอายุ 90 วัน ดังนั้นหากเรามาต่ออายุทุกครั้งอาจจะไม่สะดวกนัก จึงต้องมีวิธีการต่ออายุอัตโนมัติดังนี้
1. เริ่มต้นทำการสร้างการต่ออายุด้วยคำสั่งดังนี้ (เปลี่ยน webroot-path และชื่อเว็บให้ตรงกับคุณ)
cd /opt/letsencrypt
./letsencrypt-auto certonly -a webroot --agree-tos --renew-by-default --webroot-path=/usr/share/nginx/html/ -d www.example.com -d example.com
2. ทำการสร้าง bash script ไฟล์ สำหรับตรวจสอบวันหมดอายุและต่ออายุอัตโนมัติ
vi /usr/local/bin/cert-renew
ให้เพิ่ม script ตามด้านล่างนี้ (เปลี่ยนที่อยู่ตัวแปร $webpath)
#!/bin/bash
webpath='/usr/share/nginx/html/'
domain=$1
le_path='/opt/letsencrypt'
le_conf='/etc/letsencrypt'
exp_limit=30;
get_domain_list(){
certdomain=$1
config_file="$le_conf/renewal/$certdomain.conf"
if [ ! -f $config_file ] ; then
echo "[ERROR] The config file for the certificate $certdomain was not found."
exit 1;
fi
domains=$(grep --only-matching --perl-regex "(?<=domains \= ).*" "${config_file}")
last_char=$(echo "${domains}" | awk '{print substr($0,length,1)}')
if [ "${last_char}" = "," ]; then
domains=$(echo "${domains}" |awk '{print substr($0, 1, length-1)}')
fi
echo $domains;
}
if [ -z "$domain" ] ; then
echo "[ERROR] you must provide the domain name for the certificate renewal."
exit 1;
fi
cert_file="/etc/letsencrypt/live/$domain/fullchain.pem"
if [ ! -f $cert_file ]; then
echo "[ERROR] certificate file not found for domain $domain."
exit 1;
fi
exp=$(date -d "`openssl x509 -in $cert_file -text -noout|grep "Not After"|cut -c 25-`" +%s)
datenow=$(date -d "now" +%s)
days_exp=$(echo \( $exp - $datenow \) / 86400 |bc)
echo "Checking expiration date for $domain..."
if [ "$days_exp" -gt "$exp_limit" ] ; then
echo "The certificate is up to date, no need for renewal ($days_exp days left)."
exit 0;
else
echo "The certificate for $domain is about to expire soon. Starting renewal request..."
domain_list=$( get_domain_list $domain )
"$le_path"/letsencrypt-auto certonly -a webroot --agree-tos --renew-by-default --webroot-path="$webpath" --domains "${domain_list}"
echo "Reloading Nginx..."
sudo systemctl reload nginx
echo "Renewal process finished for domain $domain"
exit 0;
fi
3. เพื่อให้แน่ใจว่า script ทำงานได้ และ bc calculator ได้ถูกติดตั้งแล้ว
chmod +x /usr/local/bin/cert-renew
yum install bc
ลองทดสอบการทำงาน
/usr/local/bin/cert-renew example.com
ผลลัพท์จะได้ประมาณนี้
Checking expiration date for example.com...
The certificate is up to date, no need for renewal (89 days left).
4. และสุดท้ายทำการตั้ง Cron job เพื่อให้ทำงาน Script ตรวจสอบการหมดอายุและต่ออายุทุกสัปดาห์
crontab -e
เพิ่มคำสั่งตามด้านล่างนี้
@weekly /usr/local/bin/cert-renew example.com >> /var/log/example.com-renew.log 2>&1
จากนั้นสั่ง restart crontab
service crond restart
และลองแสดงรายการที่ตั้งเวลาไว้ใน crontab อีกครั้ง
crontab -l
เท่านี้เราก็จะมี SSL จาก Let's Encrypt ใช้ฟรีได้แล้ว ลองเข้าเว็บ https://www.sslshopper.com/ssl-checker.html เพื่อตรวจสอบ SSL certificate อีกครั้งว่าสามารถใช้ HTTPS ได้อย่างสมบูรณ์
สำหรับบทความนี้หวังว่าคงมีประโยชน์กับทุกท่านนะครับ
อ้างอิง: http://www.tecmint.com/setup-https-with-lets-encrypt-ssl-certificate-for-nginx-on-centos/