如何在已托管域名下部署Angular前端与Spring Boot后端Web应用
Deploying Spring Boot Backend with Angular Frontend on Your Domain
Hey there! Let's break down how to get your Spring Boot backend up and running alongside your already deployed Angular frontend. Unlike static Angular files (your dist folder), Spring Boot is a Java application that needs a runtime environment and proper configuration to work—here's what you need to do based on common hosting setups:
If you're using a VPS/Cloud Server (e.g., AWS EC2, DigitalOcean Droplet, Alibaba Cloud ECS)
This is the most flexible setup for full control over your deployment:
- Install a compatible JDK: Spring Boot 3.x requires JDK 17 or higher; Spring Boot 2.x works with JDK 8/11. Install it via your server's package manager, e.g.,
sudo apt install openjdk-17-jdkon Ubuntu,yum install java-17-openjdkon CentOS. - Package your Spring Boot app: Run
mvn clean package(Maven) or./gradlew bootJar(Gradle) in your project root. This generates an executable JAR file in thetarget/(Maven) orbuild/libs/(Gradle) directory. - Upload the JAR to your server: Use SFTP, SCP, or your host's file manager to transfer the JAR to a dedicated directory (like
/opt/your-backend/). - Start the backend service:
- For testing: Run
java -jar your-app-name.jardirectly, but this stops when you close your terminal. - For production: Use a background process like
nohup java -jar your-app-name.jar &(logs go tonohup.out), or set up asystemdservice to auto-start the app on server boot and restart it if it crashes.
- For testing: Run
- Configure reverse proxy with Nginx/Apache: Since your Angular frontend is already served, add a reverse proxy rule to forward API requests to Spring Boot. Example Nginx config:
This routeslocation /api/ { proxy_pass http://localhost:8080/; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; }yourdomain.com/api/requests to your Spring Boot app on port 8080, avoiding cross-origin issues.
If you're using a PaaS Platform (e.g., Heroku, AWS Elastic Beanstalk, Alibaba Cloud Serverless)
These platforms handle infrastructure management for you:
- Package your Spring Boot app: Build the executable JAR as described above.
- Deploy the JAR: Most PaaS tools let you upload the JAR directly or connect your Git repo to auto-build/deploy on code pushes.
- Configure environment variables: Set database connection strings, API keys, etc., in the platform's dashboard—these override your
application.properties/application.ymlsettings. - Set up routing: Ensure the platform routes traffic to your backend's default port (8080) and configure your Angular app to send API requests to the same domain (or enable CORS if using separate URLs).
If you're on a basic shared hosting plan (static files only)
Most basic shared hosts don't support Java applications, so you can't run Spring Boot here. You'll need to upgrade to a VPS, cloud server, or Java-compatible PaaS.
Key Notes to Remember
- CORS Handling: If you skip reverse proxy, enable CORS in Spring Boot with
@CrossOriginon controllers or a global CORS bean. - Firewall Settings: Open your Spring Boot port (8080 by default) in the server firewall, or use Nginx/Apache to listen on standard 80/443 ports and forward to 8080.
- Database Access: Whitelist your server's IP address in your cloud database settings to let the backend connect.
内容的提问来源于stack exchange,提问作者Duarte Somsen
相关产品推荐
相关产品推荐

