Android

Bảo mật apache với mã hóa trên Ubuntu 18.04

How to Install Certbot and Generate Let’s Encrypt SSL/TLS Certificate on Ubuntu 20.04 18.04

How to Install Certbot and Generate Let’s Encrypt SSL/TLS Certificate on Ubuntu 20.04 18.04

Mục lục:

Anonim

Let Encrypt là một tổ chức chứng nhận được tạo bởi Nhóm nghiên cứu bảo mật Internet (ISRG). Nó cung cấp chứng chỉ SSL miễn phí thông qua quy trình hoàn toàn tự động được thiết kế để loại bỏ việc tạo, xác thực, cài đặt và gia hạn chứng chỉ thủ công.

Chứng chỉ do Let Encrypt cấp được tin cậy bởi tất cả các trình duyệt chính hiện nay.

Trong hướng dẫn này, chúng tôi sẽ cung cấp hướng dẫn từng bước về cách bảo mật Apache của bạn bằng Let Encrypt bằng công cụ certbot trên Ubuntu 18.04.

Điều kiện tiên quyết

Hãy chắc chắn rằng bạn đã đáp ứng các điều kiện tiên quyết sau đây trước khi tiếp tục với hướng dẫn này:

  • Tên miền trỏ đến IP máy chủ công cộng của bạn. Chúng tôi sẽ sử dụng example.com . Bạn đã cài đặt Apache với máy chủ ảo apache cho miền của mình.

Cài đặt Certbot

Certbot là một công cụ đầy đủ tính năng và dễ sử dụng, có thể tự động hóa các tác vụ để lấy và làm mới Chứng chỉ SSL mã hóa và định cấu hình máy chủ web. Gói certbot được bao gồm trong kho Ubuntu mặc định.

Cập nhật danh sách các gói và cài đặt gói certbot:

sudo apt update sudo apt install certbot

Tạo nhóm Strong Dh (Diffie-Hellman)

Trao đổi khóa Diffieifer Hellman (DH) là một phương pháp trao đổi khóa mật mã an toàn qua một kênh liên lạc không bảo mật. Chúng tôi sẽ tạo một bộ thông số DH 2048 bit mới để tăng cường bảo mật:

sudo openssl dhparam -out /etc/ssl/certs/dhparam.pem 2048 Nếu bạn muốn, bạn có thể thay đổi kích thước lên tới 4096 bit, nhưng trong trường hợp đó, việc tạo có thể mất hơn 30 phút tùy thuộc vào entropy của hệ thống.

Nhận chứng chỉ SSL Hãy mã hóa

Để có được chứng chỉ SSL cho tên miền, chúng tôi sẽ sử dụng plugin Webroot hoạt động bằng cách tạo một tệp tạm thời để xác thực tên miền được yêu cầu trong thư mục ${webroot-path}/.well-known/acme-challenge . Máy chủ Let Encrypt tạo các yêu cầu HTTP đến tệp tạm thời để xác thực rằng miền được yêu cầu phân giải đến máy chủ nơi certbot chạy.

Để đơn giản hơn, chúng tôi sẽ ánh xạ tất cả các yêu cầu HTTP cho .well-known/acme-challenge vào một thư mục duy nhất, /var/lib/letsencrypt .

Các lệnh sau sẽ tạo thư mục và làm cho nó có thể ghi được cho máy chủ Apache.

sudo mkdir -p /var/lib/letsencrypt/.well-known sudo chgrp www-data /var/lib/letsencrypt sudo chmod g+s /var/lib/letsencrypt

Để tránh trùng lặp mã, hãy tạo hai đoạn cấu hình sau:

/etc/apache2/conf-av Available /letsencrypt.conf

Alias /.well-known/acme-challenge/ "/var/lib/letsencrypt/.well-known/acme-challenge/" AllowOverride None Options MultiViews Indexes SymLinksIfOwnerMatch IncludesNoExec Require method GET POST OPTIONS Alias /.well-known/acme-challenge/ "/var/lib/letsencrypt/.well-known/acme-challenge/" AllowOverride None Options MultiViews Indexes SymLinksIfOwnerMatch IncludesNoExec Require method GET POST OPTIONS /etc/apache2/conf-av Available / sss-params.conf

SSLCipherSuite EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH SSLProtocol All -SSLv2 -SSLv3 -TLSv1 -TLSv1.1 SSLHonorCipherOrder On Header always set Strict-Transport-Security "max-age=63072000; includeSubDomains; preload" Header always set X-Frame-Options DENY Header always set X-Content-Type-Options nosniff # Requires Apache >= 2.4 SSLCompression off SSLUseStapling on SSLStaplingCache "shmcb:logs/stapling-cache(150000)" # Requires Apache >= 2.4.11 SSLSessionTickets Off SSLOpenSSLConfCmd DHParameters "/etc/ssl/certs/dhparam.pem"

Đoạn mã ở trên đang sử dụng các trình cắt được đề xuất bởi Codesli.st, cho phép OCSP Stapling, HTTP Strict Transport Security (HSTS) và thực thi một vài tiêu đề HTTP tập trung vào bảo mật.

Trước khi bật các tệp cấu hình, hãy đảm bảo cả mod_ssl mod_headers đều được bật bằng cách phát hành:

sudo a2enmod ssl sudo a2enmod headers

Tiếp theo, kích hoạt các tệp cấu hình SSL bằng cách chạy các lệnh sau:

sudo a2enconf letsencrypt sudo a2enconf ssl-params

Kích hoạt mô-đun HTTP / 2, giúp trang web của bạn nhanh hơn và mạnh hơn:

sudo a2enmod

Tải lại cấu hình Apache để các thay đổi có hiệu lực:

sudo systemctl reload apache2

Bây giờ, chúng ta có thể chạy công cụ Certbot với plugin webroot và lấy các tệp chứng chỉ SSL bằng cách nhập:

sudo certbot certonly --agree-tos --email [email protected] --webroot -w /var/lib/letsencrypt/ -d example.com -d www.example.com

Nếu chứng nhận SSL được lấy thành công, certbot sẽ in thông báo sau:

IMPORTANT NOTES: - Congratulations! Your certificate and chain have been saved at: /etc/letsencrypt/live/example.com/fullchain.pem Your key file has been saved at: /etc/letsencrypt/live/example.com/privkey.pem Your cert will expire on 2018-10-28. To obtain a new or tweaked version of this certificate in the future, simply run certbot again. To non-interactively renew *all* of your certificates, run "certbot renew" - If you like Certbot, please consider supporting our work by: Donating to ISRG / Let's Encrypt: https://letsencrypt.org/donate Donating to EFF:

Bây giờ bạn đã có các tệp chứng chỉ, chỉnh sửa cấu hình máy chủ ảo tên miền của bạn như sau:

/etc/apache2/sites-av Available / example.com

ServerName example.com ServerAlias www.example.com Redirect permanent / https://example.com/ ServerName example.com ServerAlias www.example.com Protocols h2 http:/1.1 Redirect permanent / https://example.com/ DocumentRoot /var/www/example.com/public_html ErrorLog ${APACHE_LOG_DIR}/example.com-error.log CustomLog ${APACHE_LOG_DIR}/example.com-access.log combined SSLEngine On SSLCertificateFile /etc/letsencrypt/live/example.com/fullchain.pem SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem # Other Apache Configuration ServerName example.com ServerAlias www.example.com Redirect permanent / https://example.com/ ServerName example.com ServerAlias www.example.com Protocols h2 http:/1.1 Redirect permanent / https://example.com/ DocumentRoot /var/www/example.com/public_html ErrorLog ${APACHE_LOG_DIR}/example.com-error.log CustomLog ${APACHE_LOG_DIR}/example.com-access.log combined SSLEngine On SSLCertificateFile /etc/letsencrypt/live/example.com/fullchain.pem SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem # Other Apache Configuration ServerName example.com ServerAlias www.example.com Redirect permanent / https://example.com/ ServerName example.com ServerAlias www.example.com Protocols h2 http:/1.1 Redirect permanent / https://example.com/ DocumentRoot /var/www/example.com/public_html ErrorLog ${APACHE_LOG_DIR}/example.com-error.log CustomLog ${APACHE_LOG_DIR}/example.com-access.log combined SSLEngine On SSLCertificateFile /etc/letsencrypt/live/example.com/fullchain.pem SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem # Other Apache Configuration ServerName example.com ServerAlias www.example.com Redirect permanent / https://example.com/ ServerName example.com ServerAlias www.example.com Protocols h2 http:/1.1 Redirect permanent / https://example.com/ DocumentRoot /var/www/example.com/public_html ErrorLog ${APACHE_LOG_DIR}/example.com-error.log CustomLog ${APACHE_LOG_DIR}/example.com-access.log combined SSLEngine On SSLCertificateFile /etc/letsencrypt/live/example.com/fullchain.pem SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem # Other Apache Configuration

Với cấu hình ở trên, chúng tôi đang buộc HTTPS và chuyển hướng từ www sang phiên bản không có www. Được miễn phí để điều chỉnh cấu hình theo nhu cầu của bạn.

Tải lại dịch vụ Apache để các thay đổi có hiệu lực:

sudo systemctl reload apache2

Bây giờ bạn có thể mở trang web của mình bằng https:// và bạn sẽ thấy biểu tượng khóa màu xanh lá cây.

Tự động gia hạn Hãy mã hóa chứng chỉ SSL

Chứng chỉ của Encrypt có hiệu lực trong 90 ngày. Để tự động gia hạn các chứng chỉ trước khi chúng hết hạn, gói certbot tạo ra một cronjob chạy hai lần một ngày và tự động gia hạn bất kỳ chứng chỉ nào 30 ngày trước khi hết hạn.

Khi chứng chỉ được gia hạn, chúng tôi cũng phải tải lại dịch vụ Apache. Nối --renew-hook "systemctl reload apache2" vào tệp /etc/cron.d/certbot để nó trông giống như sau:

/etc/cron.d/certbot

0 */12 * * * root test -x /usr/bin/certbot -a \! -d /run/systemd/system && perl -e 'sleep int(rand(3600))' && certbot -q renew --renew-hook "systemctl reload apache2"

Để kiểm tra quá trình gia hạn, bạn có thể sử dụng công tắc certbot --dry-run :

sudo certbot renew --dry-run

Nếu không có lỗi, điều đó có nghĩa là quá trình gia hạn đã thành công.

Phần kết luận

Trong hướng dẫn này, bạn đã sử dụng chứng chỉ ứng dụng khách Encrypt của khách hàng, để tải xuống chứng chỉ SSL cho tên miền của bạn. Bạn cũng đã tạo các đoạn mã Apache để tránh sao chép mã và định cấu hình Apache để sử dụng các chứng chỉ. Vào cuối hướng dẫn, bạn đã thiết lập một cronjob để gia hạn chứng chỉ tự động.

apache ub Ubuntu hãy mã hóa certbot ssl

Bài đăng này là một phần của loạt bài hướng dẫn cách cài đặt đèn-stack-on-ub Ubuntu-18-04.

Các bài viết khác trong loạt bài này:

• Cách cài đặt Apache trên Ubuntu 18.04 • Cách thiết lập máy chủ ảo Apache trên Ubuntu 18.04 • Bảo mật Apache bằng mã hóa trên Ubuntu 18.04 • Cách cài đặt MySQL trên Ubuntu 18.04 • Cách cài đặt PHP trên Ubuntu 18.04