准备工作
要学习本教程,我们需要先准备:
-
有 sudo 的 root 用户权限,大家可根据《Ubuntu 120.04初始服务器设置》(撰写中,稍后上线)指南来配置自己的服务器。
-
注册域名。本教程使用example.com作为范例。大家可以在阿里云购买一个域名,.com 域名一年60多块钱。
-
为服务器设置了以下两个DNS记录。
- example.com 的A记录指向服务器公网IP
- www.example.com的A记录指向服务器公网IP
-
请确保一个域对应一个server block。在本教程中,我们使用/etc/nginx/sites-available/example.com 来作为操作对象。
安装 Nginx
由于 Nginx 可以从 ubuntu 软件源中获得,因此我们可以使用 apt 来安装 Nginx。
我们可以使用以下命令安装 Nginx 到 Ubuntu 中。
$ sudo apt update
$ sudo apt install nginx
选择 Y 来开始安装,apt 会帮你把 Nginx 和它所必备的依赖安装到我们的服务器中。
调整防火墙
在测试 Nginx 之前,我们需要调整防火墙,让他允许 Nginx 服务通过。Nginx ufw 在安装时会把他自身注册成为服务。
$ sudo ufw app list
输出结果:
kalasearch@chuan-server:~$ sudo ufw app list
Available applications:
Nginx Full
Nginx HTTP
Nginx HTTPS
OpenSSH
你可以看到 Nginx 提供了三个配置文件:
- Nginx Full
- 开端口80 正常,未加密的网络流量
- 端口443 TLS / SSL加密的流量
- Nginx HTTP
- 仅打开端口80 正常,未加密
- Nginx HTTPS
- 仅打开端口443 TLS / SSL加密
我们使用 Nginx HTTPS 来做本教程演示。
$ sudo ufw allow 'Nginx HTTPS'
我们用以下命令来查看更改结果
$ sudo ufw status
我们可以在输出结果中看到
Status: active
To Action From
-- ------ ----
OpenSSH ALLOW Anywhere
Nginx HTTP ALLOW Anywhere
OpenSSH (v6) ALLOW Anywhere (v6)
Nginx HTTP (v6) ALLOW Anywhere (v6)
管理 Nginx
现在,您已启动并运行了Web服务器,让我们回顾一些基本的管理命令。
上一步,我们已经启动起来 Web 服务器了。接下来,让我们来学习一下 Nginx 的基本命令。
要停止Web服务器,输入:
$ sudo systemctl stop nginx
要在停止时,启动Web服务器,键入:
$ sudo systemctl start nginx
要停止,然后再次启动该服务,键入:
$ sudo systemctl restart nginx
如果我们只是修改配置,Nginx 可以在不终端的情况下热加载。我们可以键入:
$ sudo systemctl reload nginx
设置服务器块(Server block)
使用Nginx Web服务器时,服务器块(类似于Apache中的虚拟主机)可用于封装配置详细信息,并在一台服务器中托管多个域。我们将建立一个名为 example.com 的域,但我们可以用自己的域名替换它。
在 Ubuntu 上的 Nginx 默认情况下启用了一个服务器块(server block),服务器块的配置是为给服务器的目录提供地址 /var/www/html。尽管这对于单个站点非常有效,但是如果我们在服务器上托管多个站点,则可能变很臃肿。让我们给/var/www/html目录添加上分站点目录。
比如我们的网站是 example.com ,那我们创建一个对应的目录 example.com 目录:
$ sudo mkdir -p /var/www/example.com/html
接下来,使用$USER环境变量分配目录的所有权:
$ sudo chown -R $USER:$USER /var/www/example.com/html
如果我们没有修改自己的umask值,那么 Web 根目录的权限应该正确,我们可以通过输入以下命令来确认:
$ sudo chmod -R 755 /var/www/example.com
接下来,让我们来编辑index.html 可以使用 nano 编辑器或其他你用这顺手的编辑器:
$ nano /var/www/example.com/html/index.html
在其中,添加以下示例HTML:
/var/www/example.com/html/index.html
<html>
<head>
<title>Welcome to Example.com!</title>
</head>
<body>
<h1>Success! The example.com server block is working!</h1>
</body>
</html>
完成后保存并关闭文件。
接下来我们为 Nginx 来创建一个服务器块。与直接修改默认配置文件不同,我们在以下位置创建一个新文件:/etc/nginx/sites-available/example.com
$ sudo nano /etc/nginx/sites-available/example.com
粘贴到以下内容添加到文件中,这个块的配置与默认块的配置相似,但针对我们的新目录和域名进行了更新:
/etc/nginx/sites-available/example.com
server {
listen 80;
listen [::]:80;
root /var/www/example.com/html;
index index.html index.htm index.nginx-debian.html;
server_name example.com www.example.com;
location / {
try_files $uri $uri/ =404;
}
}
接下来,让我们通过在sites-enabled
目录新建一个链接,好让 Nginx 在启动过程中会读取这个目录:
$ sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
现在已启用并配置了两个服务器块,以及基于它们的listen
和server_name
指令响应请求:
example.com:
将会响应example.com
和www.example.com
的请求default
: 将会响应 80 端口的请求,以及不能匹配到两个服务器块上的请求
为避免可能由于添加其他服务器名称而引起的哈希存储区内存问题,有必要调整/etc/nginx/nginx.conf
文件中的单个值。
打开文件:
$ sudo nano /etc/nginx/nginx.conf
找到server_names_hash_bucket_size
指令并删除#
符号:
...
http {
...
server_names_hash_bucket_size 64;
...
}
...
完成后保存并关闭文件。
接下来,测试以确保我们在 Nginx 文件中的改动,没有任何问题:
sudo nginx -t
如果没有任何问题,请重新启动 Nginx:
sudo systemctl restart nginx
有关要考虑的主题列表,请参阅themes.gohugo.io。本快速入门使用漂亮的Ananke 主题。
首先,从 GitHub 下载主题并将其添加到您网站的themes目录中:
$ cd quickstart
$ git init
$ git submodule add https://github.com/theNewDynamic/gohugo-theme-ananke.git themes/ananke
然后,将主题添加到站点配置中:
$ echo theme = \"ananke\" >> config.toml
安装Certbot
首先我们得现在服务器上安装 Certbot
安装Certbot 及 Nginx 插件 apt
:
$ sudo apt install certbot python3-certbot-nginx
选择 Y 后开始安装,安装完毕我们就可以使用 Certbot 了。
接下来我们要验证一下 Certbot 是否能为Nginx 自动配置 SSL。
让我们的防火墙允许HTTPS
需要先允许ufw使用
$ sudo ufw enable
我们来查看一下当前状态。
$ sudo ufw status
输出结果如果类似下面这样:
Status: active
To Action From
-- ------ ----
OpenSSH ALLOW Anywhere
Nginx HTTP ALLOW Anywhere
OpenSSH (v6) ALLOW Anywhere (v6)
Nginx HTTP (v6) ALLOW Anywhere (v6)
说明防火墙允许HTTPS通信。但如果你的服务器不允许,那么请执行以下命令。
$ sudo ufw allow 'Nginx Full'
$ sudo ufw delete allow 'Nginx HTTP'
然后再让我们查看一下状态的变化。
$ sudo ufw status
输出结果变为:
Status: active
To Action From
-- ------ ----
OpenSSH ALLOW Anywhere
Nginx Full ALLOW Anywhere
OpenSSH (v6) ALLOW Anywhere (v6)
Nginx Full (v6) ALLOW Anywhere (v6)
在此处我们要注意把22,443,80几个端口加入白名单,不然可能后续会导致你进不去ssh
$ sudo ufw allow 22
$ sudo ufw allow 80
$ sudo ufw allow 443
好的,接下来,让我们运行 Certbot 来获取我们的安全证书吧。
获取 SSL 证书
Certbot 提供了获得 SSL 的方法。Nginx Plugins(插件) 帮助我们在必要的时候重新加载配置文件。让我们来开启这个插件。
$ sudo certbot --nginx -d example.com -d www.example.com
certbot
与--nginx
插件一起运行,-d
用于指定我们希望获得证书的域名。
如果我们是第一次运行,那么 Certbot
将提示我们输入邮箱并阅读服务条款。
Invalid email address: .
Enter email address (used for urgent renewal and security notices)
If you really want to skip this, you can run the client with
--register-unsafely-without-email but make sure you then backup your account key
from /etc/letsencrypt/accounts
(Enter 'c' to cancel): nginx@kalasearch.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:
如果这是您第一次运行certbot,将提示您输入电子邮件地址并同意服务条款。
完成此操作后,certbot开始与 Let’s Encrypt 服务器通信,然后开始验证我们是否是这个域名的真正拥有者。
如果成功,certbot 会继续询问我们如何配置HTTPS。
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,然后回车。配置文件就会更新,Nginx 也会重新加载。Certbot
会显示一条消息,告诉我们整个过程已经完成,证书存储在服务器的什么位置上:
IMPORTANT NOTES:
- Congratulations! Your certificate and chain have been saved at:
/etc/letsencrypt/live/example.com/fullchain.pem
Your key file has been saved at:
/etc/letsencrypt/live/example.com/privkey.pem
Your cert will expire on 2020-11-01. 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
我们的证书已经下载并已经加载成功。我们可以使用 https:// 来进行访问。如果访问成功并且浏览器地址栏前面已经出现带锁的标志,那么说明我们的网站已经获得A级保护。
接下来,我们学习如何续订这个安全证书。
验证 Certbot 自动续订
我们获得的加密证书只有 90 天有效期,但不用担心,Certbot
会帮我们解决续订问题,它会每天运行两次systemd
监测程序来检查域名证书是否快到期。如果域名证书在近 30 天到期,它会自动续订这些域名的证书。
我们可以输入以下命令来检查systemctl
的状态:
sudo systemctl status certbot.timer
kalasearch@chuan-server:~$ sudo systemctl status certbot.timer
● certbot.timer - Run certbot twice daily
Loaded: loaded (/lib/systemd/system/certbot.timer; enabled; vendor preset: enabled)
Active: active (waiting) since Sat 2020-08-01 03:37:47 UTC; 46min ago
Trigger: Sat 2020-08-01 12:54:45 UTC; 8h left
Triggers: ● certbot.service
Aug 01 03:37:47 chuan-server systemd[1]: Started Run certbot twice daily.
要测试域名证书的续订过程,我们可以输入以下命令:
sudo certbot renew --dry-run
如果输出结果没有任何错误,则表明一切就绪。如果在未来,续订证书发生问题,那么也不用担心,Let’s Encrypt 会通过前面几步中我们留的邮箱联系我们,通知我们域名证书即将过期。
总结
在本教程中,我们学习了如何安装 Let’s Encrypt 的客户端 certbot。以及如何使用certbot 为我们的域名下载了 SSL 证书。以及学习了如何设置 certbot 让它自动帮我们更新证书。如果你对使用 Certbot 还有疑问,可以在本文下面留言,也可以查看它的官方文档
。
原文
Image Share
相册语法来自 Typlog