背景目的
在ubuntu系统上搭建一个https服务器,客户端在浏览器上用域名访问https://hello.example.com,页面显示hello world!
搭建步骤
安装nginx服务器
- 安装软件包 apt install nginx -y
- 确认运行状态 systemctl status nginx
- 测试默认页面 http://localhost
后续关于nginx的配置围绕以下三个文件展开
nginx配置命令
- 检查配置语法 nginx -t
- 重载配置 systemctrl reload nginx
安装域名服务器
- 安装bind9软件包 apt install bind9 bind9utils bind9-doc
- 创建区域配置文件 mkdir -p /etc/bind/zones cd /etc/bind/zones touch db.example.com
- 修改区域配置文件
;
; BIND data file for local loopback interface
;
$TTL 604800
@ IN SOA example.com. root.example.com. (
2 ; Serial
604800 ; Refresh
86400 ; Retry
2419200 ; Expire
604800 ) ; Negative Cache TTL
;
IN NS ns.example.com.
ns IN A 192.168.2.188
hello IN A 192.168.2.188
- 添加区域配置,打开*/etc/bind/named.conf.local*并添加
zone "example.com" {
type master;
file "/etc/bind/zones/db.example.com";
};
- 检查配置 named-checkconf named-checkzone /etc/bind/zones/db.example.com
- 重启服务 systemctl restart bind9
- 测试服务 在其他设备上,ping hello.example.com或者nslookup hello.example.com
制作签名证书
使用mkcert工具生成证书,用以下几个简单步骤实现:
- 下载安装mkcert工具 apt install libnss3-tools wget https://github.com/FiloSottile/mkcert/releases/download/v1.4.4/mkcert-v1.4.4-linux-amd64 -O mkcert chmod +x mkcert mv mkcert /usr/local/bin/
- 初始化本地CA根证书 mkcert install 生成的证书路径是/root/.local/share/mkcert/,包含rootCA-key.pem和rootCA.pem两个文件,后者需要放到客户端
- 生成域名证书 mkdir /etc/ssl/hello cd /etc/ssl/hello mkcert hello.example.com 在/etc/ssl/hello目录下会生成hello.example.com-key.pem和hello.example.com.pem两个文件
- 客户端上手动安装证书 1.windows上win+r输入mmc打开控制台,文件->添加/删除管理单元->证书->增加,选择本地计算机,点完成 2.证书->受信任的根证书颁发机构->证书,右键,所有任务->导入,选择在第二步里生成的证书,根据提示完成导入
制作网页
nginx服务器存放网页文件的路径是/var/www/html,但是这次不做那么复杂,只是单纯返回一个字符串
- 在路径 /etc/nginx/sites-available 下创建站点配置文件hello cd /etc/nginx/sites-available touch hello
- 在hello增加如下内容
server {
listen 443 ssl;
server_name hello.example.com;
ssl_certificate /etc/ssl/hello/hello.example.com.pem;
ssl_certificate_key /etc/ssl/hello/hello.example.com-key.pem;
location / {
add_header Content-Type application/json;
return 200 'hello world!';
}
}
- 使能站点配置文件 在路径 /etc/nginx/sites-enabled 下创建指向hello的软链接 cd /etc/nginx/sites-enabled ln -s hello /etc/nginx/sites-available/hello
- 重新加载nginx配置 systemctl reload nginx
测试网页
打开浏览器,输入 https://hello.example.com/ ,显示如下信息: hello world! 表示测试成功
总结
本文展示了创建一个https页面大概有哪些工作要做,实际生产环境不太可能按照上述步骤来。
评论前必须登录!
注册