在Ubuntu Linux上安装Apache Free HTTPS证书

本教程将向您展示在Ubuntu Linux上安装Apache Free HTTPS证书所需的所有步骤。

在Ubuntu Linux上安装Apache

让我们在适当的PHP支持下安装Apache。

使用Ubuntu APT命令安装Apache服务器。

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

可选的。 使用以下命令安装Apache最常用的PHP模块。

# apt-get install php7.2-xml php7.2-curl php7.2-gd php7.2-mbstring
# apt-get install php7.2-bz2 php7.2-zip php7.2-json php7.2-readline

启用Apache mod_ssl。

启用Apache mod_rewrite。

编辑apache2.conf文件。

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

在apache2.conf的末尾添加以下行

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

重新启动Apache服务。

# service apache2 restart

您在Ubuntu Linux上完成了Apache Web服务器安装。

配置Apache虚拟主机

虚拟主机,如果一个功能允许一个Apache服务器使用相同的IP地址提供多个网站。

让我们创建必要的基础设施来使用Apache VirtualHosts功能。

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

我们的网站名为mining-pool.ninja。

mining-pool.ninja网站文件应位于/ websites / mining-pool / www目录中。

mining-pool.ninja日志将存储在/ websites / mining-pool / logs目录中。

Alert!

Mining-pool.ninja was used as an example!

You need to change your configuration files to reflect your website name.

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

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

这是我们配置的文件。

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

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

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

启用您的Website Virtualhost配置文件。

重新启动Apache服务。

# a2ensite mining-pool.conf
# service apache2 restart

您完成了Apache VirtualHosts配置。

配置DNS域注册表

访问 GODADDY 并购买DNS域名。

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

您可以使用任何网站购买DNS域名,GoDaddy只是我个人的选择。

创建一个DNS条目,将您的网站指向运行Apache的计算机。

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

Godaddy Apache DNS Configuration

使用NSLOOKUP命令测试DNS配置

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

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

您完成了DNS域配置。

打开浏览器并尝试访问您网站的HTTP版本。

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

•http://mining-pool.ninja

Apache Virtualhost HTTP

现在,我们唯一关心的是安装免费的HTTPS证书,并自动将所有HTTP流量重定向到我们网站的HTTPS版本。

在Apache上配置免费HTTPS证书

安装所需的软件包以在Ubuntu Linux上使用LET'S ENCRYPT免费SSL / TLS证书

# 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

请求并安装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文件包含您的证书私钥,必须始终保存在安全的地方。

mining-pool.ninja的密钥文件存储在/etc/letsencrypt/live/mining-pool.ninja/privkey.pem中。

系统会自动修改原始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>

在Apache上测试免费HTTPS证书

完成所有配置。

是时候测试您的配置了。

打开浏览器并尝试访问您网站的HTTP版本。

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

•http://mining-pool.ninja

Apache会自动将HTTP请求重定向到您网站的HTTPS版本。

Lets Encrypt Apache HTTPS

您完成了Apache HTTPS免费证书配置。

如何续订免费HTTPS证书

LET'S ENCRYPT免费SSL / TLS证书仅在90天内有效。

系统创建计划任务,以在到期后的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.