Nginx 安装和配置
信息
本次以 Debian 为例:如果不是 Debian,请参考:官方教程 安装,如果已经安装 Nginx,可跳过
前提条件:
1
sudo apt install curl gnupg2 ca-certificates lsb-release debian-archive-keyring
导入密钥,验证包的正确性:
1
2
curl https://nginx.org/keys/nginx_signing.key | gpg --dearmor \
| sudo tee /usr/share/keyrings/nginx-archive-keyring.gpg >/dev/null
验证下载的文件是否包含正确的密钥:
1
gpg --dry-run --quiet --no-keyring --import --import-options import-show /usr/share/keyrings/nginx-archive-keyring.gpg
输出应包含完整指纹 573BFD6B3D8FBC641079A6ABABF5BD827BD9BF62 如下:
pub rsa2048 2011-08-19 [SC] [expires: 2024-06-14]
573BFD6B3D8FBC641079A6ABABF5BD827BD9BF62
uid nginx signing key signing-key@nginx.com
如果指纹不同,删除文件。
二选一:
要为稳定的 nginx 包设置 apt 存储库,请运行以下命令:
1
2
3
echo "deb [signed-by=/usr/share/keyrings/nginx-archive-keyring.gpg] \
http://nginx.org/packages/mainline/debian `lsb_release -cs` nginx" \
| sudo tee /etc/apt/sources.list.d/nginx.list
如果希望使用 mainline nginx 包,请改为运行以下命令:
1
2
3
echo "deb [signed-by=/usr/share/keyrings/nginx-archive-keyring.gpg] \
http://nginx.org/packages/mainline/debian `lsb_release -cs` nginx" \
| sudo tee /etc/apt/sources.list.d/nginx.list
设置存储库固定,使用 Nginx 官方的软件包而不是发行版提供的软件包:
1
2
echo -e "Package: *\nPin: origin nginx.org\nPin: release o=nginx\nPin-Priority: 900\n" \
| sudo tee /etc/apt/preferences.d/99nginx
要安装 nginx,请运行以下命令:
1
2
sudo apt update
sudo apt install nginx
Certbot 安装
安装 snap
certbot 官方已经说了,各个发行版安装的 certbot 版本落后,功能不全,推荐我们使用 snap 安装
1
2
3
4
5
6
7
8
# 安装snap
sudo apt update
sudo apt install snapd
# 重启系统
# 安装core
sudo snap install core
sudo snap refresh core
安装 certbot
安装之前确保没有其他方式安装的 certbot
如:apt,pip
可通过这条命令查看 whereis certbot
如果有,卸载它们
sudo apt remove certbot
sudo pip uninstall certbot
1
2
sudo snap install --classic certbot
sudo ln -s /snap/bin/certbot /usr/bin/certbot
(3)查看版本
Certbot 插件
nginx
使用 snap 安装的版本是已经含有 nginx 插件的
申请方法如下
新建一个 nginx 的配置文件
1
sudo touch /etc/nginx/conf.d/abc.conf
编辑 abc.conf
,填入以下模板,根据自己实际情况修改
1
sudo vim /etc/nginx/conf.d/abc.conf
1
2
3
4
5
6
7
8
9
10
11
12
server {
listen 80;
server_name domain.com;
location / {
proxy_pass http://127.0.0.1:80;
proxy_set_header Host $host ;
proxy_set_header X-Real-IP $remote_addr ;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for ;
proxy_set_header X-Forwarded-Proto $scheme ;
}
}
使用 certbot 的 nginx 插件申请
1
2
# 可以先查看哪些可以申请,是否列出了需要的域名
sudo certbot --nginx
申请前保证域名解析到当前的 ip
certbot-dns-dnspod
此插件用于托管在 dnspod 上的域名通过 dns 方式申请
dns 的优势是可以申请泛域名证书,也就是像 *.seektao.cc
这种
1
2
3
sudo snap install certbot-dns-dnspod
sudo snap set certbot trust-plugin-with-root= ok
sudo snap connect certbot:plugin certbot-dns-dnspod
安装完成之后新建一个 .ini 文件,比如 /etc/certbot/dnspod.ini
1
2
sudo mkdir /etc/certbot
sudo touch /etc/certbot/dnspod.ini
编辑 dnspod.ini,填入下面的内容,点此跳转到 dnspod api 申请页面 ,注意申请的是 dnspod token
1
2
dns_dnspod_api_id = xxxxxxx
dns_dnspod_api_token = xxxxxxxxxxxxxbcdef
1
sudo chmod 600 /etc/certbot/dnspod.ini
申请证书的命令,需要替换为自己的域名
1
2
3
4
5
6
7
8
9
10
11
sudo certbot certonly -a dns-dnspod \
--dns-dnspod-credentials /etc/certbot/dnspod.ini \
-d "*.yourdomain.com"
接着就是:
输入邮箱,
是否同意注册acme(选Y),
是否同意发送邮件。。。
接着等待,等待。。就完成了
证书文件保存在 /etc/letsencrypt/live
1
2
3
4
5
# 其中
[ cert name] /privkey.pem:证书的私钥。
[ cert name] /fullchain.pem:在大多数服务器软件中使用的证书文件。
[ cert name] /chain.pem:在Nginx >= 1.3.7 中用于 OCSP stapling。
[ cert name] /cert.pem:会破坏许多服务器配置,不应在未进一步阅读文档的情况下使用。
用到 privkey.pem,fullchain.pem 即可。
certbot-dns-cloudflare
此插件用于托管在 cloudflare 上的域名通过 dns 方式申请
dns 的优势是可以申请泛域名证书,也就是像 *.seektao.cc
这种
1
2
3
4
5
6
7
8
9
10
11
12
sudo snap install certbot-dns-cloudflare
# 如果提示如下错误信息,输入:sudo snap set certbot trust-plugin-with-root=ok
# 接着重新执行 sudo snap install certbot-dns-cloudflare
error: cannot perform the following tasks:
- Run hook prepare-plug-plugin of snap "certbot" ( run hook "prepare-plug-plugin" :
-----
Only connect this interface if you trust the plugin author to have root on the system.
Run ` snap set certbot trust-plugin-with-root= ok` to acknowledge this and then run this command again to perform the connection.
If that doesn' t work, you may need to remove all certbot-dns-* plugins from the system, then try installing the certbot snap again.
-----)
申请 api token,点此跳转
在 /etc/certbot
新建 cloudflare.ini
,将下面的值替换为自己申请的 api token
1
2
# Cloudflare API token used by Certbot
dns_cloudflare_api_token = xxxxxxxxxxxxxxxxx
修改权限
1
sudo chmod 600 cloudflare.ini
申请证书的命令,需要替换为自己的域名
1
2
3
4
sudo certbot certonly \
--dns-cloudflare \
--dns-cloudflare-credentials /etc/certbot/cloudflare.ini \
-d "*.youdomain.com"
certbot-dns-huawei(待续)
此插件用于托管在 huawei 上的域名通过 dns 方式申请
dns 的优势是可以申请泛域名证书,也就是像 *.seektao.cc
这种
自动续期
自动续期添加一个定时任务即可
1
2
3
4
5
# 设置定时任务
sudo crontab -e
# 每月1号的午夜执行 certbot renew 命令来续订证书
0 0 1 * * /usr/local/bin/certbot renew
需要注意自己的 certbot 执行路径是否正确
which certbot
可以看到 certbot 执行路径
一键脚本
经过上面的步骤,你会发现每新增一个服务,需要
新增 nginx 配置文件
配置 nginx 文件
验证并重启 nginx 服务
这时候发现服务器每新增一个服务,就需要添加、修改 nginx 配置文件,域名和端口这些都需要修改,申请 ssl 证书,这些步骤相当麻烦。
如果没有一个简单的 nginx 模板,则会更加复杂繁琐。基于以上需求,我编写了一个脚本。功能 :配置好服务后,只需要一条命令即可完成 nginx 配置,并配置 ssl 证书
命令为:
如果需要获取原文和脚本,关注公众号"寻道之旅",发送"nginx"获取 。