Cài đặt Apache2 trên Ubuntu 22.04: Cấu hình SSL/TLS

bởi | LPI, Ubuntu

Home » LPI » Ubuntu » Cài đặt Apache2 trên Ubuntu 22.04: Cấu hình SSL/TLS

Cấu hình SSL/TLS để sử dụng kết nối HTTPS mã hóa an toàn

Chuẩn bị

– Nhận SSL Certificates từ Let’s Encrypt nhà cung cấp SSL Certificates miễn phí như bài viết: Get SSL Certificates (Let’s Encrypt) trên Ubuntu 22.04

Kích hoạt cài đặt SSL/TLS

root@www:~# vi /etc/apache2/sites-available/default-ssl.conf
# line 3 : change admin email
ServerAdmin admin@dinhducthanh.com
# line 32,33 : thay đổi đường dẫn lưu file cert
SSLCertificateFile      /etc/letsencrypt/live/srv.dinhducthanh.com/cert.pem
SSLCertificateKeyFile   /etc/letsencrypt/live/srv.dinhducthanh.com/privkey.pem
# line 42 : bỏ ghi chú và thay đổi đường dẫn lưu chain-file
SSLCertificateChainFile /etc/letsencrypt/live/srv.dinhducthanh.com/chain.pem
root@www:~# a2ensite default-ssl
Enabling site default-ssl.
To activate the new configuration, you need to run:
  systemctl reload apache2

root@www:~# a2enmod ssl
Considering dependency setenvif for ssl:
Module setenvif already enabled
Considering dependency mime for ssl:
Module mime already enabled
Considering dependency socache_shmcb for ssl:
Enabling module socache_shmcb.
Enabling module ssl.
See /usr/share/doc/apache2/README.Debian.gz on how to configure SSL and create self-signed certificates.
To activate the new configuration, you need to run:
  systemctl restart apache2

root@www:~# systemctl restart apache2

Kích hoạt chuyển hướng HTTPS

– Nếu bạn muốn đặt kết nối HTTP chuyển hướng đến HTTPS (Luôn bật SSL/TLS), hãy đặt RewriteRule cho từng cài đặt Máy chủ.

– Ví dụ: nếu bạn đặt Virtual Hostings như dưới đây, Thêm RewriteRule như sau. Hoặc có thể đặt RewriteRule trong [.htaccess] không phải trong [httpd.conf].

root@www:~# vi /etc/apache2/sites-available/srv.dinhducthanh.com.conf

    DocumentRoot /var/www/srv.dinhducthanh.com
    ServerName svr.dinhducthanh.com
    ServerAdmin webmaster@srv.dinhducthanh.com
    ErrorLog /var/log/apache2/srv.dinhducthanh.com.error.log
    CustomLog /var/log/apache2/srv.dinhducthanh.com.log combined
    RewriteEngine On
    RewriteCond %{HTTPS} off
    RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

root@www:~# a2enmod rewrite
Enabling module rewrite.
To activate the new configuration, you need to run:
  systemctl restart apache2

root@www:~# systemctl restart apache2

Kiểm tra

– Để xác minh, truy cập vào trang kiểm tra từ bất kỳ máy khách nào có trình duyệt Web qua HTTPS.
Cài đặt Apache2 trên Ubuntu 22.04: Cấu hình SSL/TLS

Tham khảo thêm

Cài đặt Apache2 trên Ubuntu 22.04

Cài đặt DNS Server trên Ubuntu 22.04: External Network

Cài đặt DNS Server trên Ubuntu 22.04: Internal Network và External Network