Cài đặt Ssl Let’s Encrypt miễn phí cho website https trên Centos

Nếu mới build website lần đầu có thể bạn chưa quan tâm lắm vấn đề về SSL hay HTTPS, nhưng lâu dần bạn sẽ cần phải think đến việc này. Hiện nay các trình duyệt có xu hướng khuyến cáo nhà phát triển website nên sử dụng giao thức https thay vì http thông thường cho website.

HTTPS là gì?

Https là tên viết tắt của Hypertext Transfer Protocol Secure, nó là một giao thức kết hợp giữa giao thức HTTP thông thường và giao thức bảo mật SSL hay TLS cho phép truyền tải thông tin một cách bảo mật giữa client và server trên Internet.

Tham khảo thêm chi tiết Https là gì?

SSL là gì?

SSL là viết tắt của Secure Sockets Layer (tầng socket bảo mật, hay lớp socket bảo mật), nó là các giao thức mã hóa được thiết kế nhằm cung cấp giải pháp truyền tải dữ liệu 1 cách an toàn và bảo mật giữa client và server trên Internet.

Tham khảo thêm chi tiết SSL là gì?

Cài đặt SSL Let's Encrypt

Cài đặt SSL Let’s Encrypt

Let’s Encrypt là gì?

Let’s Encrypt là một tổ chức chứng chỉ SSL phi lợi nhuận được điều hành bởi Nhóm nghiên cứu bảo mật Internet (ISRG), cung cấp chứng chỉ X.509 cho mã hóa TLS (Transport Layer Security) miễn phí vì cộng đồng (tương tự codezi.pro cũng ra đời vì mục đích vì cộng đồng).

Cách cài SSL Let’s Encrypt

Yêu cầu cần có

  • Hệ điều hành Centos hoặc bản phân phối Linux tương đương
  • Cài đặt service nginx: tham khảo tại đây
  • Cài git thì càng tốt (ở bài này tôi hướng dẫn qua cách clone từ github)
  • Trỏ domain về IP của server (hãy đảm bảo chắc chắn Record A hoặc CNAME

Các bước cài SSL Let’s Encrypt

Clone source code của Let’s Encrypt từ github về theo lệnh sau

git clone https://github.com/letsencrypt/letsencrypt

Cd đến thư mục letsencrypt vừa tải về

cd letsencrypt

Hãy đảm bảo bạn đã stop service nginx trên server của bạn

service nginx stop

Sau đó thực hiện lệnh sau

./letsencrypt-auto --server https://acme-v02.api.letsencrypt.org/directory auth

Trong 1 số trường hợp có thể bạn sẽ cần thêm tham số –debug (ví dụ trường hợp như codezi từng thử cài test trên máy MacOs. (nếu k cần thiết thì k cần chạy lệnh dưới đây)

./letsencrypt-auto --debug --server https://acme-v02.api.letsencrypt.org/directory auth

Trên terminal của bạn sẽ có output như sau

[[email protected] letsencrypt]# ./letsencrypt-auto --server https://acme-v02.api.letsencrypt.org/directory auth
Upgrading certbot-auto 0.32.0 to 0.34.2...
Replacing certbot-auto...
Creating virtual environment...
Installing Python packages...
Installation succeeded.
Saving debug log to /var/log/letsencrypt/letsencrypt.log

How would you like to authenticate with the ACME CA?
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1: Apache Web Server plugin (apache)
2: Nginx Web Server plugin (nginx)
3: Spin up a temporary webserver (standalone)
4: Place files in webroot directory (webroot)
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Select the appropriate number [1-4] then [enter] (press 'c' to cancel): 

Hãy chọn 3 – Spin up a temporary webserver (standalone) để tiếp tục

Tiếp theo hệ thống sẽ yêu cầu bạn nhập tên domain bạn cần cài đặt https, bạn sẽ nhận được kết quả

Select the appropriate number [1-4] then [enter] (press 'c' to cancel): 3
Plugins selected: Authenticator standalone, Installer None
Please enter in your domain name(s) (comma and/or space separated)  (Enter 'c'
to cancel): codezi.pro
Obtaining a new certificate
Performing the following challenges:
http-01 challenge for codezi.pro
Cleaning up challenges
Problem binding to port 80: Could not bind to IPv4 or IPv6.

Nếu bạn nhận được kết quả thông báo như bên trên Could not bind to IPv4 or IPv6 khi cài ssl let’s encrypt thì đồng nghĩa với việc port 80 của bạn đang được sử dụng bởi 1 ứng dụng khác, rất có thể là nginx (ở đây của tôi là do chưa stop nginx trước khi cài) => trong trường hợp này hãy stop nginx hoặc tìm ứng dụng nào đang dùng port 80 và kill đi rồi thao tác lại là OK

Hoặc cũng có thể phát sinh lỗi khác như domain của bạn trỏ không đúng đến server này, hoặc record trên phần cài đặt DNS domain bị sai, và khi đó sẽ nhận được thông báo như sau:

IMPORTANT NOTES:
 - The following errors were reported by the server:

   Domain: codezi.pro
   Type:   connection
   Detail: dns :: DNS problem: NXDOMAIN looking up A for codezi.pro

   To fix these errors, please make sure that your domain name was
   entered correctly and the DNS A/AAAA record(s) for that domain
   contain(s) the right IP address. Additionally, please check that
   your computer has a publicly routable IP address and that no
   firewalls are preventing the server from communicating with the
   client. If you're using the webroot plugin, you should also verify
   that you are serving files from the webroot path you provided.

Trong trường hợp thuận lợi thì đến đây bạn sẽ nhận được thông báo thành công, với nội dung như sau

Select the appropriate number [1-4] then [enter] (press 'c' to cancel): 3
Plugins selected: Authenticator standalone, Installer None
Please enter in your domain name(s) (comma and/or space separated)  (Enter 'c'
to cancel): codezi.pro
Obtaining a new certificate
Performing the following challenges:
http-01 challenge for codezi.pro
Cleaning up challenges
Problem binding to port 80: Could not bind to IPv4 or IPv6.

Please enter in your domain name(s) (comma and/or space separated)  (Enter 'c'
to cancel): codezi.pro
Obtaining a new certificate
Performing the following challenges:
http-01 challenge for codezi.pro
Waiting for verification...
Cleaning up challenges

IMPORTANT NOTES:
 - Congratulations! Your certificate and chain have been saved at:
   /etc/letsencrypt/live/codezi.pro/fullchain.pem
   Your key file has been saved at:
   /etc/letsencrypt/live/codezi.pro/privkey.pem
   Your cert will expire on 2019-08-08. To obtain a new or tweaked
   version of this certificate in the future, simply run
   letsencrypt-auto again. To non-interactively renew *all* of your
   certificates, run "letsencrypt-auto renew"
 - If you like Certbot, please consider supporting our work by:

   Donating to ISRG / Let's Encrypt:   https://letsencrypt.org/donate
   Donating to EFF:                    https://eff.org/donate-le

Dến đây là bạn đã cài thành công SSL miễn phí cho website của bạn.

Cấu hình SSL với nginx

Việc tiếp theo là config lại phần virtual host của nginx của domain này sang port 443 là ok. Hãy tạo file conf trong thư mục /etc/nginx/conf.d

nano /etc/nginx/conf.d/codezi.pro.conf

Nội dung file chi tiết như sau

server {
    listen 80;
    server_name codezi.pro www.codezi.pro;
    rewrite ^(.*) https://codezi.pro$1 permanent;
}
server {
    listen 443 ssl http2;
    ssl_certificate /etc/letsencrypt/live/codezi.pro/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/codezi.pro/privkey.pem;
    ssl_session_cache shared:SSL:10m;
    ssl_session_timeout 10m;
    ssl_prefer_server_ciphers on;
    ssl_protocols       TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers         HIGH:!aNULL:!MD5;
    access_log off;
    error_log off;
    root /home/codezi.pro;
    index index.php index.html index.htm;
    server_name codezi.pro;

    location / {
          try_files $uri $uri/ /index.php?$query_string;
    }

    location ~ \.php$ {
                fastcgi_split_path_info ^(.+\.php)(/.+)$;
                include /etc/nginx/fastcgi_params;
                fastcgi_split_path_info ^(.+\.php)(/.+)$;
                include /etc/nginx/fastcgi_params;
                fastcgi_pass 127.0.0.1:9000;
                fastcgi_index index.php;
                fastcgi_connect_timeout 60;
                fastcgi_send_timeout 180;
                fastcgi_read_timeout 180;
                fastcgi_buffer_size 256k;
                fastcgi_buffers 4 256k;
                fastcgi_busy_buffers_size 256k;
                fastcgi_temp_file_write_size 256k;
                fastcgi_intercept_errors on;
                fastcgi_param    SCRIPT_FILENAME $document_root$fastcgi_script_name;
        }
}

Khởi động lại service nginx và trải nghiệm

service nginx start

Ở trong file config virtual host ở trên có 2 khối lệnh, phần block server thứ nhất có tác dụng điều hướng 301 redirect về https nếu người dùng cố gắng truy cập http, bạn hãy nhớ thực hiện việc này.

Hướng dẫn renew SSl Let’s Encrypt

Việc renew thực hiện rất đơn giản, renew bằng tay có thể thực hiện lệnh sau

./letsencrypt-auto renew

Hoặc nếu có phát sinh lỗi nào đó có thể thử lệnh sau

./letsencrypt-auto renew --force-renew

Bạn cũng có thể sử dụng file sh và paste lệnh trên, sau đó các bạn setup time chạy vào ngày 20 của mỗi tháng hoặc mỗi tuần chạy 1 phát để check

Đấy, thế là xong rồi đấy, làm thì có tí mà trình bày thì lâu v~. Hãy chia sẻ bài viết này nếu bạn thấy nó có ích cho bạn và cộng đồng

Cách 2 bạn có thể cài Certbot

Cài certbot nginx với lệnh sau

sudo yum install certbot-nginx

Tiếp theo đó hãy đảm bảo cài đặt nginx lên server của bạn

Rồi cập nhật firewall

sudo firewall-cmd --add-service=http
sudo firewall-cmd --add-service=https
sudo firewall-cmd --runtime-to-permanent

Đảm bảo mở port 443 và 80 với lệnh sau

sudo iptables -I INPUT -p tcp -m tcp --dport 80 -j ACCEPT
sudo iptables -I INPUT -p tcp -m tcp --dport 443 -j ACCEPT

Cuối cùng tiến hành cài ssl cho domain với lệnh sau

sudo certbot --nginx -d example.com -d www.example.com

Related posts:

  1. Cài đặt LEMP với PHP7.2 trên Centos 7
  2. Hướng dẫn cài đặt LAMP với PHP 7 trên CentOS 7
  3. Hướng dẫn sử dụng lệnh Create Database trong PostgreSQL