Apache2 支持 SSL

Apache的准备

$ sudo a2enmod ssl rewrite
$ sudo a2ensite default-ssl
$ sudo systemctl restart apache2
$ nmap localhost

Starting Nmap 7.80 ( https://nmap.org ) at 2020-09-18 21:00 JST
Nmap scan report for localhost (127.0.0.1)
Host is up (0.000064s latency).
Not shown: 995 closed ports
PORT     STATE SERVICE
80/tcp   open  http
443/tcp  open  https

逐步解释以下事项:
1. 应用SSL通信模块和重定向模块
2. 启用SSL配置文件
3. 重启Apache2服务器
4. 检查SSL通信是否正常

确认通过[服务器IP]访问,确保index.html页面能够显示。

如果没有显示。

sudo ufw statusで443ポートが開放されているか確認する
ドキュメントルートを変更していないか確認する

変更している場合はdefault-ssl.confのドキュメントルートを修正する

获取服务器证书

在这里,可以使用Let’s Encrypt的网站获取免费的服务器证书来使用。

# apt install -y certbot openssl
# certbot certonly --webroot -w [ドキュメントルート] -d [ドメイン名]

我們將依次進行以下各行的解釋:
1. 安裝certbot(用於Let’s Encrypt)和openssl(用於OpenSSL)。
2. 使用certbot獲取伺服器憑證。

以下是使用 certbot certonly –webroot -w [ドキュメントルート] -d [ドメイン名] 命令时所需输入信息的说明:

1. 输入联系邮箱地址
2. 同意规定条款
3. 注册邮件列表

请确认文件已创建。

# ls -l
合計 4
-rw-r--r-- 1 root root 692  9月 18 18:48 README
lrwxrwxrwx 1 root root  37  9月 18 18:50 cert.pem -> ../../archive/disk.mydns.jp/cert2.pem
lrwxrwxrwx 1 root root  38  9月 18 18:50 chain.pem -> ../../archive/disk.mydns.jp/chain2.pem
lrwxrwxrwx 1 root root  42  9月 18 18:50 fullchain.pem -> ../../archive/disk.mydns.jp/fullchain2.pem
lrwxrwxrwx 1 root root  40  9月 18 18:50 privkey.pem -> ../../archive/disk.mydns.jp/privkey2.pem

Apache2的配置

应用SSL证书

32 SSLCertificateFile      /etc/letsencrypt/live/disk.mydns.jp/cert.pem
33 SSLCertificateKeyFile /etc/letsencrypt/live/disk.mydns.jp/privkey.pem
34 SSLCertificateChainFile /etc/letsencrypt/live/disk.mydns.jp/chain.pem

请确认根据上述内容对每一行进行更改。
请访问https://[服务器IP]以确认index.html页面是否显示。

将HTTP重定向到HTTPS

RewriteEngine on
RewriteCond %{HTTP_HOST} ^[ドメイン名]
RewriteRule ^/(.*)$ https://[ドメイン名]/$1 [R=301,L]

通过添加上述内容,可以确保将页面重定向到HTTPS。

让我们自动更新Let’s Encrypt证书。

* * * /10 * * root certbot renew

如果每10天进行确认,请按照上述方式进行追加。

应用设置并确认。

$ sudo systemctl restart apache2
$ sudo systemctl status apache2
● apache2.service - The Apache HTTP Server
     Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)
     Active: active (running) since Fri 2020-09-18 19:17:30 JST; 2h 40min ago
       Docs: https://httpd.apache.org/docs/2.4/
    Process: 13140 ExecStart=/usr/sbin/apachectl start (code=exited, status=0/SUCCESS)
   Main PID: 13159 (apache2)
      Tasks: 8 (limit: 9388)
     Memory: 15.4M
     CGroup: /system.slice/apache2.service
             ├─13159 /usr/sbin/apache2 -k start
             ├─13160 /usr/sbin/apache2 -k start
             ├─13161 /usr/sbin/apache2 -k start
             ├─13162 /usr/sbin/apache2 -k start
             ├─13163 /usr/sbin/apache2 -k start
             ├─13164 /usr/sbin/apache2 -k start
             ├─13168 /usr/sbin/apache2 -k start
             └─13234 /usr/sbin/apache2 -k start

 9月 18 19:17:30 hostname systemd[1]: Starting The Apache HTTP Server...
 9月 18 19:17:30 hostname systemd[1]: Started The Apache HTTP Server.

如果按照上述的方式操作,成功的话则只需访问https://[域名]和http://[域名],并确认两者都能够连接到https://[域名],那么就可以了。

广告
将在 10 秒后关闭
bannerAds