SSL证书安装

转载自 nginx 以及 certbot 在 Ubuntu18.04 上的部署

本文介绍 Nginx 在 Ubuntu18.04 上的安装,以及使用 Certbot 配置 HTTPS。

安装 Nginx

1
2
$ sudo apt update
$ sudo apt install nginx

Ngnix 相关命令

1
2
3
4
5
6
7
8
9
10
11
12
13
# 开始nginx服务
$ sudo systemctl start nginx
# 停止nginx服务
$ sudo systemctl stop nginx
# 重启nginx服务,一般用于修改nginx配置文件后
$ sudo systemctl restart nginx
$ sudo nginx -s reload
# 默认情况下nginx开机启动,如果禁止开机启动,可以使用如下命令
$ sudo systemctl disable nginx
# 开启开机自动启动nginx
$ sudo systemctl enable nginx
# 检查nginx配置文件语法(配置信息通常在文件 /etc/nginx/nginx.conf 或者 /etc/nginx/sites-available/default 当中配置)
$ sudo nginx -t

安装 Certbot

1
2
$ sudo add-apt-repository ppa:certbot/certbot
$ sudo apt install python-certbot-nginx

使用 Certbot 获取免费 SSL 证书

1
$ sudo certbot --nginx -d example.com -d www.example.com

–nginx 选项指明使用 nginx 插件,生成证书后 certbot 会修改 nginx/sites-available/default 中的路由配置文件,如果希望手动更改路由配置可以再生成证书时选择 [1]。

1
2
3
4
5
6
7
8
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):

配置生效

1
nginx -s reload

证书自动重申

1
$ sudo certbot renew --dry-run

Nginx 相关配置文件目录和日志目录

/var/www/html 默认的 nginx 提供的返回页面 /var/log/nginx/*log 通过 log 文件查询请求和错误信息 /etc/nginx/nginx.conf nginx 默认的配置文件 /etc/nginx/sites-available/ 默认配置文件为 default,可以手动添加和修改文件 /etc/nginx/sites-enabled/ 此文件夹的内容与sites-available下内容关联


本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!