Web�T�[�o�[�ԒʐM���e�Í����iApache+mod_SSL+Certbot�j

�ŏI�X�V���F 2023.02.23

���T�v

���[�U�[����p�X���[�h���̋@������Web�u���E�U������͂���ꍇ�A���݌�����鋰�ꂪ���邽�߁AWeb�T�[�o�[�Ԃ̒ʐM���e���Í�������B
�����ł́AWeb�T�[�o�[��mod_ssl�𓱓����āAURL��http://�`�ł͂Ȃ��Ahttps://�`�ŃA�N�Z�X���邱�Ƃɂ���āAWeb�T�[�o�[�Ԃ̒ʐM���e���Í�������悤�ɂ���B
�܂��AWeb�T�[�o�[�Ƃ̒ʐM���e���Í�������ɂ́A�T�[�o�[�ؖ����𔭍s����K�v�����邪�A���삵���T�[�o�[�ؖ����ł̓N���C�A���g���ʐM���s�����сiWeb�u���E�U�N�����j�ɃZ�L�����e�B�̌x�����\������Ă��܂��B�Ώ��Ƃ��ẮA�L���̃T�[�o�[�ؖ����𔭍s���邩�A����T�[�o�[�ؖ����𔭍s���Ċe�N���C�A���g�ɃC���|�[�g����΂悢���A�L���̃T�[�o�[�ؖ����͍��z���”N�P�ʂɌp�����Ĕ�p��������A����T�[�o�[�ؖ����̃C���|�[�g�͕s���葽���̂��ׂĂ̗��p�҂ɑ΂��ăT�[�o�[�ؖ������C���|�[�g������̂͌����I�ł͂Ȃ����Ƃ���A�����ł́ACertbot�̖����T�[�o�[�ؖ����𓱓����A�����ŃZ�L�����e�B�̌x�����\�����ꂸ�ɈÍ����ʐM���s�����悤�ɂ���B

�yCertbot�̓����z
�E�T�[�o�[�ؖ����𖳗��Ŕ��s�ł���
�E�T�[�o�[�ؖ����̎擾���R�}���h�݂̂ōs����iWeb�o�R�A���[���o�R���ł̔ς킵���葱�������Ȃ��j
�E�T�[�o�[�ؖ����̍X�V���R�}���h�݂̂ōs����i�ێ����T�[�o�[�ؖ����̍X�V�^�����ւ������S�������j


��Certbot�N���C�A���g�C���X�g�[��

[root@centos ~]# yum -y install epel-release�@���@yum�pEPEL���|�W�g���C���X�g�[��

[root@centos ~]# yum -y install snapd�@���@snapd�C���X�g�[��

[root@centos ~]# systemctl enable --now snapd.socket�@���@snapd.socket�L����

[root@centos ~]# ln -s /var/lib/snapd/snap /snap�@���@/var/lib/snapd/snap����/snap�փ����N�𒣂�

[root@centos ~]# snap install --classic certbot�@���@certbot�C���X�g�[��

[root@centos ~]# ln -s /snap/bin/certbot /usr/bin/certbot�@���@/snap/bin/certbot����/usr/bin/certbot�փ����N�𒣂�

���T�[�o�[�ؖ����擾

[root@centos ~]# certbot certonly --webroot -w �h�L�������g���[�g -m ���[���A�h���X -d Web�T�[�o�[�� --agree-tos�@���@�T�[�o�[�ؖ����擾


�|�|�p�����[�^�w���|�|
�h�L�������g���[�g��/var/www/html/centos/
���[���A�h���X�˔C�ӂ̃��[���A�h���X
Web�T�[�o�[����centossrv.com


Saving debug log to /var/log/letsencrypt/letsencrypt.log
Requesting a certificate for centossrv.com

Successfully received certificate.
Certificate is saved at: /etc/letsencrypt/live/centossrv.com/fullchain.pem
Key is saved at:         /etc/letsencrypt/live/centossrv.com/privkey.pem
This certificate expires on 2023-01-14.
These files will be updated when the certificate renews.
Certbot has set up a scheduled task to automatically renew this certificate in the background.

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
If you like Certbot, please consider supporting our work by:
 * Donating to ISRG / Let's Encrypt:   https://letsencrypt.org/donate
 * Donating to EFF:                    https://eff.org/donate-le
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -


��mod_ssl�C���X�g�[��

[root@centos ~]# yum -y install mod_ssl�@���@mod_ssl�C���X�g�[��

��Apache�ݒ�

�i�P�jApache�ݒ�iCertbot�Ή��j
[root@centos ~]# vi /etc/httpd/conf.d/ssl.conf�@���@SSL�ݒ�t�@�C���ҏW
#   Server Certificate:
# Point SSLCertificateFile at a PEM encoded certificate.  If
# the certificate is encrypted, then you will be prompted for a
# pass phrase.  Note that a kill -HUP will prompt again.  A new
# certificate can be generated using the genkey(1) command.
SSLCertificateFile /etc/letsencrypt/live/centossrv.com/cert.pem�@���@���J���w��

#   Server Private Key:
#   If the key is not combined with the certificate, use this
#   directive to point at the key file.  Keep in mind that if
#   you've both a RSA and a DSA private key you can configure
#   both in parallel (to also allow the use of DSA ciphers, etc.)
SSLCertificateKeyFile /etc/letsencrypt/live/centossrv.com/privkey.pem�@���@�閧���w��

#   Server Certificate Chain:
#   Point SSLCertificateChainFile at a file containing the
#   concatenation of PEM encoded CA certificates which form the
#   certificate chain for the server certificate. Alternatively
#   the referenced file can be the same as SSLCertificateFile
#   when the CA certificates are directly appended to the server
#   certificate for convinience.
SSLCertificateChainFile /etc/letsencrypt/live/centossrv.com/chain.pem�@���@���ԏؖ����w��

�i�Q�jApache�ݒ�iSSL Server Test�Ή��j
SSL Server Test��A+�]���𓾂邽�߂̐ݒ���s���B
[root@centos ~]# vi /etc/httpd/conf.d/ssl.conf�@���@SSL�ݒ�t�@�C���ҏW
#   SSL Protocol support:
# List the enable protocol levels with which clients will be able to
# connect.  Disable SSLv2 access by default:
SSLProtocol All -SSLv2 -SSLv3 -TLSv1 -TLSv1.1�@���@TLS 1.2�̂ݗL��

#   Speed-optimized SSL Cipher configuration:
#   If speed is your main concern (on busy HTTPS servers e.g.),
#   you might want to force clients to specific, performance
#   optimized ciphers. In this case, prepend those ciphers
#   to the SSLCipherSuite list, and enable SSLHonorCipherOrder.
#   Caveat: by giving precedence to RC4-SHA and AES128-SHA
#   (as in the example below), most connections will no longer
#   have perfect forward secrecy - if the server's key is
#   compromised, captures of past or future traffic must be
#   considered compromised, too.
#SSLCipherSuite RC4-SHA:AES128-SHA:HIGH:MEDIUM:!aNULL:!MD5
#SSLHonorCipherOrder on
SSLCipherSuite ECDH+AESGCM:DH+AESGCM:ECDH+AES256:DH+AES256:ECDH+AES128:DH+AES:RSA+AESGCM:RSA+AES:!aNULL:!MD5:!DSS�@���@�lj��i�g�p����������@�j
SSLHonorCipherOrder on�@���@�lj��i�Í������@���T�[�o�[���Ō���j

Header always set Strict-Transport-Security "max-age=15768000"�@���@�lj��iHSTS���펞HTTPS�A�N�Z�X�̗L�����j
</VirtualHost>

�i�R�jApache�ݒ�iAWStats�Ή��j
Apache�A�N�Z�X���O���(AWStats)�ʼn�͂ł���悤��https�A�N�Z�X���O��http�A�N�Z�X���O�Ɠ����t�@�C���ɓ����t�H�[�}�b�g�ŏo�͂���悤�ɂ���B
[root@centos ~]# vi /etc/httpd/conf.d/ssl.conf�@���@SSL�ݒ�t�@�C���ҏW
# Use separate log files for the SSL virtual host; note that LogLevel
# is not inherited from httpd.conf.
ErrorLog logs/error_log�@���@���O�t�@�C�����ύX
CustomLog logs/access_log combined env=!no_log�@���@���O�擾�f�B���N�e�B�u�ƃ��O�t�@�C�����ύX
LogLevel warn

��Apache�ݒ蔽�f

�i�P�jApache�ݒ蔽�f
[root@centos ~]# systemctl reload httpd�@���@Apache�ݒ蔽�f

�i�Q�jTCP443�ԃ|�[�g�J��
�y���[�^�[�z
���[�^�[���̐ݒ�ŁATCP443�ԃ|�[�g�ւ̃A�N�Z�X���T�[�o�[�ɓ]������悤�ɂ���B
�����[�^�[�̐ݒ�͊e���[�^�[�̃}�j���A���܂������[�J�[�ʃ��[�^�[�|�[�g�J���菇���Q��

�y�t�@�C�A�E�H�[���z
�T�[�o�[���̃t�@�C�A�E�H�[���ݒ�ŁATCP443�ԃ|�[�g�ւ̃A�N�Z�X�����‚���悤�ɂ���B
���t�@�C�A�E�H�[���ݒ�͂�������Q��

Port�`�F�b�N�e�X�g�y�O�������Port�J���m�F�z�Łu�z�X�g��(FQDN) �܂��� �O���[�o��IP�A�h���X�v�ɃT�[�o�[��(��:centossrv.com)�A�u�`�F�b�N�|�[�g�ԍ��v��443�Ɠ��́A�u�����ӁE���񎖍��v���m�F�`�F�b�N���āuPort�`�F�b�N���s�v�{�^�����������A
�z�X�g:centossrv.com
�|�[�g:443
�ɃA�N�Z�X�ł��܂���
�ƕ\������邱�Ƃ��m�F�B

��Apache�m�F

Web�u���E�U��https://Web�T�[�o�[���i��:https://centossrv.com�j�փA�N�Z�X���ăZ�L�����e�B�̌x�����\������Ȃ�����

SSL Server Test��A+�]���ƂȂ邱�Ƃ��m�F�B

���T�[�o�[�ؖ��������X�V�ݒ�

Certbot�̃T�[�o�[�ؖ����̗L�����Ԃ�3�����̂��߁A���������ŃT�[�o�[�ؖ������X�V����悤�ɂ���B
[root@centos ~]# vi /etc/cron.monthly/certbot�@���@�T�[�o�[�ؖ��������X�V�X�N���v�g�쐬
#!/bin/bash
log=`mktemp`
code=0

which certbot > /dev/null 2>&1
if [ $? -eq 0 ]; then
    CERTBOT=`which certbot`
else
    CERTBOT=`which certbot-auto`
fi

#
# �ؖ����X�V
#
for conf in `ls /etc/letsencrypt/renewal/`
do
    # �h���C�����擾
    domain=`echo ${conf}|sed -e 's/\([^ ]*\)\.conf/\1/p' -e d`

    # �F�ؕ����擾
    authenticator=`grep authenticator /etc/letsencrypt/renewal/${conf}|awk '{print $3}'`

    if [ ${authenticator} = 'webroot' ]; then
        # Web�F�؂̏ꍇ

        # �h�L�������g���[�g�擾
        webroot=`grep webroot_path /etc/letsencrypt/renewal/${conf}|grep =|awk '{print $3}'|awk -F '[,]' '{print $1}'`

        # �ؖ����X�V
        ${CERTBOT} certonly --webroot \
        -w ${webroot} -d ${domain} --renew-by-default >> ${log} 2>&1
        [ $? -ne 0 ] && cat ${log}
    else
        # �X�^���h�A�����F�؂̏ꍇ

        # �ؖ����X�V
        lsof -i:80 > /dev/null 2>&1
        if [ $? -eq 0 ]; then
            echo 'Web�T�[�o�[�ғ����̂��߃X�^���h�A�����F�ؕs��'
        else
            ${CERTBOT} certonly -a standalone \
            -d ${domain} --renew-by-default >> ${log} 2>&1
            [ $? -ne 0 ] && cat ${log}
        fi
    fi
    
    # �Â��ؖ������폜
    find /etc/letsencrypt/archive/${domain}/ -mtime +30 -delete
    
done

#
# �ؖ����X�V���f
#

# Web�T�[�o�[�ݒ�ēǍ���
lsof -i:443 > /dev/null 2>&1
if [ $? -eq 0 ]; then
    rpm -q systemd > /dev/null 2>&1
    if [ $? -eq 0 ]; then
        systemctl reload httpd
    else
        /etc/rc.d/init.d/httpd reload > /dev/null 2>&1
    fi
fi

# SMTP�T�[�o�[�ݒ�ēǍ���
lsof -i:465 > /dev/null 2>&1
if [ $? -eq 0 ]; then
    rpm -q systemd > /dev/null 2>&1
    if [ $? -eq 0 ]; then
        systemctl reload postfix
    else
        /etc/rc.d/init.d/postfix reload > /dev/null 2>&1
    fi
fi

# IMAP�T�[�o�[�ݒ�ēǍ���
lsof -i:995 > /dev/null 2>&1
if [ $? -eq 0 ]; then
    rpm -q systemd > /dev/null 2>&1
    if [ $? -eq 0 ]; then
        systemctl reload dovecot
    else
        /etc/rc.d/init.d/dovecot reload > /dev/null 2>&1
    fi
fi

#
# ���O��syslog�֏o�͌�폜
#
cat ${log}|logger -t `basename ${0}` ; rm -f ${log}

[root@centos ~]# chmod +x /etc/cron.monthly/certbot�@���@�T�[�o�[�ؖ��������X�V�X�N���v�g�֎��s�����t��


���֘A�R���e���c




�����̃y�[�W�̃g�b�v�֖߂�

�v���C�o�V�[�|���V�[
centossrv.com