安装和配置 Nginx 以及部署 Java 应用

第一步:安装 Nginx

在 Ubuntu/Debian 上安装
  1. 更新包列表:

    bash 复制代码
    sudo apt update
  2. 安装 Nginx:

    bash 复制代码
    sudo apt install nginx
  3. 启动 Nginx 并设置开机启动:

    bash 复制代码
    sudo systemctl start nginx
    sudo systemctl enable nginx
  4. 验证 Nginx 是否成功启动:

    bash 复制代码
    sudo systemctl status nginx
在 CentOS/RHEL 上安装
  1. 更新包列表:

    bash 复制代码
    sudo yum update
  2. 安装 Nginx:

    bash 复制代码
    sudo yum install epel-release
    sudo yum install nginx
  3. 启动 Nginx 并设置开机启动:

    bash 复制代码
    sudo systemctl start nginx
    sudo systemctl enable nginx
  4. 验证 Nginx 是否成功启动:

    bash 复制代码
    sudo systemctl status nginx

第二步:配置 Nginx

配置 Nginx 服务器块
  1. 创建一个新的 Nginx 服务器块配置文件:

    bash 复制代码
    sudo nano /etc/nginx/conf.d/example.com.conf
  2. 编辑配置文件:

    nginx 复制代码
    server {
        listen 80;
        server_name example.com www.example.com;
        return 301 https://$host$request_uri; # 重定向 HTTP 到 HTTPS
    }
    
    server {
        listen 443 ssl; # 监听 HTTPS 请求
        server_name example.com www.example.com;
    
        # SSL 证书路径
        ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
        ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
    
        # 配置 SSL 设置
        ssl_session_cache shared:SSL:1m;
        ssl_session_timeout 10m;
        ssl_protocols TLSv1.2 TLSv1.3; # 使用最新的安全协议
        ssl_ciphers HIGH:!aNULL:!MD5;
        ssl_prefer_server_ciphers on;
    
        location / {
            proxy_pass http://localhost:8080; # 将请求转发给 Java 应用
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection 'upgrade';
            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;
            proxy_set_header X-Forwarded-Host $server_name;
            proxy_set_header X-NginX-Proxy true;
        }
    
        # 自定义错误页面
        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
            root /usr/share/nginx/html;
        }
    }
  3. 保存并关闭配置文件。

  4. 测试配置:

    bash 复制代码
    sudo nginx -t
  5. 重启 Nginx 以使更改生效:

    bash 复制代码
    sudo systemctl restart nginx

第三步:部署 Java 应用

  1. 安装 Java 运行环境:

    bash 复制代码
    sudo apt install openjdk-11-jdk # 对于 Ubuntu/Debian
    sudo yum install java-11-openjdk-devel # 对于 CentOS/RHEL
  2. 安装 Tomcat:

    bash 复制代码
    sudo apt install tomcat9 # 对于 Ubuntu/Debian
    sudo yum install tomcat # 对于 CentOS/RHEL
  3. 部署 Java 应用:

    • 将 .war 或 .jar 文件放到 Tomcat 的 webapps 目录下。
    • 如果是 .war 文件,Tomcat 会自动解压并部署。
    • 如果是 .jar 文件,需要手动配置 context.xml 并放置在 webapps/ROOT/WEB-INF 目录下。
  4. 启动 Tomcat:

    bash 复制代码
    sudo systemctl start tomcat9 # 对于 Ubuntu/Debian
    sudo systemctl start tomcat # 对于 CentOS/RHEL
  5. 设置 Tomcat 开机启动:

    bash 复制代码
    sudo systemctl enable tomcat9 # 对于 Ubuntu/Debian
    sudo systemctl enable tomcat # 对于 CentOS/RHEL
  6. 验证 Tomcat 是否启动成功:

    bash 复制代码
    sudo systemctl status tomcat9 # 对于 Ubuntu/Debian
    sudo systemctl status tomcat # 对于 CentOS/RHEL

第四步:获取 SSL 证书

  1. 安装 Certbot:

    bash 复制代码
    sudo apt install certbot python3-certbot-nginx # 对于 Ubuntu/Debian
    sudo yum install certbot python3-certbot-nginx # 对于 CentOS/RHEL
  2. 获取并设置证书:

    bash 复制代码
    sudo certbot --nginx -d example.com -d www.example.com
  3. 自动续期证书:

    bash 复制代码
    sudo systemctl restart nginx

完成上述步骤后,Nginx 服务器可以处理 HTTP 和 HTTPS 请求,并将流量转发到 Tomcat 中的 Java 应用。

注意事项

  • 确保防火墙允许 HTTP (80) 和 HTTPS (443) 流量。
  • 根据实际需要调整 SSL 设置。
  • 检查 Tomcat 日志以解决部署问题。
相关推荐
小南家的青蛙42 分钟前
微服务体系下DevOps运维岗位面试方案设计
运维·面试·职场和发展·devops
杨凯凡1 小时前
Spring Boot 链路追踪实战:Micrometer、TraceId 与异步线程上下文传播
java·spring boot·后端
计算机毕设定制辅导-无忧学长1 小时前
《基于Spring Boot的迷游哈游戏综合服务平台的开发》
java·vue.js·spring boot·mysql·迷游哈游戏综合服务平台
非优秀程序员10 小时前
第04章 邮箱大战,163 与 QQ 的暗战|《互联网产品恩仇录》
java·vue.js·架构
知已难寻11 小时前
Docker 入门:容器技术与 Docker 基础
java·运维·服务器·docker·云原生·容器
文丑颜不良啊13 小时前
LeetCode2481~2500题目解析
java·leetcode
Flynn Liang14 小时前
同一需求三套写法:用 Spring AI、LangChain4j、AgentScope Java 各实现一个带工具调用的 RAG 助手(实战对比)
java·人工智能·大模型·架构设计·spring ai·langchain4j·agentscope
聪明的笨猪猪14 小时前
实际工程问题:AI 生成 Spring Cache(@Cacheable / @CachePut / @CacheEvict)缓存注解踩坑
java·经验分享·面试·ai编程
愿有所息15 小时前
记一次 ARM64 + JDK8 上 fastjson2 的诡异数据错乱
java
秋饼15 小时前
MCP 2026-07-28 无状态协议大改版:Spring AI 2.1 多轮往返请求与网关路由落地
java·ai·技术分享·后端开发