Skip to main content

Cài đặt và cấu hình let's encrypt trên linux server

17,April 2018
cau hinh let's encrypt cho nginx

SSL hay HTTPS trong tương lai sẽ là giao thức chuẩn, khi các trình duyệt đang có những cải tiến để giúp người dùng dễ dàng phân biệt được website nào được bảo mật, website nào không. Let’s Encrypt là một nhà cung cấp chứng chỉ số SSL (Certificate Authority) hoàn toàn miễn phí, được nhiều cá nhân và tổ chức tin dùng và đặc biệt không giới hạn.

1. Cài đặt Certbot's Nginx package

+ Ubuntu 16

sudo add-apt-repository ppa:certbot/certbot
sudo apt-get update
sudo apt-get install python-certbot-nginx

+ CentOS 7

sudo yum -y install python-certbot-nginx


2. Tạo certificate cho domain 

sudo certbot --nginx -d

Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator nginx, Installer nginx
Enter email address (used for urgent renewal and security notices) (Enter 'c' to
Starting new HTTPS connection (1):

Please read the Terms of Service at You must
agree in order to register with the ACME server at
(A)gree/(C)ancel: A

Would you be willing to share your email address with the Electronic Frontier
Foundation, a founding partner of the Let's Encrypt project and the non-profit
organization that develops Certbot? We'd like to send you email about EFF and
our work to encrypt the web, protect its users and defend digital rights.
(Y)es/(N)o: N
Obtaining a new certificate
Performing the following challenges:
http-01 challenge for
Waiting for verification...
Cleaning up challenges
Deploying Certificate to VirtualHost /etc/nginx/conf.d/

Please choose whether or not to redirect HTTP traffic to HTTPS, removing HTTP access.
1: No redirect - Make no further changes to the webserver configuration.
2: Redirect - Make all requests redirect to secure HTTPS access. Choose this for
new sites, or if you're confident your site works on HTTPS. You can undo this
change by editing your web server's configuration.
Select the appropriate number [1-2] then [enter] (press 'c' to cancel): 2
Redirecting all traffic on port 80 to ssl in /etc/nginx/conf.d/

Congratulations! You have successfully enabled

You should test your configuration at:

 - Congratulations! Your certificate and chain have been saved at:
   Your key file has been saved at:
   Your cert will expire on 2018-07-16. To obtain a new or tweaked
   version of this certificate in the future, simply run certbot again
   with the "certonly" option. To non-interactively renew *all* of
   your certificates, run "certbot renew"
 - Your account credentials have been saved in your Certbot
   configuration directory at /etc/letsencrypt. You should make a
   secure backup of this folder now. This configuration directory will
   also contain certificates and private keys obtained by Certbot so
   making regular backups of this folder is ideal.
 - If you like Certbot, please consider supporting our work by:

   Donating to ISRG / Let's Encrypt:
   Donating to EFF:          


3. Cấu hình sử dụng SSL trên nginx config

Certbot sẽ tự động tìm đến block "server" trong nginx config tương ứng với domain bạn tạo certificate sau thêm cấu hình SSL như dưới

sudo vim /etc/nginx/conf.d/

server {
    root  /var/www/itlabvn;
    access_log /var/log/nginx/itlabvn_access.log;
    error_log  /var/log/nginx/itlabvn_error.log;
    ...    ...    ...    ...    ...    ....

    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

    ...    ...    ...    ...    ...    ....
server {
    if ($host = {
        return 301 https://$host$request_uri;
    } # managed by Certbot

    listen  80;
    return 404; # managed by Certbot

+ Khởi động lại nginx service

sudo nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

sudo systemctl nginx restart 

+ Kiểm tra certification

sudo openssl x509 -dates -noout -in /etc/letsencrypt/live/

notBefore=Apr 17 07:13:26 2018 GMT
notAfter=Jul 16 07:13:26 2018 GMT


4. Verifying Certbot Auto-Renewal

- To test the renewal process, you can do a dry run with certbot:

sudo certbot renew --dry-run 

- To execute renew certificate

sudo certbot renew

The following certs are not due for renewal yet:
  /etc/letsencrypt/live/ expires on 2018-10-04 (skipped)
Congratulations, all renewals succeeded. The following certs have been renewed:
  /etc/letsencrypt/live/ (success)

- Tạo crontab tự động gia hạn certificate

crontab -e

00 04 */2 * * certbot renew --dry-run  --post-hook "systemctl restart nginx" 1 > /dev/null 2 > /dev/null

- Kiểm tra

# openssl x509 -enddate -noout -in /etc/letsencrypt/live/
notAfter=Oct 17 06:40:57 2018 GMT
# openssl x509 -dates -noout -in /etc/letsencrypt/live/
notBefore=Jul 19 06:40:57 2018 GMT
notAfter=Oct 17 06:40:57 2018 GMT