安装和配置 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 日志以解决部署问题。
相关推荐
麦兜*1 分钟前
SpringBoot Actuator监控端点详解,打造生产级应用健康检查
java·spring boot·后端
悟能不能悟8 分钟前
Spring Boot 中处理跨域资源
java·spring boot·后端
qq_124987075310 分钟前
基于springboot+vue的无人机共享管理系统(源码+论文+部署+安装)
java·vue.js·spring boot·后端·毕业设计·无人机·计算机毕业设计
中国lanwp12 分钟前
RedHat/CentOS 系统中根目录作用说明
linux·运维·centos
多多*13 分钟前
计算机网络相关 讲一下rpc与传统http的区别
java·开发语言·网络·jvm·c#
源码获取_wx:Fegn089515 分钟前
计算机毕业设计|基于springboot + vue网上超市系统(源码+数据库+文档)
java·数据库·vue.js·spring boot·后端·spring·课程设计
码农水水16 分钟前
阿里Java面试被问:Online DDL的INSTANT、INPLACE、COPY算法差异
java·服务器·前端·数据库·mysql·算法·面试
是Yu欸17 分钟前
实时获取 Google 相关股票新闻并完成自动化总结
运维·爬虫·自动化·股票·新闻·亮数据·bringdata
小旭952717 分钟前
【Java 基础】IO 流 全面详解
java·开发语言
Coder_Boy_20 分钟前
基于SpringAI的在线考试系统-阅卷评分与错题管理模块回归测试逻辑梳理文档
java·spring boot·系统架构·ddd·tdd·全栈开发