安装和配置 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 日志以解决部署问题。
相关推荐
Elastic 中国社区官方博客4 小时前
使用 Elastic Cloud Serverless 扩展批量索引
大数据·运维·数据库·elasticsearch·搜索引擎·云原生·serverless
没有bug.的程序员4 小时前
服务安全:内部服务如何防止“裸奔”?
java·网络安全·云原生安全·服务安全·零信任架构·微服务安全·内部鉴权
一线大码4 小时前
SpringBoot 3 和 4 的版本新特性和升级要点
java·spring boot·后端
超龄超能程序猿4 小时前
Docker GPU插件(NVIDIA Container Toolkit)安装
运维·docker·容器
weixin_440730505 小时前
java数组整理笔记
java·开发语言·笔记
weixin_425023005 小时前
Spring Boot 实用核心技巧汇总:日期格式化、线程管控、MCP服务、AOP进阶等
java·spring boot·后端
一线大码5 小时前
Java 8-25 各个版本新特性总结
java·后端
岁岁种桃花儿5 小时前
Nginx 站点垂直扩容(单机性能升级)全攻略
网络·nginx·dns
2501_906150565 小时前
私有部署问卷系统操作实战记录-DWSurvey
java·运维·服务器·spring·开源
better_liang5 小时前
每日Java面试场景题知识点之-TCP/IP协议栈与Socket编程
java·tcp/ip·计算机网络·网络编程·socket·面试题