搭建一个基于nginx的https服务器

背景目的

在ubuntu系统上搭建一个https服务器,客户端在浏览器上用域名访问https://hello.example.com,页面显示hello world!

搭建步骤

安装nginx服务器

  • 安装软件包 apt install nginx -y
  • 确认运行状态 systemctl status nginx
  • 测试默认页面 http://localhost

后续关于nginx的配置围绕以下三个文件展开

  1. 主配置文件 /etc/nginx/nginx.conf
  2. 网站配置文件 /etc/nginx/sites-available/
  3. 网站配置使能 /etc/nginx/sites-enabled/

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.pemrootCA.pem两个文件,后者需要放到客户端
  • 生成域名证书
    mkdir /etc/ssl/hello
    cd /etc/ssl/hello
    mkcert hello.example.com
    在/etc/ssl/hello目录下会生成hello.example.com-key.pemhello.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页面大概有哪些工作要做,实际生产环境不太可能按照上述步骤来。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值