本文将介绍基于 Debian 的系统上使用 Docker 和 Nginx 进行 Web 应用部署的过程。着重介绍了 Debian、Docker 和 Nginx 的安装和配置。
sudo apt update
sudo apt upgrade
sudo apt install docker.io
sudo apt install nginx
1. 为 Web 应用创建一个新的 Nginx 配置文件:
sudo nano /etc/nginx/sites-available/app.conf
2. 将以下内容添加到配置文件中,将 app.example.com
替换为域名,将 app_container
替换为 Docker 容器的访问地址:
server {
listen 80;
server_name app.example.com;
location / {
proxy_pass http://app_container:8080;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
3. 创建软链接以启用站点:
sudo ln -s /etc/nginx/sites-available/app.conf /etc/nginx/sites-enabled/
4. 测试 Nginx 配置:
sudo nginx -t
5. 如果配置有效,请重新加载 Nginx:
sudo systemctl reload nginx
docker run -d --name app_container -p 8080:80 app_image
将 app_image
替换为 Web 应用的 Docker 镜像名称。
Debian 系统上,可以使用 ufw
(Uncomplicated Firewall)来管理防火墙规则。要检查当前规则,运行:
sudo ufw status
确保允许必要的端口(例如,HTTP 和 HTTPS 分别需要 80 和 443 端口,以及后端服务所需的其他端口)。
如果需要打开特定端口,使用以下命令:
sudo ufw allow [port_number]/tcp
将 [port_number]
替换为要打开的实际端口数字。
app
。233.233.233.233
。app.example.com
。app
。233.233.233.233
。现在,通过在 Nginx 配置文件中配置的域名访问 Web 应用了。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有