1. 首页
  2. >
  3. 技术信息
  4. >
  5. 技术文章

5分钟搭建公网https网页文件服务器,免费权威TLS证书

前言

本文主要讲解如何快速搭建一个https网页文件服务器,并免费申请权威机构颁发的tls证书。

5分钟搭建公网https网页文件服务器,免费权威TLS证书

5分钟搭建公网https网页文件服务器,免费权威TLS证书


最终成果:
通过浏览器打开指定网页,可以浏览和下载页面上的文件,并且通过tls协议加密传输。

部署前提条件:

  • 带公网IP的服务器
  • 熟悉基本的命令行操作
  • 具有相关域名的DNS控制,可以做域名与IP地址的映射
  • 具有系统的root账号权限

组件介绍:

  • CentOS 7.X 操作系统
  • ngnix:Nginx是一款面向性能设计的HTTP服务器,也可以用作反向代理、负载平衡器和HTTP缓存。
  • python2-certbot-nginx:用于申请和管理免费的tls数字证书

安装步骤

2.1 安装相关组件

安装epel仓库,刷新本地缓存

yum -y install epel-release && yum -y update 

安装nginx和certbot证书管理组件,申请免费tls就用它

yum -y install nginx python2-certbot-nginx 

2.2 设置开机自启动nginx服务

systemctl enable nginx 

输出结果:

Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service. 

5分钟搭建公网https网页文件服务器,免费权威TLS证书

设置开机自启动nginx服务


2.3 创建测试用的文件和目录

mkdir /usr/share/nginx/files touch /usr/share/nginx/files/test.txt 

2.4 编写nignx虚拟主机配置文件

vi /etc/nginx/conf.d/file_server.conf 

添加以下内容:

server { listen 80; server_name files.example.com; charset utf-8; root /usr/share/nginx/files; location / {  autoindex on;  autoindex_exact_size on;  autoindex_localtime on;  } } 

5分钟搭建公网https网页文件服务器,免费权威TLS证书

编写nignx虚拟主机配置文件


2.5 CentOS用户需要配置防火墙开放相关端口

firewall-cmd --add-service=http --permanent firewall-cmd --add-service=https --permanent firewall-cmd --reload 

2.6 配置SELinux

restorecon -RvF /usr/share/nginx/ 

2.7 申请Let's Encrypt免费tls证书

certbot --nginx 

按下图提示操作即可:

5分钟搭建公网https网页文件服务器,免费权威TLS证书

执行certbot程序申请Let's Encrypt免费tls证书


如果看到以下提示,则表明已经成功申请Let's Encrypt免费tls证书

5分钟搭建公网https网页文件服务器,免费权威TLS证书

成功申请Let's Encrypt免费tls证书


检查

cat /etc/nginx/conf.d/file_server.conf 

可以看到certbot已经把配置文件修改适配为https。

nginx -t 

输出以下结果表示通过测试:

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful 

5分钟搭建公网https网页文件服务器,免费权威TLS证书

测试nginx配置


2.8 开启nginx服务

systemctl start nginx.service 

2.9 浏览器上作最终测试

浏览器上打开域名后,应该可以看到以下内容,并且留意这是一个https连接:

5分钟搭建公网https网页文件服务器,免费权威TLS证书

浏览器上作最终测试


2.10 每90天重新申请tls证书

免费的https证书每隔90天要更新一次,90天后执行以下命令更新证书,或者写成crontab计划任务自动更新。

certbot renew 

3. 总结

通过本文,您应该学会如何快速配置一台https加密网页文件分享服务器了吗?使用nginx和tls证书可以提供标准的https网页文件分享服务。本文也简单介绍了如何申请免费的TLS证书,该证书由Let's Encrypt机构颁发。

Redis 之哈希
« 上一篇 2021年01月30日 pm15:19
Nginx访问日志和错误日志入门
下一篇 » 2021年01月30日 pm15:21

相关推荐