Android

Apache an toàn với mã hóa trên centos 7

How To Secure Apache with Let's Encrypt on CentOS 7

How To Secure Apache with Let's Encrypt on CentOS 7

Mục lục:

Anonim

Let Encrypt là cơ quan cấp chứng chỉ mở, tự động và miễn phí được phát triển bởi Nhóm nghiên cứu bảo mật Internet (ISRG). 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.

Trong hướng dẫn này, chúng tôi sẽ đề cập đến các bước cần thiết để cài đặt chứng chỉ SSL Encrypt SSL miễn phí trên máy chủ CentOS 7 chạy Apache dưới dạng máy chủ web. Chúng tôi sẽ sử dụng tiện ích certbot để lấy và gia hạn chứng chỉ Let Encrypt.

Đ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:

  • Có một 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 .Apache được cài đặt và chạy trên máy chủ của bạn. Lưu trữ máy chủ ảo Apache cho miền của bạn. Cổng 80 và 443 được mở trong tường lửa của bạn.

Cài đặt các gói sau đây được yêu cầu cho máy chủ web được mã hóa SSL:

yum install mod_ssl openssl

Cài đặt Certbot

Certbot là một công cụ đơn giản hóa quy trình lấy chứng chỉ SSL từ Let Encrypt và tự động kích hoạt HTTPS trên máy chủ của bạn.

Gói certbot có thể cài đặt được từ EPEL. Nếu kho EPEL không được cài đặt trên hệ thống của bạn, bạn có thể cài đặt nó bằng lệnh sau:

sudo yum install epel-release

Khi kho lưu trữ EPEL được bật, hãy cài đặt gói certbot bằng cách nhập:

sudo yum 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 để tăng cường bảo mật:

sudo openssl dhparam -out /etc/ssl/certs/dhparam.pem 2048 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 .

Chạy các lệnh sau để 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 apache /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/httpd/conf.d/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/httpd/conf.d/ssl-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

Đ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.

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

sudo systemctl reload

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-12-07. 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:

CentOS 7 xuất SSLOpenSSLConfCmd với phiên bản Apache 2.4.6, không bao gồm chỉ thị SSLOpenSSLConfCmd . Lệnh này chỉ có sẵn trên Apache 2.4.8 sau đó và nó được sử dụng để cấu hình các tham số OpenSSL như trao đổi khóa DiffieTHER Hellman (DH).

Chúng tôi sẽ phải tạo một tệp kết hợp mới bằng chứng chỉ SSL Encrypt SSL và tệp DH được tạo. Để làm điều này, gõ:

cat /etc/letsencrypt/live/example.com/cert.pem /etc/ssl/certs/dhparam.pem >/etc/letsencrypt/live/example.com/cert.dh.pem

Bây giờ mọi thứ đã được thiết lập, hãy chỉnh sửa cấu hình máy chủ ảo tên miền của bạn như sau:

/etc/httpd/conf.d/example.com

ServerName example.com ServerAlias www.example.com Redirect permanent / https://example.com/ ServerName example.com ServerAlias www.example.com Redirect permanent / https://example.com/ DocumentRoot /var/www/example.com/public_html ErrorLog /var/log/httpd/example.com-error.log CustomLog /var/log/httpd/example.com-access.log combined SSLEngine On SSLCertificateFile /etc/letsencrypt/live/example.com/cert.dh.pem SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem SSLCertificateChainFile /etc/letsencrypt/live/example.com/chain.pem # Other Apache Configuration ServerName example.com ServerAlias www.example.com Redirect permanent / https://example.com/ ServerName example.com ServerAlias www.example.com Redirect permanent / https://example.com/ DocumentRoot /var/www/example.com/public_html ErrorLog /var/log/httpd/example.com-error.log CustomLog /var/log/httpd/example.com-access.log combined SSLEngine On SSLCertificateFile /etc/letsencrypt/live/example.com/cert.dh.pem SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem SSLCertificateChainFile /etc/letsencrypt/live/example.com/chain.pem # Other Apache Configuration ServerName example.com ServerAlias www.example.com Redirect permanent / https://example.com/ ServerName example.com ServerAlias www.example.com Redirect permanent / https://example.com/ DocumentRoot /var/www/example.com/public_html ErrorLog /var/log/httpd/example.com-error.log CustomLog /var/log/httpd/example.com-access.log combined SSLEngine On SSLCertificateFile /etc/letsencrypt/live/example.com/cert.dh.pem SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem SSLCertificateChainFile /etc/letsencrypt/live/example.com/chain.pem # Other Apache Configuration ServerName example.com ServerAlias www.example.com Redirect permanent / https://example.com/ ServerName example.com ServerAlias www.example.com Redirect permanent / https://example.com/ DocumentRoot /var/www/example.com/public_html ErrorLog /var/log/httpd/example.com-error.log CustomLog /var/log/httpd/example.com-access.log combined SSLEngine On SSLCertificateFile /etc/letsencrypt/live/example.com/cert.dh.pem SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem SSLCertificateChainFile /etc/letsencrypt/live/example.com/chain.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.

Khởi động lại dịch vụ Apache để các thay đổi có hiệu lực:

sudo systemctl restart

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 chứng chỉ trước khi chúng hết hạn, chúng tôi sẽ tạo một cronjob sẽ 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.

Chạy lệnh crontab để tạo một cronjob mới sẽ gia hạn chứng chỉ, tạo một tệp kết hợp mới bao gồm khóa DH và khởi động lại apache:

sudo crontab -e

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

Lưu và đóng tập tin.

Để kiểm tra quá trình gia hạn, bạn có thể sử dụng lệnh certbot theo sau là công tắc --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 để tải 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 centos hãy mã hóa certbot ssl

Bài đăng này là một phần của Cài đặt LAMP Stack trên sê-ri CentOS 7.

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

• Cách cài đặt Apache trên CentOS 7 • Cài đặt MySQL trên CentOS 7 • Cách thiết lập máy chủ ảo Apache trên CentOS 7 • Bảo mật Apache với Encrypt trên CentOS 7