在本教程中,我们将学习如何使用Docker在CentOS7上部署Nginx服务并配置HTTPS。通过Docker容器化技术,我们可以快速搭建和管理Web服务器,确保服务的安全性和可靠性。
首先,我们需要在CentOS7上安装Docker。运行以下命令:
sudo yum install -y yum-utilssudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.reposudo yum install docker-ce docker-ce-cli containerd.iosudo systemctl start dockersudo systemctl enable docker
使用Docker拉取官方的Nginx镜像:
sudo docker pull nginx:latest
为了配置HTTPS,我们需要SSL证书。您可以使用自签名证书或从CA获取。这里以自签名证书为例:
sudo mkdir -p /etc/nginx/sslsudo openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout /etc/nginx/ssl/nginx.key -out /etc/nginx/ssl/nginx.crt
按照提示填写证书信息。
创建一个Nginx配置文件以启用HTTPS。创建文件 /etc/nginx/conf.d/default.conf 并添加以下内容:
server { listen 80; server_name localhost; return 301 https://$server_name$request_uri;}server { listen 443 ssl; server_name localhost; ssl_certificate /etc/nginx/ssl/nginx.crt; ssl_certificate_key /etc/nginx/ssl/nginx.key; location / { root /usr/share/nginx/html; index index.html index.htm; }} 现在,运行Docker容器来部署Nginx服务:
sudo docker run -d --name nginx-https -p 80:80 -p 443:443 -v /etc/nginx/conf.d:/etc/nginx/conf.d -v /etc/nginx/ssl:/etc/nginx/ssl nginx:latest
这个命令将运行一个名为nginx-https的容器,映射端口80和443,并挂载配置和SSL证书目录。
打开浏览器,访问 https://您的服务器IP 。由于使用自签名证书,浏览器可能会显示安全警告,但您可以继续访问。您应该看到Nginx欢迎页面。
通过本教程,您已经成功使用Docker在CentOS7上部署了Nginx服务并配置了HTTPS。这种方法简单高效,适合快速搭建安全Web服务。记得在生产环境中使用有效的SSL证书。
本文由主机测评网于2026-02-09发表在主机测评网_免费VPS_免费云服务器_免费独立服务器,如有疑问,请联系我们。
本文链接:https://www.vpshk.cn/20260224124.html