rbsongcan.store

d4Studio | Phật học

Cài đặt và cấu hình HTTPS cho Apache service

  • 11 Tháng 9, 2021 16:12:32

  • Bắt đầu cài đặt apache theo các bước sau:

    1. apt-get update
    2. apt-get install apache2 openssl
    3. a2enmod ssl
    4. a2enmod rewrite
    5. vim /etc/apache2/apache2.conf
    6. Thêm mấy dòng sau vào file conf:
    <Directory /var/www/html>
    AllowOverride All
    </Directory>
    1. Tạo private key và certificate (public key):
    2. mkdir /etc/apache2/certificate
    3. cd /etc/apache2/certificate
    4. openssl req -new -newkey rsa:4096 -x509 -sha256 -days 365 -nodes -out apache-certificate.crt -keyout apache.key
    Generating a RSA private key
    ............++++
    .......................................................++++
    writing new private key to 'apache.key'
    -----
    You are about to be asked to enter information that will be incorporated
    into your certificate request.
    What you are about to enter is what is called a Distinguished Name or a DN.
    There are quite a few fields but you can leave some blank
    For some fields there will be a default value,
    If you enter '.', the field will be left blank.
    -----
    Country Name (2 letter code) [AU]:BR
    State or Province Name (full name) [Some-State]:Rio de Janeiro
    Locality Name (eg, city) []:
    Organization Name (eg, company) [Internet Widgits Pty Ltd]:TechExpert
    Organizational Unit Name (eg, section) []:
    Common Name (e.g. server FQDN or YOUR name) []:200.200.200.200
    Email Address []:

    Nhập IP và domain của server.

    1. vim /etc/apache2/sites-enabled/000-default.conf
    2. Chỉnh sửa file cấu hình:
    <VirtualHost *:80>
            ServerAdmin webmaster@localhost
            DocumentRoot /var/www/html
            ErrorLog ${APACHE_LOG_DIR}/error.log
            CustomLog ${APACHE_LOG_DIR}/access.log combined
    </VirtualHost>

    Chỉnh thành nội dung sau, sẽ chặn kết nối dùng HTTP và chỉ dùng HTTPS:

    <VirtualHost *:443>
            ServerAdmin webmaster@localhost
            DocumentRoot /var/www/html
            ErrorLog ${APACHE_LOG_DIR}/error.log
            CustomLog ${APACHE_LOG_DIR}/access.log combined
            SSLEngine on
            SSLCertificateFile /etc/apache2/certificate/apache-certificate.crt
            SSLCertificateKeyFile /etc/apache2/certificate/apache.key
    </VirtualHost>

    Trường hợp chuyển tiếp kết nối dùng HTTP sang HTTPS như sau:

    <VirtualHost *:80>
            RewriteEngine On
            RewriteCond %{HTTPS} off
            RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
    </virtualhost>
    <VirtualHost *:443>
            ServerAdmin webmaster@localhost
            DocumentRoot /var/www/html
            ErrorLog ${APACHE_LOG_DIR}/error.log
            CustomLog ${APACHE_LOG_DIR}/access.log combined
            SSLEngine on
            SSLCertificateFile /etc/apache2/certificate/apache-certificate.crt
            SSLCertificateKeyFile /etc/apache2/certificate/apache.key
    </VirtualHost>
    1. service apache2 restart
    2. Xong cài đặt và cấu hình trên máy chủ.
    3. Từ bước này thực hiện trên bất kỳ máy người dùng nào.
    4. Lưu ý, nên sử dụng trình duyệt Firefox vì Chrome sẽ không chấp nhận Selfsigned certificate.
    5. Sử dụng Firefox truy cập đến IP mới cấu hình để kiểm tra: https://200.200.200.200
    6. Sau khi nhập địa chỉ, trình duyệt sẽ báo lỗi bảo mật vì certificate chưa được cài đặt trên máy người dùng. Lúc này click vào link hiển thị trên trình duyệt để tải certificate về lưu tại máy. Sau đó nhấp đôi vào file cert để cài đặt vào Trusted Root Certificate. Khởi động lại trình duyệt. Xong.

    Apache default page

    Để lại một bình luận

    Email của bạn sẽ không được hiển thị công khai. Các trường bắt buộc được đánh dấu *