View Categories

Install SSL at Debian

https://www.digitalocean.com/community/tutorials/how-to-secure-apache-with-let-s-encrypt-on-debian-11

Step 1 — Installing Certbot #

The first step to using Let’s Encrypt to obtain an SSL certificate is to install the Certbot software on your server.

Note: This tutorial follows the Certbot documentation’s recommendation of installing the software on Debian by using snappy, a package manager developed for Linux systems that installs packages in a format referred to as snaps. You can install Certbot from the default Debian repositories using apt, but this will install an older version (version 1.12.0) than the Certbot snap (version 1.29.0, the latest version as of this writing).

To install Certbot as a snap on Debian, you must first have snapd installed on your server. snapd is a daemon required to install, use, and manage snaps. Installing the snapd package will also install the snap command on your server.

To install snapd, update your local package index if you’ve not done so recently:

sudo apt update

Copy

Then install the snapd package:

sudo apt install snapd

Copy

After running this command, there will be a prompt to confirm that you want to install snapd and its dependencies. You can agree by pressing Y and then ENTER.

Next, use the snap command to install the core snap. This will install some dependencies on your server that are needed for any snap you install, including the Certbot snap:

sudo snap install core

Copy

Then refresh the core snap. Doing so will ensure that you have the latest versions of snapd and its dependencies installed:

sudo snap refresh core

Copy

Note that snaps can be installed under one of three confinement levels which provide varying degrees of isolation from your system. For example, most snaps are installed under the --strict confinement level by default which prevents these programs from accessing your system’s files or network. Because Certbot must be allowed to edit certain configuration files in order to correctly set up certificates, this command includes the --classic option. This confinement level allows any snaps installed under it the same access to system resources as traditional packages.

With this in mind, you can install the certbot snap with the following command.

sudo snap install --classic certbot

Copy

This installation process will install the certbot executable in the /snap/bin/ directory. Create a symbolic link to this file in the /usr/bin/ directory to ensure that you can run the certbot command anywhere on your system:

sudo ln -s /snap/bin/certbot /usr/bin/certbot

Copy

Certbot is now ready to use, but in order for it to configure SSL for Apache, you need to verify that Apache has been configured correctly.

Step 2 — Setting Up the SSL Certificate #

Certbot needs to be able to find the correct virtual host in your Apache configuration for it to automatically configure SSL. Specifically, it does this by searching for a ServerName directive that matches the domain you request a certificate for.

If you followed the virtual host setup step in the Apache installation tutorial, you should have a VirtualHost block for your domain at /etc/apache2/sites-available/your_domain.conf with the ServerName directive already set appropriately.

To check, open the virtual host file for your domain using nano or your favorite text editor:

sudo nano /etc/apache2/sites-available/your_domain.conf

Copy

Find the existing ServerName line. It should be like the following, with your own domain name instead of your_domain:

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

...
ServerName your_domain;
...

If it doesn’t already, update the ServerName directive to point to your domain name. Then save the file and quit your editor. If you used nano, do so by pressing CTRL + XY, then ENTER.

Next, verify the syntax of your configuration edits:

sudo apache2ctl configtest

Copy

If there aren’t any syntax errors, your output will return the following:

Output. . .
Syntax OK

If you get an error, reopen the virtual host file and check for any typos or missing characters. Once your configuration file’s syntax is correct, reload Apache to load the new configuration:

sudo systemctl reload apache2

Copy

Certbot can now find the correct VirtualHost block and update it.

Next, let’s update the firewall to allow HTTPS traffic.

Step 3 — Allowing HTTPS Through the Firewall #

If you have the ufw firewall enabled, as recommended by the prerequisite guide, you’ll need to adjust the settings to allow for HTTPS traffic. Luckily, when installed on Debian, ufw comes packaged with a few profiles that help the process of changing firewall rules for HTTP and HTTPS traffic.

You can verify the current setting by running:

sudo ufw status

Copy

If you followed Step 2 of our guide on How to Install Apache on Debian 11, the output of this command will be as follows, indicating that only HTTP traffic is allowed to the web server:

OutputStatus: active

To                         Action      From
--                         ------      ----
OpenSSH                    ALLOW       Anywhere                  
WWW                        ALLOW       Anywhere                  
OpenSSH (v6)               ALLOW       Anywhere (v6)             
WWW (v6)                   ALLOW       Anywhere (v6)

To allow HTTPS traffic, allow the “WWW Full” profile and delete the redundant “WWW” profile allowance:

sudo ufw allow 'WWW Full'
sudo ufw delete allow 'WWW'

Copy

Your status should now be the following:

sudo ufw status

Copy

OutputStatus: active

To                         Action      From
--                         ------      ----
OpenSSH                    ALLOW       Anywhere                  
WWW Full                   ALLOW       Anywhere                  
OpenSSH (v6)               ALLOW       Anywhere (v6)             
WWW Full (v6)              ALLOW       Anywhere (v6)        

Next, let’s run Certbot and fetch our certificates.

Step 4 — Obtaining an SSL Certificate #

Certbot provides a variety of ways to obtain SSL certificates through plugins. The Apache plugin will take care of reconfiguring Apache and reloading the configuration whenever necessary. To use this plugin, run the following:

sudo certbot --apache -d your_domain -d www.your_domain

Copy

This runs certbot with the --apache plugin, using -d to specify the names for which you’d like the certificate to be valid.

If this is your first time running certbot, you will be prompted to enter an email address and agree to the terms of service. Additionally, it will ask if you’re willing to share your email address with the Electronic Frontier Foundation, a nonprofit organization that advocates for digital rights and is also the maker of Certbot. Feel free to enter Y to share your email address or N to decline.

After doing so, certbot will communicate with the Let’s Encrypt server, then run a challenge to verify that you control the domain you’re requesting a certificate for.

If that’s successful, the configuration will be updated automatically and Apache will reload to pick up the new settings. certbot will wrap up with a message telling you the process was successful and where your certificates are stored:

OutputSuccessfully received certificate.
Certificate is saved at: /etc/letsencrypt/live/your_domain/fullchain.pem
Key is saved at:         /etc/letsencrypt/live/your_domain/privkey.pem
This certificate expires on 2022-10-31.
These files will be updated when the certificate renews.
Certbot has set up a scheduled task to automatically renew this certificate in the background.

Deploying certificate
Successfully deployed certificate for your_domain to /etc/apache2/sites-available/your_domain-le-ssl.conf
Successfully deployed certificate for www.your_domain to /etc/apache2/sites-available/your_domain-le-ssl.conf
Congratulations! You have successfully enabled HTTPS on https://your_domain and https://www.your_domain

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

Your certificates are downloaded, installed, and loaded. Try reloading your website using https:// and notice your browser’s security indicator. It should indicate that the site is properly secured, usually with a green lock icon. If you test your server using the SSL Labs Server Test, it will get an A grade.

Let’s finish by testing the renewal process.

Step 5 — Verifying Certbot Auto-Renewal #

Let’s Encrypt certificates are only valid for ninety days. This is to encourage users to automate their certificate renewal process. The certbot package you installed takes care of this for you by adding a renew script to /etc/cron.d. This script runs twice a day and will automatically renew any certificate that’s within thirty days of expiration.

To test the renewal process, you can do a dry run with certbot:

sudo certbot renew --dry-run

Copy

If you receive no errors, you’re all set. When necessary, Certbot will renew your certificates and reload Apache to pick up the changes. If the automated renewal process ever fails, Let’s Encrypt will send a message to the email you specified


https://cn.linux-console.net/?p=31698

第 6 步:在 Apache 上创建虚拟主机 #

如果您希望 Web 服务器托管多个站点,最好的方法是在 Apache Web 服务器中创建虚拟主机。当您想在单个服务器中托管多个域时,虚拟主机会派上用场

首先,我们需要为域 linux-console.net 创建一个 webroot 目录。

sudo mkdir -p /var/www/html/linux-console.net/

接下来,我们将使用 $USER 变量为目录分配必要的权限。

sudo chown -R $USER:$USER /var/www/html/linux-console.net/

接下来,为域分配 webroot 目录的必要权限。

sudo chmod -R 755 /var/www/html/linux-console.net

现在使用您最喜欢的文本编辑器,开始创建一个示例 index.html 文件。

sudo nano /var/www/html/linux-console.net/index.html

让我们添加一些 HTML 示例内容,如下所示。

<html>
    <head>
        <title>Welcome to TecMint.com</title>
    </head>
    <body>
        <h1>Howdy Geeks!</h1>
    </body>
</html>

保存并退出文本编辑器。

现在,使用如下所示的命令为域创建一个虚拟主机文件。

sudo nano /etc/apache2/sites-available/linux-console.net.conf

现在复制并粘贴下面的内容,并将域 linux-console.net 替换为您自己的域。

<VirtualHost *:80>
    ServerAdmin [email 
    ServerName linux-console.net
    ServerAlias linux-console.net
    DocumentRoot /var/www/html/linux-console.net/
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

保存并退出。

此时,启用虚拟主机文件,如图所示。

sudo a2ensite linux-console.net.conf

现在让我们禁用默认站点

sudo a2dissite 000-default.conf

要使更改生效,请重新加载 Apache Web 服务器。

sudo systemctl restart apache2

现在重新加载您的 Web 服务器并注意您的域的更改。

第 7 步:在 Apache 上使用 Let’s Encrypt 启用 HTTPS/SSL #

要使用 HTTPS 保护您的网站,您将使用 Let’s Encrypt 获取免费的 SSL 证书。

要获得免费的 SSL,您需要安装 Certbot 工具,该工具可以自动获取和更新 SSL 证书。

sudo apt install certbot python3-certbot-apache -y

运行 Certbot 以获取证书并自动配置 Apache 以使用它。

sudo certbot --apache -d linux-console.net -d linux-console.net

系统将提示您输入电子邮件地址并同意服务条款。然后,Certbot 将联系 Let’s Encrypt CA 以获取并安装您的证书。

Certbot 应该会自动将您的虚拟主机配置为使用 SSL,您可以通过检查配置文件来验证它:

sudo nano /etc/apache2/sites-available/linux-console.net-le-ssl.conf

它应如下所示:

<IfModule mod_ssl.c>
<VirtualHost *:443>
    ServerAdmin [email 
    ServerName linux-console.net
    ServerAlias linux-console.net
    DocumentRoot /var/www/html/linux-console.net/
    ErrorLog ${APACHE_LOG_DIR}/linux-console.net_error.log
    CustomLog ${APACHE_LOG_DIR}/linux-console.net_access.log combined

    SSLEngine on
    SSLCertificateFile /etc/letsencrypt/live/linux-console.net/fullchain.pem
    SSLCertificateKeyFile /etc/letsencrypt/live/linux-console.net/privkey.pem
    Include /etc/letsencrypt/options-ssl-apache.conf
</VirtualHost>
</IfModule>

打开 Web 浏览器并导航到 https://linux-console.net 和 https://linux-console.net。您应该会看到之前创建的示例页面,并且连接应该使用 HTTPS 是安全的。

第 8 步:自动续订 Apache 的 SSL 证书 #

Let’s Encrypt 证书的有效期为 90 天,因此通过 cron 作业设置自动续订非常重要。

sudo crontab -e

将以下行添加到 crontab。

0 0 * * 0 /usr/bin/certbot renew --quiet

这将每周自动续订 SSL 证书,确保它在 90 天到期之前续订。