Instalando certificado letsencrypt no apache2 linux

Instale os pacotes

apt install certbot python3-certbot-apache apache2

crie o arquivo /etc/apache2/letsencrypt.conf

Alias /.well-known/acme-challenge/ /var/www/letsencrypt/.well-known/acme-challenge/
<Directory "/var/www/letsencrypt/.well-known/acme-challenge/">
    Options None
    AllowOverride None
    ForceType text/plain
</Directory>

crie o arquivo /etc/letsencrypt/options-ssl-apache.conf

# This file contains important security parameters. If you modify this file
# manually, Certbot will be unable to automatically provide future security
# updates. Instead, Certbot will print and log an error message with a path to
# the up-to-date file that you will need to refer to when manually updating
# this file.

SSLEngine on

# Intermediate configuration, tweak to your needs
SSLProtocol             all -SSLv2 -SSLv3
SSLCipherSuite          ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA:ECDHE-RSA-AES256-SHA:DHE-RSA-AES128-SHA256:DHE-RSA-AES128-SHA:DHE-RSA-AES256-SHA256:DHE-RSA-AES256-SHA:ECDHE-ECDSA-DES-CBC3-SHA:ECDHE-RSA-DES-CBC3-SHA:EDH-RSA-DES-CBC3-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:AES128-SHA256:AES256-SHA256:AES128-SHA:AES256-SHA:DES-CBC3-SHA:!DSS
SSLHonorCipherOrder     on
SSLCompression          off

SSLOptions +StrictRequire

# Add vhost name to log entries:
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-agent}i\"" vhost_combined
LogFormat "%v %h %l %u %t \"%r\" %>s %b" vhost_common

#CustomLog /var/log/apache2/access.log vhost_combined
#LogLevel warn
#ErrorLog /var/log/apache2/error.log

# Always ensure Cookies have "Secure" set (JAH 2012/1)
#Header edit Set-Cookie (?i)^(.*)(;\s*secure)??((\s*;)?(.*)) "$1; Secure$3$4"

crie o arquivo de virtualhost do seu domínio

/etc/apache2/sites-available/dominio.conf

<VirtualHost *:80>
        ServerAdmin [email protected]
        DocumentRoot /var/www/html/dominio
	include /etc/apache2/letsencrypt.conf
        ServerName dominio.com.br
        ServerAlias dominio.com.br
        ErrorLog ${APACHE_LOG_DIR}/error-dominio.com.br.log
        CustomLog ${APACHE_LOG_DIR}/access-dominio.com.br.log combined
 </VirtualHost>

reinicie o apache /etc/init.d/apache2 restart

agora vamos criar o certificado

certbot certonly -d dominio.com.br –webroot -w /var/www/letsencrypt/

se tudo ocorrer bem, os certificados devem ser criados em /etc/letsencrypt/live/

agora vamos criar o virtualhost do dominio com o certificado, outro arquivo…

/etc/apache2/sites-available/dominio-ssl.conf

<IfModule mod_ssl.c>
<VirtualHost *:443>
        ServerAdmin [email protected]
        DocumentRoot /var/www/html/nfe/api-asaas/
	include /etc/apache2/letsencrypt.conf
        ServerName nfe.bitcontrol.us
        ErrorLog ${APACHE_LOG_DIR}/error-nfe.log
        CustomLog ${APACHE_LOG_DIR}/access-nfe.log combined
        SSLCertificateFile /etc/letsencrypt/live/dominio.com.br/fullchain.pem
        SSLCertificateKeyFile /etc/letsencrypt/live/dominio.com.br/privkey.pem
        Include /etc/letsencrypt/options-ssl-apache.conf
</VirtualHost>

depois de criar o certificado, ative o redirecionamento automático assim sempre que alguém acessar http://dominio.com.br será automaticamente direcionado para https://dominio.com.br

para isso basta adicionar as linhas a seguir dentro do <VirtualHost> </VirtualHost>

RewriteEngine on
RewriteCond %{SERVER_NAME} =dominio.com.br
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]

ficando o arquivo /etc/apache2/sites-available/dominio.conf assim (não esqueça de reiniciar o apache depois das mudança dos arquivos de configuração)

<VirtualHost *:80>
        ServerAdmin [email protected]
        DocumentRoot /var/www/html/dominio
	include /etc/apache2/letsencrypt.conf
        ServerName dominio.com.br
        ServerAlias dominio.com.br
        ErrorLog ${APACHE_LOG_DIR}/error-dominio.com.br.log
        CustomLog ${APACHE_LOG_DIR}/access-dominio.com.br.log combined
RewriteEngine on
RewriteCond %{SERVER_NAME} =dominio.com.br
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
 </VirtualHost>