Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I run a Spring Boot application on port 80

I can't start an application on port 80.

I have tried on my local computer (using my IDE, and on a local server), no luck.

I have checked other similar posts and make sure that I run jar on server with root.

This is the error:

 till here all ok ... java.net.SocketException: Permission denied at sun.nio.ch.Net.bind0(Native Method) at sun.nio.ch.Net.bind(Net.java:433) at sun.nio.ch.Net.bind(Net.java:425) at sun.nio.ch.ServerSocketChannelImpl.bind(ServerSocketChannelImpl.java:223) at sun.nio.ch.ServerSocketAdaptor.bind(ServerSocketAdaptor.java:74) at org.apache.tomcat.util.net.NioEndpoint.bind(NioEndpoint.java:338) at org.apache.tomcat.util.net.AbstractEndpoint.start(AbstractEndpoint.java:760) at org.apache.coyote.AbstractProtocol.start(AbstractProtocol.java:472) at org.apache.catalina.connector.Connector.startInternal(Connector.java:986) at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) at org.apache.catalina.core.StandardService.addConnector(StandardService.java:237) at org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainer.addPreviouslyRemovedConnectors(TomcatEmbeddedServletContainer.java:186) at org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainer.start(TomcatEmbeddedServletContainer.java:149) at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.startEmbeddedServletContainer(EmbeddedWebApplicationContext.java:288) at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.finishRefresh(EmbeddedWebApplicationContext.java:141) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:483) at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118) at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:686) at org.springframework.boot.SpringApplication.run(SpringApplication.java:320) at org.springframework.boot.SpringApplication.run(SpringApplication.java:957) at org.springframework.boot.SpringApplication.run(SpringApplication.java:946) at com.andirod.StartApplication.main(StartApplication.java:20) ... ... ... Exception in thread "main" java.lang.IllegalStateException: Tomcat connector in failed state at org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainer.start(TomcatEmbeddedServletContainer.java:157) at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.startEmbeddedServletContainer(EmbeddedWebApplicationContext.java:288) at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.finishRefresh(EmbeddedWebApplicationContext.java:141) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:483) at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118) at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:686) at org.springframework.boot.SpringApplication.run(SpringApplication.java:320) at org.springframework.boot.SpringApplication.run(SpringApplication.java:957) at org.springframework.boot.SpringApplication.run(SpringApplication.java:946) at com.andirod.StartApplication.main(StartApplication.java:20) 
like image 727
5er Avatar asked Nov 14 '15 00:11

5er


People also ask

How do I change the running port of spring boot?

We can change the port of the Spring Boot in the following ways: By Adding the configuration in the application properties of the Spring Boot project. By Implementing the WebServerFactoryCustomizer interface in the component class. Changing the configuration of VM options.

What port does spring boot run on?

Introduction. Spring Boot applications ship with an embedded server, and the default port for them is 8080 .


2 Answers

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 
like image 61
Oskar Dajnowicz Avatar answered Sep 25 '22 07:09

Oskar Dajnowicz


Use sudo on linux.

I was running a Spring Boot application on Ubuntu and java -jar app.jar --server.port=80 gave me the same error. Since ports below 1024 can only be opened by root access, so use"sudo": sudo java -jar app.jar --server.port=80.

This way of deployment is only suggested for local tests due to security concerns. See comments for details.

like image 45
PhoenixPan Avatar answered Sep 22 '22 07:09

PhoenixPan