How do I configure the TCP/IP port listened on by a Spring Boot application, so it does not use the default port of 8080.
Java – How to Configure Port for a Spring Boot Application
javaportserverspringspring-boot
Related Solutions
Actually you want to change server.port
and you can change it in many different ways as described http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-external-config
Examples:
- in your application.properties (in or outside the jar)
command line
java -Dserver.port=$PORT -jar target/demo-0.0.1-SNAPSHOT.jar
and much more
On linux ports below 1024 can be opened only by root, so the port 80 is restricted by default
if you want to publish your app on 80 port you need to redirect request from port 80 to the port you gonna run your springapp (e.g 8080) port
Solution 1: HTTP Proxy server
You can use Apache2 server which is allowed by default to work on port 80 and can forward requests for you to Tomcat
Example configuration for Debian
sudo apt-get install apache2
a2enmod proxy
a2enmod proxy_http
cd /etc/apache2/sites-enabled
sudo nano 000-default.conf
Edit file:
<VIRTUALHOST *:80>
ProxyPreserveHost On
# ...
ProxyPass / http://localhost:8080/
</VIRTUALHOST>
Save file: Ctrl+O, ENTER, Ctrl+X
Note: To learn more about virtual host configurations, you can check out the detailed Apache manual on the subject by clicking here.
Restart Apache2 to apply changes:
sudo service apache2 restart
or
sudo systemctl restart apache2
Solution 2: Port forwarding
Use iptables for redirects
iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to-ports 8080
if you need to use localhost also add this
iptables -t nat -I OUTPUT -p tcp -d 127.0.0.1 --dport 80 -j REDIRECT --to-ports 8080
Best Answer
As said in docs either set
server.port
as system property using command line option to jvm-Dserver.port=8090
or addapplication.properties
in/src/main/resources/
withFor a random port use:
Similarly add
application.yml
in/src/main/resources/
with: