Android

Apache an toàn với mã hóa trên debian 9

Install SSL HTTPs Let's Encrypt pada Apache di Linux Debian Server

Install SSL HTTPs Let's Encrypt pada Apache di Linux Debian Server

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ó giá trị trong 90 ngày kể từ ngày phát hành và được tất cả các trình duyệt chính tin cậy ngày hôm nay.

Hướng dẫn này sẽ hướng dẫn bạn trong quá trình nhận Mã hóa Let miễn phí bằng công cụ certbot trên Debian 9. Chúng tôi cũng sẽ trình bày cách định cấu hình Apache để sử dụng chứng chỉ SSL mới và bật HTTP / 2.

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

Đảm bảo 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:

  • Đăng nhập với tư cách người dùng với các đặc quyền sudo. Có một tên miền trỏ đến IP máy chủ công cộng của máy chủ của bạn. Chúng tôi sẽ sử dụng example.com .Apache được cài đặt. Một máy chủ ảo apache cho tên miền của bạn. Bạn có thể làm theo các hướng dẫn này để biết chi tiết về cách tạo một cái.

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à gia hạn chứng chỉ SSL Hãy mã hóa. Gói certbot được bao gồm trong kho Debian mặc định.

Cập nhật danh sách các gói và cài đặt gói certbot bằng các lệnh sau:

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.

Để tạo một bộ tham số DH 2048 bit mới chạy:

sudo openssl dhparam -out /etc/ssl/certs/dhparam.pem 2048 Nếu bạn thích, 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 của chúng tôi, 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 tạo thư mục và làm cho nó có thể ghi 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

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

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

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

sudo systemctl reload apache2

Sử dụng công cụ Certbot với plugin webroot để lấy các tệp chứng chỉ SSL:

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: IMPORTANT NOTES: - Congratulations! Your certificate and chain have been saved at /etc/letsencrypt/live/example.com/fullchain.pem. Your cert will expire on 2019-01-17. 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 lose your account credentials, you can recover through e-mails sent to [email protected]. - 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: 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

Mở trang web của bạn 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 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à sẽ 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, hãy 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, để lấy chứng chỉ SSL cho miền của mình. 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 debian hãy mã hóa certbot ssl

Bài đăng này là một phần của Cách cài đặt LAMP Stack trên loạt Debian 9.

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

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