您想了解如何保护您的Apache网站不使用Let的加密免费HTTP证书吗? 在本教程中,我们将向您展示在Ubuntu Linux上使用Let的加密来安全Apache所需的所有步骤

•Ubuntu版本:18.04

为了说明安装,我们将使用名为MINING-POOL.NINJA的域名之一。

您需要更改配置文件,并用您的域名替换我们的域名MINING-POOL.NINJA。

硬件清单:

以下部分介绍了用于创建此Wordpress教程的设备列表。

上面列出的每一件硬件都可以在亚马逊网站上找到。

Apache相关教程:

在此页面上,我们提供了与Apache相关的教程列表的快速访问。

1.购买域名

首先,您需要访问 GODADDY 并创建一个帐户。

此帐户将用于购买DNS域。

在我们的示例中,我们购买名为MINING-POOL.NINJA的域。

随意使用任何网站购买DNS域名。

GoDaddy是我最喜欢的选择。

2.配置DNS条目

现在,您需要配置指向Apache Web服务器的DNS注册表。

购买DNS域后,您需要创建一个DNS条目,将您的网站地址指向运行Ubuntu Linux的计算机。

在我们的示例中,我们创建了一个DNS域,将域MINING-POOL.NINJA指向IP地址35.163.100.49。

HTTPS DNS Entry

使用以下命令在运行Linux的计算机上测试DNS配置。

# apt-get update
# apt-get install dnsutils
# nslookup mining-pool.ninja

Non-authoritative answer:
Name: mining-pool.ninja
Address: 35.163.100.49

上面的命令确认域指向IP地址35.163.100.49。

3.安装Apache Web服务器

Let的加密证书颁发机构要求用户拥有功能齐全的Apache Web服务器。

我们需要安装Apache Web服务器和所有必需的软件。

在Linux控制台上,使用以下命令安装所需的软件包。

# apt-get update
# apt-get install apache2 php7.2 php7.2-mysql libapache2-mod-php7.2
# apt-get install openssl

启用Apache mod_rewrite并编辑apache2.conf文件。

# a2enmod rewrite
# vi /etc/apache2/apache2.conf

在文件末尾添加以下行。

<Directory /var/www/html>
AllowOverride All
</Directory>

为您的网站创建Apache Virtualhost配置文件。

# vi /etc/apache2/sites-available/mining-pool.conf

请记住,您的Virtualhost文件名和位置可能与我的不同。

举个例子,这是我们文件的内容:

<VirtualHost *:80>
ServerAdmin nobody@care.com
DocumentRoot /websites/www
ServerName mining-pool.ninja

<Directory /websites/www/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>

ErrorLog /websites/logs/error.log
CustomLog /websites/logs/access.log combined
LogLevel error
</VirtualHost>

创建所需文件夹列表。

在所有文件夹上设置正确的文件权限。

请注意,您的文件夹位置可能与我的不同。

# mkdir /websites
# cd /websites
# mkdir www logs
# chown www-data.www-data /websites -R

启用Apache Virtualhost配置并重新启动Apache服务。

# a2ensite mining-pool.conf
# systemctl reload apache2

4.在Apache上配置免费HTTPS证书

在Linux控制台上,使用以下命令安装所需的软件包。

# apt-get install software-properties-common
# add-apt-repository universe
# add-apt-repository ppa:certbot/certbot
# apt-get update
# apt-get install python-certbot-apache

Certbot脚本要求用户拥有功能齐全的Apache Web服务器。

我们已经完成了Apache Virtualhost的安装和配置。

使用以下命令请求并安装Apache免费HTTPS证书。

请注意,您的域名不应与我的域名相同。

# certbot --apache -d mining-pool.ninja

•按(A)同意服务条款。
•按(Y)分享您的电子邮件并接收简报。
•按(2)自动将HTTP网站重定向到HTTPS版本。

Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator apache, Installer apache
Enter email address (used for urgent renewal and security notices) (Enter 'c' to
cancel): techexpert.tips@gmail.com

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Please read the Terms of Service at
https://letsencrypt.org/documents/LE-SA-v1.2-November-15-2017.pdf. You must
agree in order to register with the ACME server at
https://acme-v02.api.letsencrypt.org/directory
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(A)gree/(C)ancel: A

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Would you be willing to share your email address with the Electronic Frontier
Foundation, a founding partner of the Let's Encrypt project and the non-profit
organization that develops Certbot? We'd like to send you email about our work
encrypting the web, EFF news, campaigns, and ways to support digital freedom.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(Y)es/(N)o: Y
Obtaining a new certificate
Performing the following challenges:
http-01 challenge for mining-pool.ninja
Enabled Apache rewrite module
Waiting for verification...
Cleaning up challenges
Created an SSL vhost at /etc/apache2/sites-available/mining-pool-le-ssl.conf
Enabled Apache socache_shmcb module
Enabled Apache ssl module
Deploying Certificate to VirtualHost /etc/apache2/sites-available/mining-pool-le-ssl.conf
Enabling available site: /etc/apache2/sites-available/mining-pool-le-ssl.conf

Please choose whether or not to redirect HTTP traffic to HTTPS, removing HTTP access.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1: No redirect - Make no further changes to the webserver configuration.
2: Redirect - Make all requests redirect to secure HTTPS access. Choose this for
new sites, or if you're confident your site works on HTTPS. You can undo this
change by editing your web server's configuration.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Select the appropriate number [1-2] then [enter] (press 'c' to cancel): 2
Enabled Apache rewrite module
Redirecting vhost in /etc/apache2/sites-enabled/mining-pool.conf to ssl vhost in /etc/apache2/sites-available/mining-pool-le-ssl.conf

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Congratulations! You have successfully enabled https://mining-pool.ninja

You should test your configuration at:
https://www.ssllabs.com/ssltest/analyze.html?d=mining-pool.ninja
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

IMPORTANT NOTES:
- Congratulations! Your certificate and chain have been saved at:
/etc/letsencrypt/live/mining-pool.ninja/fullchain.pem
Your key file has been saved at:
/etc/letsencrypt/live/mining-pool.ninja/privkey.pem
Your cert will expire on 2019-03-19. To obtain a new or tweaked
version of this certificate in the future, simply run certbot again
with the "certonly" option. To non-interactively renew *all* of
your certificates, run "certbot renew"
- 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

系统将自动请求证书并配置Apache Web服务器以将所有HTTP访问重定向到您的网站的HTTPS版本。

在我们的示例中,系统创建了名为mining-pool-le-ssl.conf的HTTPS Virtualhost配置文件

以下是HTTPS Virtualhost配置文件mining-pool-le-ssl.conf的内容:

<IfModule mod_ssl.c>
<VirtualHost *:443>
ServerAdmin nobody@care.com
DocumentRoot /websites/www
ServerName mining-pool.ninja

<Directory /websites/www/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>

ErrorLog /websites/logs/error.log
CustomLog /websites/logs/access.log combined
LogLevel error

SSLCertificateFile /etc/letsencrypt/live/mining-pool.ninja/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/mining-pool.ninja/privkey.pem
Include /etc/letsencrypt/options-ssl-apache.conf
</VirtualHost>
</IfModule>

KEY文件包含您的证书私钥,必须始终保存在安全的地方。

以下是KEY文件的内容:/etc/letsencrypt/live/mining-pool.ninja/privkey.pem

-----BEGIN PRIVATE KEY-----
MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCwGYFx4kcQmYC5
gJftnjZhWNVZkIL7wSMTFtFfIY37PNbVNeJ11HoudX8HWout8qXOxBvHE8oUtzrO
/956cCVNsdY+eXZVVwUhjeSRT9wH2vUh3vVw6mi0TRy1KZJGo+hFeUS7zGb0/IwV
MofsEE3rAM1s9RoNOzH6Q/S7OiXFH4OhjbORcv0mBBDjbUgIB+PPoSdCj98yHjZN
vyVEvU9xZLQZxDfFMJr9lNAOR0AIOaALkXaMu1zC5+G1Z2x+W9xSI4JMegiKoHhI
/Xt3A3lCyT81GGc3RrmlighZZVstr0FK4XotcJe5YLsBn1xIukyUrWnJHGRz8MmC
26ShYRLLAgMBAAECggEAdpK19ljGQQcbBczwf67oLuQBxCzqBVomo9PZZpTRZTmx
7h46Pdyz6FR1FB6DrryTqIWjrohEqW42yuq+gvLAK3BzMd3XL99INS+1l6KOJTvp
ksTtvvoXRlAl5FX5iFcVLrJY9ZrMOGlUaXF81BamYHZJ2/OtdvtEy562Str8U52m
GTkgfpWnaLc66T05zViNwiAb5yg0v9Xb9YOEgLY8VdqYppUnJNzYs7utiDGbOjPa
OCZ5oLTl1b9l6sNw7RhhOmq/jPOur4Keg124fyleULi2Uln52waLWeI8fWI/AHmP
UQQkC3athEOKr0f1Zk9ZRxYJq5FYGPTElqQdbltEAQKBgQDiqSp4XM0tfFJwZIkH
ooEcPw+to4T0333K4mipx+YxlELCHvIXW2xIxlejyhgnVIQWX6qS0F7VUGnVuSKz
hB0zfcd89MYcqGyQSEdc9kZB+Twb9Hzd3fTXu7hKjXoz9+UKfcOXfQdILGnDqyG8
zCF//aNocRvxaO9E1cZ+1irlcQKBgQDG5OZUr8kMYI8Tl0GcmWZSFZfMYBYT3CAq
YPHmNQcHhENGz6v9jBZjkeZJBLMRZLlRWjcrWhKbEEHsjZJYiJP4u2n6n57Il9h9
OVqfK7RR3k1r9bMKzhYRnu1FsEPpMKKUrLPDChthxduXEPPZgoKGSYmhLAiYqyPg
USOyZzFt+wKBgQCmfjSgLSrZ2pSD0pWFciz8nqXZml+zr+cq/Jx8+kVw7cML7MdP
uOwVmPwPaAfN/Br6hFM7fKvsbSEQyOlgjA8XH7FFbFeJ/X2hjjY9Zvm8xGMO9cwy
/TM4tL9uo/W77r/jsPXXdRPARD98Z1xk3l1NMvaxZURtU/NN0On5iUABQQKBgGIu
FiER0R3iAF68WLAT7TVx4UgK2nsdO2eQCXkJDhpAyPEIjK8RWu/10oaS4k4guXYW
IYut3+dZpCFAVVCMwvaSlSMZbkk4464VBM3zxgRvxyoW1DXuO2ihWiD/fNZ8opTf
l7EcqR2fjOFKhgUF7qaZYG4GDYxHVuNhSkzDsdvHAoGBAIfDk7fLsG/BYROxJpzN
b1CoG8DE+DKYTWwMWW45rDF0ImbC6H3fg876CeZ3YKGWqt9qkha7gnGGVRRufP15
O7ZEXebZXeDYafXSNcnqSe7FSNtVr1eFlTWmL8OiWQuaqxjs/VogdWfhkEGPISVF
AOM3I6XSGx4NxW2RgYmvQZ4O
-----END PRIVATE KEY-----

系统会自动更改原始Apache Virtualhost配置文件,以将所有HTTP请求重定向到HTTPS版本。

以下是更新的原始HTTP Virtualhost配置文件mining-pool.conf的内容:

<VirtualHost *:80>
ServerAdmin nobody@care.com
DocumentRoot /websites/www
ServerName mining-pool.ninja

<Directory /websites/www/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>

ErrorLog /websites/logs/error.log
CustomLog /websites/logs/access.log combined
LogLevel error
RewriteEngine on
RewriteCond %{SERVER_NAME} =mining-pool.ninja
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent] </VirtualHost>

5.测试您的Apache HTTPS网站

要测试您的安装,请打开浏览器并输入您的网站HTTP地址。

在我们的示例中,在浏览器中输入了以下URL:

•http://mining-pool.ninja

应该显示您的网站的HTTPS版本,而不是HTTP版本。

Lets Encrypt Apache

恭喜,您的Apache配置现在使用LET'S ENCRYPT免费HTTPS证书。

6.续订Apache HTTPS证书

您需要注意,Let's Encrypt的证书仅在90天内有效。

Apache HTTPS Free Certificate

系统创建计划任务以自动续订到期后30天内的任何证书。

计划的任务名称是certbot,它位于目录/etc/cron.d中。

这是/etc/cron.d/certbot文件的内容:

# /etc/cron.d/certbot: crontab entries for the certbot package
#
# Upstream recommends attempting renewal twice a day
#
# Eventually, this will be an opportunity to validate certificates
# haven't been revoked, etc. Renewal will only occur if expiration
# is within 30 days.
#
# Important Note! This cronjob will NOT be executed if you are
# running systemd as your init system. If you are running systemd,
# the cronjob.timer function takes precedence over this cronjob. For
# more details, see the systemd.timer manpage, or use systemctl show
# certbot.timer.
SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

0 */12 * * * root test -x /usr/bin/certbot -a \! -d /run/systemd/system && perl -e 'sleep int(rand(43200))' && certbot -q renew

使用以下命令模拟证书续订过程。

# certbot renew --dry-run

如果一切正常,您应该看到以下消息:

Saving debug log to /var/log/letsencrypt/letsencrypt.log

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Processing /etc/letsencrypt/renewal/mining-pool.ninja.conf
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Cert not due for renewal, but simulating renewal for dry run
Plugins selected: Authenticator apache, Installer apache
Renewing an existing certificate
Performing the following challenges:
http-01 challenge for mining-pool.ninja
Waiting for verification...
Cleaning up challenges

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
new certificate deployed with reload of apache server; fullchain is
/etc/letsencrypt/live/mining-pool.ninja/fullchain.pem
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
** DRY RUN: simulating 'certbot renew' close to cert expiry
** (The test certificates below have not been saved.)

Congratulations, all renewals succeeded. The following certs have been renewed:
/etc/letsencrypt/live/mining-pool.ninja/fullchain.pem (success)
** DRY RUN: simulating 'certbot renew' close to cert expiry
** (The test certificates above have not been saved.)
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

IMPORTANT NOTES:
- Your account credentials have been saved in your Certbot
configuration directory at /etc/letsencrypt. You should make a
secure backup of this folder now. This configuration directory will
also contain certificates and private keys obtained by Certbot so
making regular backups of this folder is ideal.